我有一个警报视图,当我点击“是”按钮时,它应该生成另一个警报视图和一个Toast消息,但它没有发生。我无法弄明白。这是我的代码:
-(void)myMethod {
UIAlertView *saveAlert = [[UIAlertView alloc] initWithTitle:@"First Message"
message:@"My First message"
delegate:nil
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil];
saveAlert.tag=0;
[saveAlert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
}
这是我用来为不同的警报视图提供功能的方法。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag==0) {
if (buttonIndex == 0)
{
//Code for Cancel button
}
if (buttonIndex == 1)
{
//code for yes button
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"Successfully displayed First Message";
hud.margin = 10.f;
hud.yOffset = 150.f;
hud.removeFromSuperViewOnHide = YES;
[hud hide:YES afterDelay:3];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Second Message"
message:@"My second message"
delegate:nil
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes",nil];
alert.tag=1;
[alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
}
}
if (alertView.tag==1) {
if (buttonIndex == 0)
{
//Code for Cancel button
}
if (buttonIndex == 1)
{
//Code for yes Button
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"Succesfully displayed Second Message";
hud.margin = 10.f;
hud.yOffset = 150.f;
hud.removeFromSuperViewOnHide = YES;
[hud hide:YES afterDelay:3];
}
}
}
任何人都可以帮助找到问题。为什么在第一次警报中点击“是”按钮后我无法获得第二次警报?
答案 0 :(得分:2)
您指定nil作为警报视图的委托。您需要指定一个对象,以便可以调用alertView:clickedButtonAtIndex:方法!
答案 1 :(得分:2)
您尚未为UIAlertView
设置委托,并确保您的委托符合UIAlertViewDelegate
协议。找到下面的代码段。
您的控制器符合UIAlertViewDelegate
协议:
@interface YourViewController : UIViewController <UIAlertViewDelegate>
创建UIAlertView
并设置deleagte:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"First Message"
message:@"Show second message"
delegate:self
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil];
[alertView show];
实施UIAlertViewDelegate
委托方法:
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if( 0 == buttonIndex ){ //cancel button
[alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
} else if ( 1 == buttonIndex ){
[alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
UIAlertView * secondAlertView = [[UIAlertView alloc] initWithTitle:@"Second Message"
message:@"Displaying second message"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[secondAlertView show];
}
}
答案 2 :(得分:0)
如果您想更清楚地处理此问题,可以使用基于块的AlertView。 创建新文件 - &gt;子类 - &gt; UIAlertView
SuperAlertView.h
#import <UIKit/UIKit.h>
@class MySuperAlertView;
typedef void (^MySuperAlertViewBlock) (MySuperAlertView *alertView);
@interface MySuperAlertView : UIAlertView
- (instancetype) initWithTitle:(NSString *)title message:(NSString *)message;
- (void) addButtonWithTitle:(NSString *)buttonTitle block:(MySuperAlertViewBlock) block;
@end
SuperAlertView.m
#import "MySuperAlertView.h"
@interface MySuperAlertView()<UIAlertViewDelegate>
@property NSMutableArray *blocks;
@end
@implementation MySuperAlertView
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message
{
if (self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:nil])
{
self.blocks = [NSMutableArray array];
}
return self;
}
- (void)addButtonWithTitle:(NSString *)buttonTitle block:(MySuperAlertViewBlock)block
{
[self addButtonWithTitle:buttonTitle];
[self.blocks addObject:block ? [block copy] : [NSNull null]];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
MySuperAlertViewBlock block = self.blocks[buttonIndex];
if ((id) block != [NSNull null]){
block(self);
}
}
@end
用法:
MySuperAlertView *alertView = [[MySuperAlertView alloc] initWithTitle:@"Info" message:NSLocalizedString(@"EMAIL_SENT_SUCCESSFULL", nil)];
[alertView addButtonWithTitle:@"Ok" block:^(MySupertAlertView *alertView) {
// handle result from ok button here
}];
[alertView addButtonWithTitle:@"cancel" block:NULL];
dispatch_async(dispatch_get_main_queue(), ^{
[alertView show];
});