0

我认为最好按顺序写出正在发生的事情:

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;
    ...
}
4

1 回答 1

1

这应该有效:

//Object1.h 
//Add a property in header
@property (nonatomic, strong) Object2 *actionSheetDelegate;

//Object1.m
//Use the property instead to retain the object
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
      self.actionSheetDelegate = [[Object_2 alloc] init];
      [self.actionSheetDelegate show]
}

编辑:

还有一种替代方法,我在 uialertviews 中使用了很多。它是用块来处理委托方法。从某种意义上说,操作表非常接近警报视图,因此您可以使用本教程中的类似内容:

http://www.cocoanetics.com/2012/06/block-based-action-sheet/

于 2013-04-23T02:04:37.470 回答