简体   繁体   English

我不知道为什么这个表视图代码崩溃了

[英]i don't know why this table view code crashes

hello i'm studying iOS programming 你好,我正在学习iOS编程

i created a project, which is an empty application 我创建了一个项目,这是一个空的应用程序

and i created table view controller without xib file. 我创建了没有xib文件的表视图控制器。

and i inserted follow code in didFinishLaunchingWithOptions 我在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. 我没有在AppDelegate文件中写dealloc。

why is that?? 这是为什么??

i think i created table view controller with alloc 我想我用alloc创建了表视图控制器

so retain count is 1. 所以保留计数是1。

and add sub view to window and table view controller retain count is 2. 并向窗口和表视图控制器添加子视图保留计数为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.view as a subview of the window causes tvc.view to be retained but does not retain tvc itself. 很简单,添加tvc.view作为窗口的子视图会导致tvc.view保留但不保留tvc本身。 In essence, your TableViewController instance becomes invalid as soon as you call release on it. 实质上,只要在其上调用release ,您的TableViewController实例就会变为无效。 The app crashes when you scroll presumably because the TableViewController instance is configured as a delegate or datasource for a UITableView or UIScrollView or any other thing associated with tvc.view . 大概是因为TableViewController实例被配置为UITableViewUIScrollView的委托或数据源或与tvc.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: or pushViewController: and passing the TableViewController instance. 你真的应该调用presentModalViewController:pushViewController:并传递TableViewController实例。 This will cause the TableViewController to be retained until it is dismissed/popped, making it safe for you to call release as in your example code. 这将导致TableViewController被保留,直到它被解除/弹出,使您可以安全地调用release如示例代码中所示。

Or, since you are doing this setup manually as part of didFinishLaunchingWithOptions , you can also set window.rootViewController directly, though again that's not really recommended. 或者,由于您作为didFinishLaunchingWithOptions一部分手动进行此设置,您也可以直接设置window.rootViewController ,尽管不是真的不推荐。 XCode allows you to specify the app's default/root view controller and will automatically set it up for you when the app launches. XCode允许您指定应用程序的默认/根视图控制器,并在应用程序启动时自动为您设置。

Actually you have just added the view alone. 实际上你刚刚添加了视图。 So the view alone will be retained. 因此,仅保留观点。 The viewcontroller will be released. viewcontroller将被释放。 But the viewcontroller needs to be the datasource and delegate for the tableview. 但是viewcontroller需要是tableview的数据源和委托。 Since it has been deallocated, that datasource will not have any valid reference and so it crashes. 由于它已被解除分配,该数据源将没有任何有效的引用,因此它崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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