开发者

Does UIWindow randomly remove its subview?

开发者 https://www.devze.com 2023-01-23 16:41 出处:网络
The reason I ask is because I was doing the following in AppDelegate.m: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

The reason I ask is because I was doing the following in AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Add the view controller's view to the window and display.
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    PageViewController *pageViewController = [[PageViewController alloc] init];
    [window addSubview:pageViewController.view];
    [pageViewController release];
    [window makeKeyAndVisible];

    return YES;
}
开发者_运维知识库

But, when I tried to scroll the pageView, whose controller implements the UIScrollViewDelegate protocol, I got an error like:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL scrollViewDidScroll:]: unrecognized selector sent to instance 0x5f42a80

When I took out [pageViewController release];, this error went away. This is weird to me because window should retain ... OH GOD!!! duh... it retains the pageViewController's view, not the pageViewController.

I get it now why it's wrong to release pageViewController. Silly me... I think it's time for a break.


It looks like you figured this part out, but the answer to your question is no, UIWindow does not randomly remove its subview AFAIK. In this case, it's retaining pageViewController.view not pageViewController. So, you shouldn't release pageViewController nor should you release pageViewController.view because pageViewController.view will automatically get released when pageViewController is released. I don't see where you are releasing pageViewController. I recommend making it an ivar of the AppDelegate and then releasing it in the AppDelegate's dealloc method, as you've probably done with window. The dealloc method never gets called, so pageViewController (and window) will never explicitly get released anyway, but making pageViewController an ivar is better style IMHO. Either way, I'll bet they get cleaned up when the application terminates.

0

精彩评论

暂无评论...
验证码 换一张
取 消