当应用程序进入前台时,我正在尝试呈现模态视图 Controller 。这些是我的文件:
AppDelegate.m:
#import "AppDelegate.h"
#import "MainViewController.h"
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[self.window makeKeyAndVisible];
MainViewController * vc = [[MainViewController alloc]init];
[vc myMethodHere];
}
MainViewController.h :
//[..]
-(void) myMethodHere;
主视图 Controller .m:
-(void)myMethodHere{
NSLog(@"myMethodHere Activated.");
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
[self presentModalViewController:tweetViewController animated:YES];
}
NSLog(@"myMethodHere Activated.") 有效..所以我不明白为什么“presentModalViewController”没有!我应该编辑/添加什么?也许是延迟?谢谢你的帮助..
p.s.我知道我的英语很烂..原谅我:)
最佳答案
我不会为此依赖您的应用程序委托(delegate)中的方法(即使它似乎是显而易见的解决方案),因为它会在您的应用程序委托(delegate)和 View Controller 之间创建不必要的耦合。相反,您可以使用 MainViewController
收听UIApplicationDidBecomeActive
通知,并呈现推文 Composer View Controller 以响应此通知。
首先,在-viewDidLoad
中注册通知.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethodHere) name:UIApplicationDidBecomeActiveNotification object:nil];
}
现在,当您的应用从后台返回时收到此通知时,
myMethodHere
将被调用。最后,请记住在 View 卸载时将自己移除为观察者。
- (void)viewDidUnload
{
[super viewDidUnload];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}
关于iphone - 当应用程序进入前台时如何呈现模态视图 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8452289/