我正在生成一个UIAlertController,用于在iOS8中替换UIActionSheet函数。
控制器正确显示,但我无法点击控制器中的任何位置来获取响应。我把简单的NSLog放入处理程序中,绝对不会触发。
有人能解释一下这个问题吗?
BTW,在iOS8中,UIActionSheet也没有生成点击事件,但在iOS7中,这完全没有代码更改。这是代码:
NSString *alertMessage = @"How do you wish to share ?";
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Share Image"
message:alertMessage
preferredStyle:UIAlertControllerStyleAlert];
// You can add as many actions as you want
UIAlertAction *emailShare = [UIAlertAction actionWithTitle:@"Share using email"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(@"EMAIL");
}];
UIAlertAction *smShare = [UIAlertAction actionWithTitle:@"Share using twitter/facebook"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self actionSheet:nil didDismissWithButtonIndex:1];
}];
UIAlertAction *cancelShare = [UIAlertAction actionWithTitle:@"cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self actionSheet:nil didDismissWithButtonIndex:2];
}];
// Add actions to the controller so they will appear
[alertController addAction:emailShare];
[alertController addAction:smShare];
[alertController addAction:cancelShare];
// Finally present the action
[self presentViewController:alertController animated:YES completion:nil];
答案 0 :(得分:0)
您的代码存在一些不同的问题。我评论了必要的变化。如果您有任何问题,请告诉我。
// No need to create a NSString. Just set the message.
// NSString *alertMessage = @"How do you wish to share?";
// If you'd like a UIActionSheet use UIAlertControllerStyleActionSheet as the preferredStyle
// Opposed to UIAlertControllerStyleAlert, which presents a UIAlertView
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Share Image"
message:@"How do you wish to share?" // Message Set
preferredStyle:UIAlertControllerStyleActionSheet]; // Changed to show action sheet
UIAlertAction *emailShare = [UIAlertAction actionWithTitle:@"Share using email"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[alertController dismissViewControllerAnimated:YES completion:nil];
// Call your function here
// ie. [self emailShare];
NSLog(@"First Button");
}];
UIAlertAction *smShare = [UIAlertAction actionWithTitle:@"Share using twitter/facebook"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
// [self actionSheet:nil didDismissWithButtonIndex:1];
// Where did you create actionSheet?
// didDismissWithButtonIndex: is not neccessary
// Just simply dismiss the UIAlertController
[alertController dismissViewControllerAnimated:YES completion:nil];
// And then call your function here
// ie. [self twitterShare];
NSLog(@"Second Button");
}];
UIAlertAction *cancelShare = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel // Set style to cancel
handler:^(UIAlertAction *action) {
// [self actionSheet:nil didDismissWithButtonIndex:2];
// Where did you create actionSheet?
// didDismissWithButtonIndex: is not neccessary
// Just simply dismiss the UIAlertController
[alertController dismissViewControllerAnimated:YES completion:nil];
NSLog(@"Cancel Button");
}];
[alertController addAction:emailShare];
[alertController addAction:smShare];
[alertController addAction:cancelShare];
[self presentViewController:alertController animated:YES completion:nil];