2012-04-26 37 views
2

是否可以檢查哪個UIViewController場景在代碼中處於活動狀態?如何檢查哪個UIViewController從AppDelegate處於活動狀態(推後)

我有一個推送通知進來的應用程序,並希望這取決於查看用戶不同的事情要做的是,這樣的事情:

的AppDelegate:

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo 
{ 
    if (tableViewController == active) 
     //get data from server 
    else if (detailedViewController == active) 
     //Get Image from server 
} 

感謝

回答

3

發佈本地通知:

[NSNotificationCenter defaultCenter] postNotificationName:@"foo" object:whatever]; 

,擁有所有適當的viewControllers注意它:

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(handleFoo:) 
              name:@"foo" 
              object:nil]; 

viewControllers將添加或刪除觀察員作爲必要條件。

+0

太棒了!非常感謝 – BlackMouse 2012-04-26 12:31:13

2
if (_viewController.isViewLoaded == YES) 
{ 
    NSLog(@"Yes"); 

} 
else 
{ 
    NSLog(@"No"); 
} 

希望,這將幫助你......

1

嗯,我猜你使用了一些contaneir視圖控制器。如果您使用的是UITabBarController你可以只問自己爲它:

@property(nonatomic, assign) UIViewController *selectedViewController 

或其:

@property(nonatomic) NSUInteger selectedIndex 

如果您使用的是UINavigationController

@property(nonatomic, readonly, retain) UIViewController *visibleViewController 

如果你喜歡的罰款對容器的粒度控制可以使Appdelegate符合相應的代理協議:UITabbarControllerDelegateUINavigationControllerDelegate

相關問題