iphone - 当应用程序进入前台时如何呈现模态视图 Controller ?

标签 iphone objective-c ios methods presentmodalviewcontroller

当应用程序进入前台时,我正在尝试呈现模态视图 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/

相关文章:

ios - CGContext横竖线宽的区别

iPhone + UITableView + 放置图像作为分隔符

iphone - 关闭应用程序时,核心数据中保存的元素将被删除

ios - 一次限制一个 View 平移

ios - Xcode 不生成 dSYM 文件

iphone - 类(class)值(value)始终为-1

objective-c - 如何在操作方法中使用 cocoa 中的进度指示器

ios - 如何在应用程序处于前台时显示本地通知

ios - 单元格维度 rowHeight 大小

ios - 我在 Controller 内调用 startDeviceMotionUpdatesToQueue 时得到 OSSpinLockLock