0

簡単な質問があります。本当に困っているので、助けてください。ログインとパスワードが空でない場合、ログインボタンが押された後に別のViewControllerに切り替える必要があります

ユーザー名、パスワード、ログインボタンを備えたシンプルなログインインターフェイスがあります。パスワードまたはログインが空の場合、UIAlertView が表示されます。コードは次のとおりです。

.m ファイル

- (IBAction)login:(id)sender {
    if (userName.text.length == 0 || password.text.length == 0){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Complete All Fields" message:@"You must complete all fields to login!" delegate:self cancelButtonTitle:@"Good" otherButtonTitles:nil, nil];
        [alert show]; 
    }
}

ボタンのアクションを追加して他のUIControllerに切り替える方法は? これは常にクラッシュしています[self presentModalViewController: anotherUIViewController animated YES];ここにも通知があります-「アプリケーションはターゲットに nil modal View Controller を提示しようとしました。」

4

1 に答える 1

2

xib またはストーリーボードを使用しているかどうかに応じて、以下で説明するように行の 1 つだけを保持します。

- (IBAction)login:(id)sender {
    if (userName.text.length == 0 || password.text.length == 0){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Complete All Fields" message:@"You must complete all fields to login!" delegate:self cancelButtonTitle:@"Good" otherButtonTitles:nil, nil];
        [alert show];
    }else{
        //if you are using xibs use this line
        UIViewController *anotherUIViewController = [[UIViewController alloc] initWithNibName:@"anotherUIViewController" bundle:[NSBundle mainBundle]];
        //
        //if you are using storyboards use this line
        UIViewController *anotherUIViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"someIdentifierForThisController"];
        //
        //then simply present the view controller
        [self presentViewController:anotherUIViewController animated:YES completion:nil];
    }
}
于 2012-08-26T05:45:44.457 に答える