2016-07-06 61 views
0

我使用Objective-C開發iOS tabBar應用程序。當用戶打開應用程序時,它在tab1中。當用戶點擊tab1中的圖片時,該應用程序將跳轉到帶有圖片名稱的tab3。iOS中添加觀察器的位置

問題是在tab3中調用addObserver的地方,因爲tab3從不打開?

+1

你用'的addObserver意味着什麼方法目前還不清楚'(至少有兩組不同的方法和這個名稱)或爲什麼你想這樣做。一般來說,如果視圖控制器在屏幕上不需要觀察時,你可能違反了MVC。 https://developer.apple.com/library/mac/documentation/General/Conceptual/CocoaEncyclopedia/Model-View-Controller/Model-View-Controller.html如果你只是需要觀察你在屏幕上,使用'viewDidAppear '和'viewWillDisappear'。 –

回答

0

如果我正確地得到你的問題,你只想當您單擊從選項卡中的照片1

IMO打開TAB3,它應該有類似窗口 - > mainController-> tabController [標籤]

在您可以設置委託的tab1Controller讓我們稱之爲「Tab1ControllerDelegate」,它有一個名爲「tab1DidSelectSomething:(NSString *)something」的方法。那麼在你的mainController中,你可以把委託賦給self。

i.e. 
    tab1Controller.delegate = self; 

在你tab1Controller選擇器動作,你可以這樣做:

if(_delegate){ [_delegate tab1DidSelectSomething:<whatEverheSelected>]; }

然後在您的mainController:

-(void)tab1DidSelectSomething:(NSString *)something{ // get the viewController here UIViewController *controller = _tabController.viewControllers[2]; [controller updateselected:something]; _tabController.selectedIndex = 2; }