1

我正在为 ios 构建一个应用程序,我有一个导航栏按钮,将向用户显示一个带有 2 个按钮的 uialertview离开他会来到以前的视图控制器

这是.m文件的代码

-(IBAction)done:(id)sender{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"Are You Sure??" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];

    [alert show];

}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if ([title isEqualToString:@"YES"]) {

        segueShouldOccur = YES;

    }

}

- (BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{

 if ([identifier isEqualToString:@"perform"]) {

 if (!segueShouldOccur) {

     return NO;

    }

 }

    return YES;

 }

当我编译此代码时,它会执行警报视图显示,但即​​使我按下是或否,它也不会停留在同一页面上。请帮忙。

4

2 回答 2

0

试试这种方式:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"YES"]){
    //YES is pressed , perform segue!
    [self performSegueWithIdentifier:@"perform" sender:self];   
    }  
}

编辑,您必须将代表设置为 self 而不是 nil

-(IBAction)done:(id)sender{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" 
                                                    message:@"Are You Sure??" 
                                                   delegate:self //set the delegate !
                                          cancelButtonTitle:@"NO" 
                                          otherButtonTitles:@"YES", nil];

    [alert show];
}
于 2013-11-07T21:25:16.977 回答
0

您实际上并没有调用segue“发生” - 只是设置一个 Bool 以允许它发生。(您应该调用 performSegueWithIdentifier: sender :)

于 2013-11-07T21:25:30.560 回答