2014-09-30 11 views
2

我已經在應用程序中下面的故事板我的工作:切換到在用戶後不同的選項卡的視圖控制器執行動作

View of my storyboard

在根,我有一個標籤欄控制器。它鏈接到兩個視圖控制器。

第一個視圖控制器顯示用戶上傳的圖片的新聞源(故事板底部的圖片)。

第二個視圖控制器用於啓動拍攝圖片並附加一些數據。在最後一步(右上角),當觸摸導航欄右邊的「保存」時,我希望用戶被重定向到新聞源視圖控制器,以傳遞一些數據。

我嘗試使用segue,它的工作原理。數據被傳遞給新聞源,但選擇了錯誤的選項卡。我改變了選擇標籤使用

[self.tabBarController setSelectedIndex:0]; 

但通過再次點擊第二個選項卡,事情搞砸了。我可以看到新聞源而不是拍攝照片。如果我再次點擊,它會崩潰。

在某些時候,我想我可能得到了錯誤的故事板,應該在我的新聞源中實現TabBar,並將拍攝的照片作爲模態視圖處理。

你知道任何干淨的方式來實現嗎?

感謝

回答

1

你不應該使用普通SEGUE,這增加目標控制器到堆棧中。做你正在嘗試的最好的方法應該是使用放鬆的繼續。這是您需要做什麼的粗略草圖:

•在NewsfeedController(如(IBAction)unwindFromPictureSaved:(UIStoryboardSegue *)segue)中聲明展開的繼續動作;
•將您的SavingPictureController中的「保存」按鈕連接到故事板中的「退出」圖標,並選擇以前定義的方法;
•在新創建的展開順序中,使用類似SavedPictureSegue的東西來定義其標識符;
•在SavingPictureController的標題中定義要傳遞的數據,如@property (strong, readonly, nonatomic) id passedData;
•在SavingPictureController實施

-(void)prepareForSegue:(UIStoryboardSegue *)segue 
{ 
    if ([segue.identifier isEqualToString:@"SavedPictureSegue"]) { 
     _passedData = // Your data here 
    } 
} 

•在NewsfeedController現在實施前面定義的方法和從(SavingPictureController *)segue.sourceController獲取數據。一定要#import "SavingPictureController.h"

+2

感謝@Davide你把我在正確的軌道上我呢。注意到默認行爲是搜索視圖控制器來回答不同孩子VC中的放鬆繼續,或者回到父母,他們將依次調用其不同的子女VC。因爲在我的情況下,'NewsFeedController'在'NavigationController'之後,當'TabBarController'搜索時,它沒有找到它。因此,我創建了一個'TabBarController'的子類,並覆蓋了方法'viewControllerForUnwindSegueAction:fromViewController:fromViewController:withSender:' – kintso 2014-10-01 12:04:30

+0

我認爲它會找到它,因爲調用應該繼續到UITabBarController,它詢問子節點另一個'UINavigationController'也向子節點詢問'NewsfeedViewController'的位置... – DeFrenZ 2014-10-01 12:44:48

+1

經過測試,第二部分不會發生。至少在我的情況下 – kintso 2014-10-01 13:25:01

0

由於@Davide,我創建TabBarController的子類,實現下面的方法:在「NewsFeedController的退繞方法

// Find the appropriate controller to answer to an unwind segue 
// For each child view controller 
// Checks if it is a Navigation Controller 
// If it is check its children view controllers 
// Return the first view controller that answers the unwind segue 
// This because I assumed the default behavior is just to check one level up (in this case, it would have stopped at the NavigationController) 
// Based on https://developer.apple.com/library/ios/technotes/tn2298/_index.html#//apple_ref/doc/uid/DTS40013591-CH1-CCVC-SELECTING_A_CHILD_VIEW_CONTROLLER_TO_HANDLE_AN_UNWIND_ACTION 
- (UIViewController *)viewControllerForUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender { 
    BOOL resChildren, res; 
    for(UIViewController *controller in self.childViewControllers) { 
     if ([controller isKindOfClass:[UINavigationController class]]) { 
      for (UIViewController *childController in controller.childViewControllers) { 
       resChildren = [childController canPerformUnwindSegueAction:action fromViewController:fromViewController withSender:sender]; 
       if (resChildren) { 
        return childController; 
       } 
      } 
     } 
     res = [controller canPerformUnwindSegueAction:action fromViewController:fromViewController withSender:sender]; 
     if (res) { 
      return controller; 
     } 
    } 
    return nil; 
} 

然後」,有必要設置正確的索引中看到的控制器喜歡的東西:

[self.tabBarController setSelectedIndex:1]; 

我上傳GitHub上演示在https://github.com/kintso/unwindSegueWithTabBarControllerAndNavigationController

相關問題