UIAlert和push segues

时间:2014-04-30 08:49:51

标签: ios objective-c

我有一个IF语句,用于检查标签是否为空,如果标签显示警告。

  if ([_DOBDate.text length] == 0) 
    {

    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Cannot Proceed"
                                                      message:@"Please Submit your DOB and Gender"
                                                     delegate:nil
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];

    [message show];
    }

稍后在我执行segue的函数中,像这样:

[self performSegueWithIdentifier:@"SetUptoMain" sender:self];

是的,当标签为空时,警报会触发,但它也会执行segue。执行segue行不在IF语句中。所以我原本以为它会运行IF语句并一直呆在那里直到我按下OK。好的就是停留在同一个视图控制器上。

执行segue,这是由于Block?任何建议?

因此,如果USER从UIAlert按下Ok,则VC不会移动,它会保持原样,因此用户可以输入所需的详细信息。

这是我的代码:

- (IBAction)SettingsSave:(id)sender {

    if ([_DOBDate.text length] == 0) 
    {

    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Cannot Proceed"
                                                      message:@"Please Submit your DOB and Gender"
                                                     delegate:self
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];

    [message show];
    }

更多代码...... 然后在最后

[self performSegueWithIdentifier:@"SetUptoMain" sender:self];

}

谢谢

2 个答案:

答案 0 :(得分:0)

UIAlertView不会阻止执行。它以异步运行。因此,代码路径将进入您的if statement,然后继续经过它。

如果您只想在用户按提醒视图按钮后执行 segue ,则需要实施UIAlertViewDelegate

在你的头文件中添加如下内容:

@interface MyController : UIViewController <UIAlertViewDelegate>

创建提醒视图时,请执行以下操作:

UIAlertView *message = [[UIAlertView alloc] initWithTitle: @"Cannot Proceed"
                                                      message: @"Please Submit your DOB and Gender"
                                                     delegate: self
                                            cancelButtonTitle: @"OK"
                                            otherButtonTitles: nil];

[message show];

并添加此方法以实现UIAlertViewDelegate

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

    // perform segue here.  You can also check what button was pressed based on the button index.
}

答案 1 :(得分:0)

似乎是if/else逻辑问题。了解它。

-(IBAction)yourAction:(id)sender
{
    if ([_DOBDate.text length] == 0)
    {
       //Show your AlertView as you did
    }
    else
    {
        //put the rest of your code, including the performSegue
    }
}