在我的应用中,我多次使用相同的底部工具栏。所以我创建了新的UITollbar类,其中包含4个UIBarButtonItems。如何编写它们来呈现另一个viewcontroller?我不想每次都这样做,当我创建新的视图控制器时,所以我更喜欢这样做一次并将这个类用于每个相同的tooolbars。 例如,这是我对所有按钮的操作:
- (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];
}
问题是 - 我不止这么做了。按钮上的其他点击失败,错误:&#34;尝试在<UINavigationController: 0x7ab0cc00>
上显示<UINavigationController: 0x7c304600>
,其视图不在窗口层次结构中!&#34;有没有办法使用pushViewController?
答案 0 :(得分:0)
如果你只是想呈现一个vc,你可以在窗口的rootvc上做这个。
[[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:vc animated:NO completion:^{
}];
我希望能帮到你!
答案 1 :(得分:0)
试试这个:
UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[[vc presentedViewController] ? vc.presentedViewController : vc presentViewController:alert animated:YES completion:nil];
答案 2 :(得分:0)
使用以下代码,我希望这对您有帮助。
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"NameOfYourStoryBoard" bundle:nil];
YourViewController *yourVC = [storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentifier"];
[self presentViewController:yourVC animated:YES completion:nil];
答案 3 :(得分: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"
}