hello i’m studying iOS programming
i created a project, which is an empty application
and i created table view controller without xib file.
and i inserted follow code in didFinishLaunchingWithOptions
TableViewController *tvc = [[TableViewController alloc] init];
[self.window addSubView:tvc.view];
[tvc release];
this code was crashed when i scrolled down. why is that?
when i comment this code
[tvc release];
program doesn’t crash.
i didn’t write dealloc in AppDelegate file.
why is that??
i think i created table view controller with alloc
so retain count is 1.
and add sub view to window and table view controller retain count is 2.
so i release table view controller
but it crash when i scrolled down.
i don’t know why..
help me please
Simple, adding
tvc.viewas a subview of the window causestvc.viewto be retained but does not retaintvcitself. In essence, yourTableViewControllerinstance becomes invalid as soon as you callreleaseon it. The app crashes when you scroll presumably because theTableViewControllerinstance is configured as a delegate or datasource for aUITableVieworUIScrollViewor any other thing associated withtvc.view.Also note that the way you are displaying the view is not the recommended way to go about it. Really you should be calling
presentModalViewController:orpushViewController:and passing theTableViewControllerinstance. This will cause theTableViewControllerto be retained until it is dismissed/popped, making it safe for you to callreleaseas in your example code.Or, since you are doing this setup manually as part of
didFinishLaunchingWithOptions, you can also setwindow.rootViewControllerdirectly, though again that’s not really recommended. XCode allows you to specify the app’s default/root view controller and will automatically set it up for you when the app launches.