- viewDidLoad infinite loop issue...(iOS)
I'm attempting to write a multiview app in iOS and really having a bit of a tough time... I've setup a new project and I've got a rootViewController being launched by the appDelegate. In turn, the rootViewController attempts to load and display my first content view, although I seem to have fallen into some kind of infinite loop, I'm hoping someone here may have a hunch as to why...
-(void)viewDidLoad
{
// Load up new inst开发者_开发问答ance of view
TopLevelViewController *topLevelController =
[[TopLevelViewController alloc] initWithNibName:@"TopLevelView" bundle:nil];
// Hand off viewController reference to root controller
self.topLevelViewController = topLevelController;
// Display the view
[self.view insertSubview:topLevelController.view atIndex:0];
// Release viewController
[topLevelController release];
[super viewDidLoad];
}
Above is my rootViewController viewDidLoad: method, although every time it executes insertSubview, it seems to return to the top and perform the whole thing again. I'm a bit confused as I've based this code almost identically on a tutorial I followed and it ran beautifully...which leads me to think the problem must be elsewhere, although I couldn't possibly think where.
Appreciate any insight!
I ran into the same issue and cost a while to figue out.
When self.view doesn't exist, iOS will call loadview/viewdidload and try to create the view. this cause the deadloop. In my case, I didn't call [super loadView] in my loadView, and cause this problem.
See this discussion http://forums.macrumors.com/showthread.php?t=552520
Set a breakpoint on viewDidLoad
, continue a few times, then grab the backtrace and post it.
Also, add NSLog(@"%@ self is %p", NSStringFromSelector(_cmd), self);
to the beginning of your viewDidLoad
. It might be that you have created a sort of "infinite mirrors" configuration of nib files; if the hex number keeps changing, that'll be different instances of your view.
I know it sounds simple but try moving
[super viewDidLoad]
to the top of your code. I have a feeling that super has to be the first thing u call.
Just realized I had never closed this after the recent interest.
As I remember, @Wolfgang Schreurs is exactly correct. For whatever reason, while experimenting with the code, I had topLevelViewController
inherit from rootViewController
.
Hopefully this helps anyone else who may run into the same issue.
I removed the loadView method and it fixed it for me , if you have that method in your .m file delete it!!
Try [self.view addSubview:topLevelController.view]
Set a breakpoint and look where your loop is running through...
This happens also if you make a @property (nonatomic, strong) UIView *loadView
and access it in viewDidLoad
You have to rename the property.
精彩评论