[英]UIActionsheet delegate method calling twice in ios8?
在ios8的UIActionSheet委托方法中多次调用
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
我已经在iOS 8的ipad 2中签入过
这是iOS 8.的错误。
作为@rob所述UIActionSheet
是在IOS 8.弃用(UIActionSheetDelegate也弃用。)
要在iOS 8及更高版本中创建和管理操作表,请使用UIAlertController
实际上,这是我在多个地方看到的当前行为。 它的确似乎是一个错误,希望很快会得到修复,但无法确定。
您应该使用UIAlertController替换iOS8中的所有AlertView和ActionSheet。 如果目标低于iOS8,则应检查版本并添加更多代码
例如,此代码适用于iOS8 ActionSheet
-(void)testActionSheet{
UIAlertController* alertAS = [UIAlertController alertControllerWithTitle:@"Test ActionSheet"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Action" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
NSLog(@"Action");
}];
[alertAS addAction:defaultAction];
UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];
[alertAS addAction:cancleAction];
[self presentViewController:alertAS animated:YES completion:nil];
}
如果您想继续使用现有的API,则有一些行为可以让您知道何时运行代码以及何时忽略委托调用(即buttonIndex
。
首次调用委托方法时,它们始终会传递正确的buttonIndex
。 但是,第二次使用取消按钮的buttonIndex
调用它们。
不幸的是,iOS 8中不推荐使用UIActionSheet: https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UIActionSheet_Class/
我遇到过同样的问题。 一种解决方法:使用actionSheet.tag
。 在实例化时将其设置为有效数字(默认情况下为0),例如1、2,...。 处理响应:
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
检查它是否有效(例如:在处理它之前不为-1)。 在这里处理完响应后,返回之前,请设置:
actionSheet.tag = -1;
这样可以确保您将忽略它,尽管进行了第二次调用。 这对我来说有效。
我想补充一下:UIAlertController是现在在iOS8.0 +中使用UIAlertViews的正确方法(不推荐使用UIAlertView),但是能够选择多个选项的错误有所缓解。
可以同时选择/突出显示警报视图中的单独选项,但是委托方法似乎只激发了其中一个。 哪一个实际触发是不确定的,但是我可以确认,即使使用多根手指突出显示了两个或多个,也可以触发一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.