2

特定の状況が発生したかどうかに応じて異なるメッセージを表示するalertViewを表示したいアプリケーションを作成しています。いずれの状況も一致しない場合は、アラートを表示せず、残りのアプリケーションを処理する必要があります。私の問題は、これを行う方法がわからないことです。私は次のコードを持っています:

 - (void) methodThatIsCalled {

             NSString *msg;

             if (blah) {

                 msg = @"Message A";

             }

             else if (blah blah) {

                 msg = @"Message B";

             }

             else if (blah blah blah) {

                 msg = @"Message C";

             }

             //Here is where I want to display the Alert code
             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:msg
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
             [alert show];

             else {

                   Do rest of the application....


             }

      }

アラートを表示するコードのブロックが1つだけで、メッセージ文字列をアラートに動的に渡し、どの句も一致しない場合は何もしないように、これを行う方法を誰かに教えてもらえますか?

返信してくださった皆様、ありがとうございました。

4

2 に答える 2

4
- (void) showAlertWithTitle: (NSString*) title message: (NSString*) message
{
    UIAlertView* alert= [[[UIAlertView alloc] initWithTitle: title message: message
                                                   delegate: NULL cancelButtonTitle: @"OK" otherButtonTitles: NULL] autorelease];
    [alert show];

}

//関数内:

if (blah) {
    [self showAlertWithTitle:@"Error"  message:@"Message A"];
} 
 else if (blah blah) {
    [self showAlertWithTitle:@"Error"  message:@"Message B"];
}
于 2013-02-24T05:03:05.197 に答える
3

あなたはに変更することができます

NSString *msg = nil;

次にifを追加します

if (msg) { // If there is a message
    //Here is where I want to display the Alert code
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                              message:msg
                                              delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
    [alert show];
} else {
    ... // Rest of application
}
于 2013-02-24T04:56:04.477 に答える