Code Change?


So I’m starting chapter 7 [delegation and text input] and it says to change the loadViewMethod in BNRHypnosisViewController.m to

     CGRect frame = [UIScreen mainScreen].bounds;
     BNRHypnosisView *backgroundView = [[BNRHypnosisView alloc] initWithFrame:frame];
     CGRect textFieldRect  = CGRectMake(40, 70, 240, 30);

But as I look at the code I have for that method it doesn’t match it, i have nothing about setting the frame. So I decided to search the document for that code, and the last code that the book has for that method matches what I had

- (void) loadView
     // Create a view
     BNRHypnosisView *backgroundView = [[ BNRHypnosisView alloc] init]; 
     // Set it as *the* view of this view controller
     self.view = backgroundView;

Has anyone else noticed this? Was there something I missed where the code was changed and why?


Hi allocInit,

I noticed the same thing. I am pretty sure it’s a print error.


Unfortunately I am noticing a lot of print errors and inconsistencies. Makes me wonder if this book was ready for prime time or not.


Definitely a print error. That tripped me up for a second as well.


This took up about an hour of my time yesterday because I was trying to set the positioning of the UISegmentedControl relative to the view’s frame and experimenting with NSLayoutConstraints to programmatically auto layout. But even though the view is successfully initialised without initWithFrame (i.e. with the code as we typed it in) the view’s frame.size attribute seems to be 0. With the code as it appears here, it all seems to work. Is it best practice to use initWithFrame when initialising a view?

     CGRect frame = [UIScreen mainScreen].bounds;
     BNRHypnosisView *backgroundView = [[BNRHypnosisView alloc] initWithFrame:frame];
     BNRHypnosisView *backgroundView = [[BNRHypnosisView alloc] init]; 

The two code snippets above are the same. In case of UIView, the frame size tends to be resized depending on object it belongs to. So, even if frame size parameter is specified, it is updated by the container object such as UIWindow and UITabBarController as to another size such as window/screen size. UITabBarController’s doc below is also mentioning about resizing.

So, if you need to access frame size in initWithFrame, it might be necessary to set the frame size same as the resized one.