gpt4 book ai didi

ios - UIAlertController 操作表在第二次调用时没有响应

转载 作者:行者123 更新时间:2023-11-29 02:17:46 24 4
gpt4 key购买 nike

我有类似以下代码的内容。操作表在第一次出现时运行 doSomething OK(在按钮 IBAction 中),但是当它第二次出现时,没有任何反应,操作表只是消失而没有调用 do something。有什么想法吗?

@implementation ...

- (void) setActions {
UIAlertAction *opt1 = [UIAlertAction actionWithTitle:@"Option 1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self doSomething:@"opt1"];}];

UIAlertAction *opt2 = [UIAlertAction actionWithTitle:@"Option 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self doSomething:@"opt2"];}];

UIAlertAction *opt3 = ...

self.opt1 = opt1;
self.opt2 = opt2;
self.opt3 = opt3;

- (void) showActionSheet {

...
UIAlertController *selectAS = [UIAlertController alertControllerWithTitle:@"Select Options"
message:@"msg" preferredStyle:UIAlertControllerStyleActionSheet];

if (xyz) {
[selectAS addAction:self.opt1];
[selectAS addAction:self.opt2];
}
else{
[selectAS addAction:self.opt1];
[selectAS addAction:self.opt3];
}
[self presentViewController:selectqAS
animated:YES completion:nil];
}

- (void) doSomething: (NSString *) opt{



....
}

最佳答案

很高兴我们帮助您启动并运行。我的猜测是您的方法在翻译中丢失了。您有相互交织的方法,这可能会导致混淆,特别是 self.opt1。根据我的评论,现在 iOS8 已经引入了 UIAlertController,它附带了完成处理程序,您应该相应地进行计划:如下所示:

-(IBAction)showActionSheet {
UIAlertController *selectAS = [UIAlertController alertControllerWithTitle:@"Select Options" message:@"msg" preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *opt1 = [UIAlertAction actionWithTitle:@"Option 1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//Don't have to call another method, just put your action 1 code here. This is the power of completion handlers creating a more structured outline
}];

UIAlertAction *opt2 = [UIAlertAction actionWithTitle:@"Option 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//Don't have to call another method, just put your action 2 code here. This is the power of completion handlers creating a more structured outline
}];

UIAlertAction *opt3 = ...

if (xyz) {
[selectAs addAction:opt1];
[selectAs addAction:opt2];
} else {
[selectAs addAction:opt1];
[selectAs addAction:opt3];
}

[self presentViewController:selectAs animated:YES completion:nil];

}

更加简洁,并且实际上将 UIAlertController 用于预期目的,不需要调用其他方法。

关于ios - UIAlertController 操作表在第二次调用时没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28555529/

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