在Segue Creation中需要帮助

时间:2013-11-07 21:20:17

标签: objective-c ios7 segue

我正在为ios构建应用程序,我有一个导航栏按钮,将向用户显示uialertview,其中2个按钮用于取消,第二个用于执行segue,当用户按下取消时他将保持在同一页面但是当他按下时你想离开他会来到以前的viewcontroller

这里是.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;

 }

当我编译此代码时,它会执行警报视图显示,但是即使我按下yess或者没有它保持在同一页面上也没有任何内容。请帮忙。

2 个答案:

答案 0 :(得分: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而不是

-(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];
}

答案 1 :(得分:0)

你实际上没有将segue称为'发生' - 只是设置一个Bool来允许它发生。 (你应该调用performSegueWithIdentifier:sender:)

相关问题