私がやろうとしているアプリには、タブバーコントローラーがあります。
アプリが起動すると、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 に追加しました。ここで何か間違ったことをしていますか?