2016-11-08 76 views
0

在我的應用程序中,我多次使用相同的底部工具欄。所以我創建了包含4個UIBarButtonItems的新的UITollbar類。如何編寫它們以呈現另一個視圖控制器?我不想每次都這樣做,當我創建新的視圖控制器時,所以我更喜歡這樣做一次,併爲每個相同的工具欄使用這個類。 例如,這是我所有按鈕的作用:從自定義工具欄中呈現視圖控制器

- (void)actionButtonTapped:(UIBarButtonItem*)barButtonItem { 

    switch (barButtonItem.tag) { 
     case BacklogButton: 
      break; 

     case MyTicketsButton: 
      break; 

     case FilterButton: 
      break; 

     case ProjectsButton: 
      break; 
    } 
} 

它的工作不錯,但使用presentViewController我必須在UIViewController類。我需要你的幫助。

UPD。 我爲你們寫了更多的代碼來解釋我的問題。

- (void)actionButtonTapped:(UIBarButtonItem*)barButtonItem { 

    switch (barButtonItem.tag) { 
     case BacklogButton: 
      [self presentViewControllerFromStoryBoardName:@"TRBacklogViewController"]; 
      break; 

     case MyTicketsButton: 
      [self presentViewControllerFromStoryBoardName:@"TRMyTicketsViewController"]; 
      break; 

     case FilterButton: 
      [self presentViewControllerFromStoryBoardName:@"TRFilterViewController"]; 
      break; 

     case ProjectsButton: 
      [self presentViewControllerFromStoryBoardName:@"TRProjectsViewController"]; 
      break; 
    } 
} 

- (void)presentViewControllerFromStoryBoardName:(NSString*)storyboardName { 

    [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:[self viewControlerFromStoribordName:storyboardName] animated:YES completion:^{ 
    }]; 
} 

- (UIViewController*)viewControlerFromStoribordName:(NSString*)storyboardName { 

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil]; 
    return (UIViewController*)[storyboard instantiateInitialViewController]; 
} 

的問題是 - 我不能做一個以上的時間。其他對按鈕的點擊失敗,並顯示錯誤:「試圖在<UINavigationController: 0x7c304600>上顯示<UINavigationController: 0x7ab0cc00>,其視圖不在窗口層次結構中!」有沒有什麼方法可以使用pushViewController?

+0

把你的工作代碼存在。 – KKRocks

回答

0

如果你只是想呈現一個VC,你可以根據窗口的rootvc來做到這一點。

 [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:vc animated:NO completion:^{ 
    }]; 

我希望能幫到你!

+0

是的,它有效,但只有一次。當我再次點擊按鈕時,出現錯誤:試圖在上呈現,其視圖不在窗口層次結構中! – Schmopsel

0

試試這個:下面的代碼

UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; 

[[vc presentedViewController] ? vc.presentedViewController : vc presentViewController:alert animated:YES completion:nil]; 
0

使用,我希望這會幫助你。

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"NameOfYourStoryBoard" bundle:nil]; 
YourViewController *yourVC = [storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentifier"]; 
[self presentViewController:yourVC animated:YES completion:nil]; 
+0

[self presentViewController:yourVC animated:YES completion:nil] - 「self」表示您在viewController類中。我不是。我在UIToolBar類。 – Schmopsel

+0

可以這個鏈接幫助你[鏈接](http://pinkstone.co.uk/how-to-avoid-whose-view-is-not-in-the-window-hierarchy-error-when-presenting-a- uiviewcontroller /) –

0

我已更新您的代碼。

- (void)actionButtonTapped:(UIBarButtonItem*)barButtonItem { 

    switch (barButtonItem.tag) { 
    case BacklogButton: 
     [self presentViewControllerFromStoryBoardName:@"TRBacklogViewController"]; 
    // @"TRBacklogViewController" I think its your View controller Identifier not a storyboard name 
     break; 

    case MyTicketsButton: 
     [self presentViewControllerFromStoryBoardName:@"TRMyTicketsViewController"]; 
     break; 

    case FilterButton: 
     [self presentViewControllerFromStoryBoardName:@"TRFilterViewController"]; 
     break; 

    case ProjectsButton: 
     [self presentViewControllerFromStoryBoardName:@"TRProjectsViewController"]; 
     break; 
    } 
} 

- (void)presentViewControllerFromStoryBoardName:(NSString*)storyboardName { 

[[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:[self viewControlerFromStoribordName:storyboardName] animated:YES completion:^{ 
    }]; 
} 

- (UIViewController*)viewControlerFromStoribordName:(NSString*)storyboardName { 

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
// May be "Main" is your storyboard name 
    return (UIViewController*)[storyboard instantiateViewControllerWithIdentifier:storyboardName]; 
    // Use object name "viewcontrollerIdentifier" instead of "storyboardName" 

} 
+0

TRBacklogViewController,TRMyTicketsViewController等 - 故事板名稱。我有不同的故事板免除主。 – Schmopsel

相關問題