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];
    }

}
}

然后,在我的第一个视图控制器中,我有:

- (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];
        }
    }
}
}

第二次编辑:添加更多代码。

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 制作的,但我在 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.tabBarController 而不是 self 来呈现。再次只是建议,但不确定它是否有效。

于 2012-11-06T10:05:50.970 回答
2

使用它可以帮助某人:[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:picker animated:NO completion:nil];

于 2013-07-22T10:43:38.290 回答
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 回答