Question

Sorry for the noob question, but I'm starting to use structs in objective-c for the CG data types. If I create a new CGRect, and I set it to some view.frame, do they now point to the same thing in memory? Or are they two separate objects and if I change one, I will not change the other? Thx.

What is happening is I'm creating a CGRect and setting it to my view.frame. Then based on the data, I update the frame.size.height. However, in my else statement, I'd like to change it back to it's original size, so if I then select a different object, it's frame is large. It seems like once I set it the frame to a smaller size, it stays at that size.

CGRect frameToCenter = self.SView.frame;
CGFloat originalHeight = self.SView.frame.size.height;
//CGFloat originalHeight = 500.0; //if I hardcode it works.

if ([self.array count] == 1) {
    frameToCenter.size.height = totalRowHeight;
}
else {
    frameToCenter.size.height = originalHeight;
}
self.SView.frame = frameToCenter;

I don't want to hard code anything, but I wanted to get the basic funcitonality working so I started with when I only have 1 in my array. And then for other numbers, the view would go back the original size. But once it hits that first if statement, it never gets its height changed. The only way I can make it change back is if I hardcode a CGFloat value before this block.

Was it helpful?

Solution

First of all, CGRect is not an object at all. It sounds like you know that, but the question "are they two separate objects" hints at confusion on the point.

Since you're assigning the actual struct, and not a pointer to the struct, the assignment copies values from the origin to the receiver. After the assignment, changing one of them will have no effect on the other unless you do another assignment. So this code does not change the view's frame:

CGRect myRect = myView.frame;
myRect.origin.x += 10.0;

If I want to change the frame I need another line to assign the changed value back to the view's frame:

myView.frame = myRect;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top