3

私がやろうとしているアプリには、タブバーコントローラーがあります。

アプリが起動すると、AppDelegate でユーザーの場所を取得し、必要な精度が得られると、AppDelegate は NSNotification をアプリの開始ページ (タブ バー コントローラーのインデックス 0) に送信します。

通知を受信すると、このビューはユーザー座標とその他のデータを含む電子メールを送信しようとしますが、MFMailComposeViewController が表示されるとすぐに次のエラーが発生します。

Warning: Attempt to present <MFMailComposeViewController: 0x98a0270> on <UITabBarController: 0x988c630> whose view is not in the window hierarchy!

私は何が欠けていますか?

ありがとう。

編集:いくつかのコードを追加しています...

これは、AppDelegate.m にあるものです。

- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSUserDefaults *phoneNumbers = [NSUserDefaults standardUserDefaults];
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 10.0) {
    [self locationUpdate:newLocation];
    smsLoc = newLocation;
    if ([[phoneNumbers objectForKey:@"sendSMS"] isEqualToString:@"yes"]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"sendSMS" object:nil];
    } else if ([[phoneNumbers objectForKey:@"sendEmail"] isEqualToString:@"yes"]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"sendEmail" object:nil];
    }

}
}

次に、最初のView Controllerには次のものがあります。

- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendSMS:) name:@"sendSMS" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendEmail:) name:@"sendEmail" object:nil];
}

そして最後に、「sendSMS」のセレクター (もう一方はかなり似ています):

- (void)sendSMS: (NSNotification *)notification {
NSUserDefaults *phoneNumbers = [NSUserDefaults standardUserDefaults];
if ([phoneNumbers objectForKey:@"first"] || [phoneNumbers objectForKey:@"second"]) {
        MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
        if ([MFMessageComposeViewController canSendText]) {
            AppDelegate *deleg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            controller.body = [NSString stringWithFormat:@"some message with coordinates %.4f - %.4f", [deleg currentLocation].coordinate.latitude, [deleg currentLocation].coordinate.longitude];
            controller.recipients = [NSArray arrayWithObjects:[phoneNumbers objectForKey:@"first"], [phoneNumbers objectForKey:@"second"], nil];
            controller.messageComposeDelegate = self;
            [self presentModalViewController:controller animated:YES];
        }
    }
}
}

2 番目の編集: コードをさらに追加します。

UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;
tabBarController.selectedIndex = 0;
[[tabBarController.tabBar.items objectAtIndex:0] setTitle:NSLocalizedString(@"Home", nil)];
[[tabBarController.tabBar.items objectAtIndex:1] setTitle:NSLocalizedString(@"Requests", nil)];
[[tabBarController.tabBar.items objectAtIndex:2] setTitle:NSLocalizedString(@"Account", nil)];
[[tabBarController.tabBar.items objectAtIndex:3] setTitle:NSLocalizedString(@"Settings", nil)];
//some other controls from DB
[[tabBarController.tabBar.items objectAtIndex:1] setBadgeValue:[NSString stringWithFormat:@"%d",number]];

tabbarController は IB 経由で作成されていますが、タブ バー アイテムをローカライズし、そのうちの 1 つにバッジを追加する必要があるため、上記のコードを AppDelegate に追加しました。ここで何か間違ったことをしていますか?

4

4 に答える 4

10

この問題を解決したかどうかはわかりません。エラーメッセージは、別のモーダルビューコントローラを表示するために使用するビューコントローラがウィンドウに表示されないことを意味します。これは、たとえば次の場合に発生する可能性があります。

[VC1 presentModalViewController:VC2];

// Error here, since VC1 is no longer visible on the window
[VC1 presentModalViewController:VC3]; 

問題が上記のような場合は、次のように修正できます。

if (self.modalViewController != nil) {
    [self.modalViewController presentModalViewController:VC3 animated:YES];
} else {
    [self.tabBarController presentModalViewController:VC3 animated:YES];
}

それでも問題が解決しない場合は、selfの代わりにself.tabBarControllerを使用してプレゼンテーションを試みることができます。繰り返しになりますが、それが機能するかどうかはわかりません。

于 2012-11-06T10:05:50.970 に答える
1

modalViewController と presentModalViewController は非推奨になっているため、次のように動作します。

presentingVC = [[UIApplication sharedApplication] keyWindow].rootViewController;
if (presentingVC.presentedViewController) {
    [presentingVC.presentedViewController presentViewController:VC3 animated:YES completion:nil];
} else {
    [presentingVC presentViewController:VC3 animated:YES completion:nil];
}
于 2014-03-18T14:17:04.870 に答える
0

このパターンに従うことができます

[VC1 presentModalViewController:VC2];

//
[**VC2** presentModalViewController:VC3]; 
于 2013-05-17T09:22:31.143 に答える