gpt4 book ai didi

ios - UIActionSheet 的委托(delegate)在用户输入之前释放

转载 作者:行者123 更新时间:2023-11-29 03:58:47 25 4
gpt4 key购买 nike

我认为如果我按顺序写出发生的事情是最好的解释:

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,将其自身设置为委托(delegate)并显示它:

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

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

编辑

因为我对内存管理很神经质......

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

(如果你记不太清楚,guenis 的答案是完全正确的)

@property (strong) MoreOptionsListController *selfPointerWhileUserInteractsWithTheMenu;

然后

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

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

最佳答案

这应该有效:

//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 中经常使用的替代方法。它是用 block 来处理委托(delegate)方法。从某种意义上说,操作表与警报 View 非常接近,因此您可以使用本教程中类似的内容:

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

关于ios - UIActionSheet 的委托(delegate)在用户输入之前释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16159517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com