xcode 我想从视图控制器转到 UITabBarController 中的特定选项卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22618013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I want to go to a specific tab in a UITabBarController from a view controller
提问by ulrikbuchert
First of all, let me say I'm an absolute beginner, so please forgive me if this is a stupid questions to be asking. And let me just add that I've spent hours/days trying to figure out how to solve this problem - including extensive searches on stackoverflow (maybe the answer is somewhere and I just don't now exactly what to search for :)...
首先,让我说我是一个绝对的初学者,所以如果这是一个愚蠢的问题,请原谅我。让我补充一点,我已经花了数小时/数天的时间试图弄清楚如何解决这个问题——包括对 stackoverflow 的广泛搜索(也许答案就在某个地方,而我现在不知道要搜索什么:)。 .
But, let's continue: I have a small (?) problem with an Xcode storyboard project. Basically my project looks like this:
但是,让我们继续:我的 Xcode 故事板项目有一个小 (?) 问题。基本上我的项目是这样的:
Navigation Controller -> View Controller 0 -> Tab Bar Controller -< View Controller 1, View Controller 2, View Controller 3.
导航控制器 -> 视图控制器 0 -> 标签栏控制器 -< 视图控制器 1、视图控制器 2、视图控制器 3。
When the user pushes 'button #2' in the View Controller 0, I'd like him/her to jump directly to 'View Controller 2'.
当用户在视图控制器 0 中按下“按钮 #2”时,我希望他/她直接跳转到“视图控制器 2”。
Would that be possible at all, and if so what code should use and excatly where should I put it.
这可能吗,如果可以,应该使用什么代码,我应该把它放在哪里。
Hope someone out there will help a newbie out :)
希望有人能帮助新手:)
Regards, Ulrik
问候, 乌尔里克
回答by Max Komarychev
Yes it is possible. You may show any view controller from any other.
You should simply add a segue from button #2
to View Controller 2
. (I assume you have all your controllers in single storyboard)
对的,这是可能的。您可以显示任何其他视图控制器。您应该简单地添加一个 segue from button #2
to View Controller 2
。(我假设您在单个故事板中拥有所有控制器)
Update: the above solution will show you View Controller 2
itself without tab bar controller.
更新:上述解决方案将在View Controller 2
没有标签栏控制器的情况下向您展示。
Hard to tell in details without seeing the actual code. For more details you may refer to these documents:
如果没有看到实际的代码,很难说清楚。有关更多详细信息,您可以参考这些文件:
- View Controller Basics(especially part "Storyboards Help You Design Your User Interface")
- Presenting View Controllers from Other View Controllers
- Using View Controllers in Your App
- 视图控制器基础知识(特别是“故事板帮助您设计用户界面”部分)
- 从其他视图控制器呈现视图控制器
- 在你的应用中使用视图控制器
Probably you'll come up with more concrete question.
可能你会提出更具体的问题。
UpdateIf you want to preselect desired view controller inside tabbar controller you may use the following code sketch. Here you can programmatically initiate a segue and do the desired pre-initialization inside prepareForSegue:sender:
method.
更新如果您想在 tabbar 控制器中预选所需的视图控制器,您可以使用以下代码草图。在这里,您可以以编程方式启动 segue 并在prepareForSegue:sender:
方法内部进行所需的预初始化。
static NSString * const kShowTabSegueID = @"ShowTab";
@interface ViewController ()
- (IBAction)buttonOnePressed;
- (IBAction)buttonTwoPressed;
- (IBAction)buttonThreePressed;
@end
@implementation ViewController
- (IBAction)buttonOnePressed
{
[self performSegueWithIdentifier:kShowTabSegueID
sender:@0];
}
- (IBAction)buttonTwoPressed
{
[self performSegueWithIdentifier:kShowTabSegueID
sender:@1];
}
- (IBAction)buttonThreePressed
{
[self performSegueWithIdentifier:kShowTabSegueID
sender:@2];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue
sender:(id)sender
{
if ([segue.identifier isEqual:kShowTabSegueID]) {
NSNumber *indexToShow = sender;
UITabBarController *tabBar = segue.destinationViewController;
[tabBar setSelectedIndex:indexToShow.unsignedIntegerValue];
}
}
@end
回答by RyanG
If you are simply trying to programatically switch tabs, its as simple as:
如果您只是想以编程方式切换选项卡,那么简单如下:
[self.tabBarController setSelectedIndex:1];
[self.tabBarController setSelectedIndex:1];
If I am understanding your flow correctly, from ViewController0 you would present ViewController1(that has a UITabBarController
). In the viewWillAppear:
set the selectedIndex for the tab controller (code above) to index 1, which would be ViewController2.
如果我正确理解您的流程,则从 ViewController0 您将呈现 ViewController1(具有UITabBarController
)。在viewWillAppear:
设置selectedIndex用于选项卡控制器(以上代码),以索引1,这将是ViewController2。
EDIT
编辑
After looking at your project, add this code to your BrainBreaksViewController.m
查看您的项目后,将此代码添加到您的 BrainBreaksViewController.m
- (void)viewWillAppear:(BOOL)animated
{
[self.tabBarController setSelectedIndex:1];
}
I added this, and it switches to the 2nd tab after pressing "Press this button to goto tab #1". Follow Max Ks answer if you would like to be able to have a button to open each specific tab.
我添加了这个,它在按下“按此按钮转到选项卡 #1”后切换到第二个选项卡。如果您希望能够有一个按钮来打开每个特定选项卡,请按照 Max Ks 回答。
回答by stan
So if I understand correctly you have a view controller outside the tabbar controller and you want to navigate to the second tab in the tabbar controller from that (outside)view controller, if that is the problem then, this is my solution and I hope it helps someone as I spent some time on this issue myself.
因此,如果我理解正确,您在 tabbar 控制器之外有一个视图控制器,并且您想从该(外部)视图控制器导航到 tabbar 控制器中的第二个选项卡,如果这是问题所在,那么这是我的解决方案,我希望它帮助某人,因为我自己花了一些时间在这个问题上。
First connect the (outside)Viewcontroller 0 to the tabbar controller in the storyboard with modal segue and give it identifier - "showTabBar" (not as one of the tabs just a modal segue).
首先使用模态转场将(外部)Viewcontroller 0 连接到情节提要中的标签栏控制器,并为其指定标识符 - “showTabBar”(不是作为选项卡之一只是模态转场)。
Then: in Viewcontroller 0 declare:
然后:在 Viewcontroller 0 中声明:
var tabBarIndex: Int?
//function that will trigger the **MODAL** segue
private func loadTabBarController(atIndex: Int){
self.tabBarIndex = atIndex
self.performSegue(withIdentifier: "showTabBar", sender: self)
}
//in here you set the index of the destination tab and you are done
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showTabBar" {
let tabbarController = segue.destination as! UITabBarController
tabbarController.selectedIndex = self.tabBarIndex!
}
}
then you can navigate to ViewController 2 like that(don't forget it is 0 indexed):
然后你可以像这样导航到 ViewController 2(不要忘记它是 0 索引):
self.loadTabBarController(atIndex: 1)
This is tested and working as of the day I am posting this answer using Swift 3.0.2 / Xcode 8.2.1
这在我使用 Swift 3.0.2 / Xcode 8.2.1 发布此答案的那天已经过测试和工作