我认为最好按顺序写出正在发生的事情:
1)object_1
创建object_2
如下:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
Object_2 *object_2 = [[Object_2 alloc] init];
[object_2 show]
}
2)object_2
创建一个UIActionSheet
, set 本身作为委托并显示它:
- (void) show{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"title"
delegate:self
cancelButtonTitle:@"cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet showInView:[UIApplication sharedApplication].delegate.window.rootViewController.view];
}
3) 用户与action sheet进行交互,向delegate发送消息,delegate已被释放(by ARC)。所以应用程序崩溃了。
如何“保留”它,以便在调用委托方法时它存在,并在用户完成操作表(和委托方法)时“释放”它?
编辑
因为我对内存管理很神经质...
经过一些研究,我最终在object2中这样做了:
(如果你不记得,guenis 的答案是完全正确的)
@property (strong) MoreOptionsListController *selfPointerWhileUserInteractsWithTheMenu;
然后
- (void) show {
_selfPointerWhileUserInteractsWithTheMenu = self;
...
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
_selfPointerWhileUserInteractsWithTheMenu = nil;
...
}