我见过一些类似的问题,但我找不到与我现在遇到的问题足够接近的问题。
这就是我现在在我的故事板中的内容。我有一个带有许多按钮的视图,每个按钮都应该链接到一个特定的视图(这是选项卡之一)。现在我只能在单击任何按钮时推送到第一个选项卡上的选项卡视图。我想知道如何从按钮实际跳转到特定选项卡。
请原谅我糟糕的英语,提前谢谢你。
我见过一些类似的问题,但我找不到与我现在遇到的问题足够接近的问题。
这就是我现在在我的故事板中的内容。我有一个带有许多按钮的视图,每个按钮都应该链接到一个特定的视图(这是选项卡之一)。现在我只能在单击任何按钮时推送到第一个选项卡上的选项卡视图。我想知道如何从按钮实际跳转到特定选项卡。
请原谅我糟糕的英语,提前谢谢你。
为按钮指定标签 100、101、102、103 和 104。
将第一个 Scene 到 TabBar 场景的 segue 的 Identifier 设置为 showTabBar。
将每个按钮连接到相同的 IBAction 方法,您必须在其中编写代码:
- (IBAction)onButtonTap:(id)senderButton
{
[self performSegueWithIdentifier:@"showTabBar" sender:senderButton];
}
另外,覆盖 prepareForSegue:sender: :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
UITabBarController *tabBarController = segue.destinationViewController;
tabBarController.selectedIndex = ((UIButton *) sender).tag - 100;
}
设置 TabBarController 的情节提要 ID。参考图片
现在访问 tabBar 的对象并选择您想要的任何视图控制器
UITabBarController *tabBar=[MainStoryboard instantiateViewControllerWithIdentifier:@"MyTabBarController"];
tabBar.selectedIndex = 2;
设置 TabBarController 的情节提要 ID。正如@Mayank Jain 和首先为您的按钮分配标签所建议的那样,实现以下所有按钮通用的方法:
- (IBAction)buttonTaped:(UIButton *)sender
{
MainTabBarViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainTabBarView"];
if (sender.tag == 0)
vc.selectedIndex = 0;
else if (sender.tag == 1)
vc.selectedIndex = 1;
else if (sender.tag == 2)
vc.selectedIndex = 2;
else if (sender.tag == 3)
vc.selectedIndex = 3;
else
vc.selectedIndex = 4;
}