在 Xcode 中以编程方式按下 UITabBar 按钮
抱歉新手问题。我的主窗口视图中有一个 UITabBar 以及每个选项卡的 UINavigationController 数组。该结构类似于 iPod 应用程序,因为可以通过选择 TabBar 项目来查看主视图,然后用户可以通过将视图推送到堆栈来使用 NavigationController 进一步向下钻取。
我希望能够做的是相当于从代码中的任何子视图按下底部的 TabBar 按钮(即更改 TabBar 的选定属性并显示启动选项卡的第一个视图控制器) 。
任何帮助将不胜感激。
戴夫
Sorry for the newbie question. I have a UITabBar in my main window view as well as an array of UINavigationControllers for each Tab. The structure is similar to the iPod app in that the main views can be seen by selecting TabBar items and then the user can drill down further with the NavigationController by pushing views to the stack.
What I would like to be able to do is to do the equivalent of pressing a TabBar button at the bottom from any of the subviews in code (i.e., change the selected property of the TabBar and display launch the first view controller for the tab).
Any help would be greatly appreciated.
Dave
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
编辑:回答评论中的第 2 部分问题:
您可以在 AppDelegate 中定义一个方法来切换到不同的选项卡。
您可以从任何地方获取 appdelegate 并发送消息。
像这样的东西:
EDIT: Answering the part 2 question from the comment:
You can define a method in AppDelegate for switching to a different tab.
And you can get hold of appdelegate from anywhere and send a message..
something like:
或者...
alternatively...
我想回复 Prakash,但不知道如何回复。也许我会被封锁,直到我的分数上升。
无论如何,我希望这对某人有帮助:
我正在按照普拉卡什所说的去做,但什么也没发生。这是因为为了获得指向我的应用程序委托的指针,我正在这样做:
当我应该这样做时:
新手错误。
I'd like to reply to Prakash, but can't figure out how. Maybe I'm blocked until my score goes up.
Anyhow, I hope this helps someone:
I was doing what Prakash said, and nothing was happening. It's because to get a pointer to my app delegate, I was doing this:
When I should have been doing this:
Newbie mistake.
为此,您只需要获取 UITabBar 控制器 -
.h 文件 -
就可以根据您的需要设置索引
For this, You just need to take UITabBar controller -
.h File -
than you can set index as per your needs
为我选择了索引,但它只是突出显示了导航栏控制器的索引作为活动索引,但是虽然它突出显示了该索引,但它实际上位于与选项卡菜单项建议的不同的视图控制器上。
只是想补充一点,我从视图控制器中使用了它,它的表现就像有人实际按下了菜单项一样;来自代码:
感谢您的这篇文章/答案,它对我的项目帮助很大。
Selected the index for me but it just highlighted the navbarcontroller's index as the active index, but while it highlighted that index it was actually on a different viewcontroller than was suggested by the tabbarmenu item.
Just wanted to add that I used this from my view controller, and it performed like someone actually pressed the menuitem; from code:
Thank you for this post/answers it helped out a lot in my project.
我想做类似的事情,但对于 XCode 6.4 iOS (8.4) setSelectedIndex 本身不会这样做。
将选项卡栏的视图控制器添加到列表中,然后在某个函数中使用类似以下内容,然后调用它:
您的 didSelectedItem 中可能已经有类似的代码。
I wanted to do something similar but for XCode 6.4 iOS (8.4) setSelectedIndex by itself won't do it.
Add the view controllers of the tab bar to a list and then use something like the following in some function and then call it:
You might have similar code already inside your didSelectedItem..