Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

- 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 instance 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!

like image 753
eriknelson Avatar asked Nov 28 '22 10:11

eriknelson


2 Answers

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

like image 86
Robert Mao Avatar answered Dec 07 '22 01:12

Robert Mao


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.

like image 20
bbum Avatar answered Dec 06 '22 23:12

bbum