UITableViewCell 中的 UIButton 没有释放它创建的 UIViewController
Posted
技术标签:
【中文标题】UITableViewCell 中的 UIButton 没有释放它创建的 UIViewController【英文标题】:UIButton in UITableViewCell not releasing the UIViewController it creates 【发布时间】:2013-03-07 11:23:45 【问题描述】:在我的UITableViewCell
子类中,里面有一个UIButton
,应该推送一个新创建的UIViewController
。
我做了两个这样的方法:
- (void)configureCellWithObject:(Object *)object
self.object = object;
[self.button1 addTarget:self action:@selector(initCustomController) forControlEvents:UIControlEventTouchUpInside];
// self.button1 is a UIButton
- (void)initCustomController
CustomViewController *cvc = [[CustomViewController alloc]initWithObject:self.object];
// self.nc is the navigation controller of the UITableView presenting the cell
[self.nc pushViewController:cvc animated:YES];
我的问题是,当我触摸左上角的“back
”按钮时,创建的CustomViewController
永远不会释放(它消失但没有释放)。
如果我在 tableView:didSelectRowAtIndexPath:
中完成所有这些操作,它会按我的预期工作并得到适当的释放。
虽然我做的一切都是正确的,但不知道如何使它正确:(
编辑:是的,我正在使用ARC
【问题讨论】:
是的,忘了说谢谢 释放 customviewcontroller 的负担留给了编译器。所以它负责何时释放 customviewcontroller。 我确定我犯了一个错误,因为它永远不会被释放ever
你怎么知道它永远不会被释放?你是说 viewcontroller 中的后退按钮没有将你返回到 tableview 吗?
从这里查看我的答案***.com/questions/19000356/…你不需要标签
【参考方案1】:
您可以创建自定义按钮,并在 tableviewCellForRowAtIndexPath 中为每个按钮提供不同的标签,例如在需要时传递选定的值,否则不需要。
cell.button.tag= indexPath.row + 1;
你可以这样写你的按钮方法
[cell.button addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
在 btnClicked 方法中只需编写导航代码。
希望对你有用
【讨论】:
谢谢,但我知道而且我不太喜欢标签,目标是if possible
让它在单元格的方法中工作
您可以像给定的答案一样将目标添加到按钮。在这种方法中,只需编写您的导航代码。如果您不想分配标签,那没关系,因为您只想导航。对吧?
别担心我明白你的意思我只是不想使用tags
:p 是的,只是导航,但每一行都会创建一个不同的控制器。如果我没有找到任何答案,我会将您的答案标记为有效,但我仍在等待另一个答案;)以上是关于UITableViewCell 中的 UIButton 没有释放它创建的 UIViewController的主要内容,如果未能解决你的问题,请参考以下文章
更改 UITableViewCell 中的对象也更改了重用 UITableViewCell 的对象
确定 UITableViewCell 中的 UIButton
UITableViewCell 事件中的 UIButton 不起作用