UiAlertView应该更改ViewController

时间:2015-06-01 18:03:57

标签: ios objective-c uiviewcontroller uialertview

以下代码有什么问题?当alertView中的文本不为空时,我将更改viewController。问题是,每次都会改变UiViewController,即使它是空的。

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

    if (buttonIndex == 0) {
        if ([[alertView textFieldAtIndex:0].text isEqual:@""]) {

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error warning" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

            [alert show];
        }
        else{

            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ChangeView"];
            [self presentViewController:vc animated:YES completion:nil];
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您应该执行此检查:

if (![[alertView textFieldAtIndex:0].text.length) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error warning" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
} else ...