1

I create my views programmatically. If I do not put loadView method, the app runs well. However, When I add loadView method like this:

- (void)loadView
{
    NSLog(@"loadView is called");
}

I found this method was called many times! At last, the app crashed.

I wonder why loadView method is called so many time.

Can anyone help? Thanks a lot!

Magnus Engdal
  • 5,446
  • 3
  • 31
  • 50
Sai
  • 120
  • 10
  • Is this all you have in your `loadView` method? Where is the code that calls this method? – Popeye Dec 17 '13 at 10:30
  • are you calling self.view in init method or something? – Joshua Dec 17 '13 at 10:31
  • I did use self.view in ViewDidLoad method: [self.view addSubview:self.textField]; – Sai Dec 17 '13 at 10:43
  • Have a look at this http://stackoverflow.com/questions/573958/iphone-sdk-what-is-the-difference-between-loadview-and-viewdidload you seem to have infinite recursion because you are calling `self.view` from link - `"The self.view property accessor calls -loadView if the view isn't currently loaded"` – Popeye Dec 17 '13 at 10:45
  • The self.view isn't allocated yet so it goes to loadView then goes back to viewDIdload which inturns calls again the loadView function. did I explain that clearly? something like loadview -> viewDidLoad->loadview based on the implemntation you are currently doing – Joshua Dec 17 '13 at 10:49
  • @Joshua I was hoping you would give it as an answer as you were spot on. – Popeye Dec 17 '13 at 10:50
  • @Popeye I was about to do it but jrturton beat me to it. atleast I hope I explained it somehow – Joshua Dec 17 '13 at 10:53
  • The link you shared answered my question very well. Thanks, all. – Sai Dec 17 '13 at 11:40

2 Answers2

7

loadView is expected to, at some point, populate the view property of a view controller. The view property is lazily loaded (look at the call stack, you will see a method called something like _loadViewIfNeeded).

If loadView doesn't create a view, then each time the .view property is accessed, the view controller will call loadView again, trying to lazily load the view. At some point everything will go wrong because a view controller needs a view. If you access self.view from within your custom loadView, you'll get an infinite loop.

From the documentation:

You can override this method in order to create your views manually. If you choose to do so, assign the root view of your view hierarchy to the view property. The views you create should be unique instances and should not be shared with any other view controller object. Your custom implementation of this method should not call super.

jrturton
  • 118,105
  • 32
  • 252
  • 268
0

In your load view are you calling [self loadView] rather than [super loadView]

Matt Rees
  • 884
  • 8
  • 14
  • Nop, I didn't call [super loadView]. Maybe the reason is I used self.view in ViewDidLoad. – Sai Dec 17 '13 at 10:46
  • You shouldn't be calling loadView at all. https://developer.apple.com/LIBRARY/IOS/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/loadView for reference – Joshua Dec 17 '13 at 10:56
  • Why not Joshua? Direct quote from your Apple link "If you cannot define your views in a storyboard or a nib file, override the loadView method to manually instantiate a view hierarchy and assign it to the view property." – Matt Rees Dec 17 '13 at 11:28
  • i use only coding part (with out xib and story board) in ViewDidLoad we can use self.view it will never create an issue. – Charan Giri Dec 17 '13 at 11:32
  • @matt what i mean is you do not directly call it like [self loadview] let the viewController handle the call and just override the loadview to setup the current view. hope it makes sense – Joshua Dec 17 '13 at 13:36