2

私のiPhoneアプリには、VC1とVC2という2つのビューがあります。VC1には、VC2に移動するためのボタンがあります。VC1のボタンアクションのコードは次のとおりです。

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (!appDelegate.vc2) {
    appDelegate.vc2 = [[VC2 alloc]initWithNibName:@"VC2" bundle:nil];
}
[self presentModalViewController:appDelegate.vc2 animated:NO];

(VC2でいくつかのステータスを保持する必要があるため、appdelegateファイルでVC2を宣言しました)

そしてVC2では、戻るために私は使用しました[self dismissModalViewControllerAnimated:NO];

VC2に移動し、ボタンアクションを使用して戻るプロセスを繰り返しても、問題はありません。ただし、アプリにプッシュ通知も実装しました。したがって、プッシュを取得すると、プッシュ通知アラートビューの[OK]ボタンをクリックして通知を送信します。VC1に通知受信メソッドが書き込まれると、イベントが受信され、VC2に移動します。

VC2に移動するための受信通知方法で上記と同じコードを使用しました。

アラートビューの[OK]ボタンをクリックしてVC2に移動しようとすると、エラーが発生Application tried to present modally an active controllerします。このエラーはiPhone 4S(iO5)でのみ発生します。しかし、VC1のボタンアクションを使用してそれを繰り返すと、正常に動作します。

編集 追加コードの詳細:

appdelegateファイルで、プッシュ通知アラートビューの[OK]ボタンのアクション:

[[NSNotificationCenter defaultCenter] postNotificationName:@"VC1" object:nil];

そして、通知を受信するときのVC1では:

- (void)recieveNotification:(NSNotification *) notification{
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        if (!appDelegate.menuVC) {
            appDelegate.menuVC = [[MenuScreenVC alloc]initWithNibName:@"MenuScreenVC" bundle:nil];
        }
        [self presentModalViewController:appDelegate.menuVC animated:NO];
}

VC1のボタンのボタンアクション:

- (IBAction)viewMenuScreen:(id)sender{

            AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            if (!appDelegate.menuVC) {
                appDelegate.menuVC = [[MenuScreenVC alloc]initWithNibName:@"MenuScreenVC" bundle:nil];
            }
            [self presentModalViewController:appDelegate.menuVC animated:NO];
}

およびVC2の場合:

- (IBAction)backToVC1:(id)sender{
    [self dismissModalViewControllerAnimated:NO];
}
4

0 に答える 0