UIActionSheet的委托在用户输入之前发布

时间:2013-04-23 01:43:39

标签: ios objective-c uiactionsheet

我认为如果按顺序写下发生的事情,最好解释一下:

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,将其本身设为委托并显示:

- (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)用户与操作表交互,并向委托发送消息,该委托已被释放(通过ARC)。应用程序崩溃了。

如何“保留”它,因此在调用委托方法时存在,并在用户完成操作表(以及委托方法)时“释放”它?

修改

因为我对记忆管理感到神经质......

经过一些研究后,我最终在object2中做了这个:

(如果你不是那种记忆,guenis的回答是完全正确的)

@property (strong) MoreOptionsListController *selfPointerWhileUserInteractsWithTheMenu;

然后

- (void) show {
    _selfPointerWhileUserInteractsWithTheMenu = self;
    ...
}   

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    _selfPointerWhileUserInteractsWithTheMenu = nil;
    ...
}

1 个答案:

答案 0 :(得分: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/