2012-09-12 74 views
6

我想重新加載包含在tabbar控制器(UIViewController)中的所有視圖。搜索後,我發現我必須應用setNeedsDisplay方法,但我無法獲得應該在哪裏應用它。所有其他替代方法也歡迎如何重新加載UIViewController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    ..... 
    ..... 

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
    [self customToolbar]; 
    [self.window addSubview:tabBarController.view]; 
    [self.window makeKeyAndVisible];  
    return YES; 
} 
-(void)customToolbar 
{ 
    //Declared view controllers and their Navigation Controller 
    ..... 

    //Declared tab bar items 
    .....  

    tabBarController = [[GTabBar alloc] initWithTabViewControllers:viewControllersArray tabItems:tabItemsArray initialTab:1]; 
} 
+0

當你想重新加載/刷新你的VC? – Maulik

+0

我在另一個NSObject類中調用應用程序委託,我在其中放置了一些按鈕動作。當按鈕被按下時,我想重新加載VC的 –

回答

4

正確的方法是將需要作爲觀察者刷新的任何VC添加到某個NSNotificationCenter通知名稱。一旦VC得到這個消息,就調用一個叫做[self setNeedsDisplay]的選擇器。

要將VC添加到NSNotificationCenter:

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

不要忘記調用removeObserver:self當視圖控制器被釋放。

+0

嗨Stavash ..我可以放這個方法嗎?我的意思是「ViewWillAppear」?當我切換tabbar時,我打電話查看購物車web服務。我正在使用UIViewController類。 –

+1

ViewWillAppear在每一次視圖即將被顯示時都會被調用 - 這不僅僅是當您在標籤欄之間切換時,還有當您從導航堆棧中彈出或彈出控制器,或者出現並消除模態視圖時。如果這符合你的需求,那麼是的,否則你需要考慮其他事情 – Stavash