从自定义工具栏中呈现视图控制器
在我的应用程序中,我多次使用相同的底部工具栏。所以我创建了包含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?
如果你只是想呈现一个VC,你可以根据窗口的rootvc来做到这一点。
[[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:vc animated:NO completion:^{
}];
我希望能帮到你!
是的,它有效,但只有一次。当我再次点击按钮时,出现错误:试图在
试试这个:下面的代码
UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[[vc presentedViewController] ? vc.presentedViewController : vc presentViewController:alert animated:YES completion:nil];
使用,我希望这会帮助你。
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"NameOfYourStoryBoard" bundle:nil];
YourViewController *yourVC = [storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentifier"];
[self presentViewController:yourVC animated:YES completion:nil];
[self presentViewController:yourVC animated:YES completion:nil] - “self”表示您在viewController类中。我不是。我在UIToolBar类。 – Schmopsel
可以这个链接帮助你[链接](http://pinkstone.co.uk/how-to-avoid-whose-view-is-not-in-the-window-hierarchy-error-when-presenting-a- uiviewcontroller /) –
我已更新您的代码。
- (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"
}
TRBacklogViewController,TRMyTicketsViewController等 - 故事板名称。我有不同的故事板免除主。 – Schmopsel
把你的工作代码存在。 – KKRocks