简体   繁体   中英

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

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.

我删除了loadView方法,它为我修复了它,如果你的.m文件中有这个方法删除它!

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.

如果您创建@property (nonatomic, strong) UIView *loadView并在viewDidLoad访问它,也会发生这种情况您必须重命名该属性。

试试[self.view addSubview:topLevelController.view]

设置断点并查看循环运行的位置......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM