使用 UITabBar 切换到另一个视图
我刚刚开始使用 iPhone SDK 进行开发,并且在使用 UITabBar 切换到另一个选项卡时遇到问题。
这是我当前的代码,到目前为止它有效:
myAppAppDelegate *appDel = (myAppAppDelegate *)[[UIApplication sharedApplication] delegate]
[appDel.tabBar setSelectedViewController:[appDel.tabBar.viewControllers objectAtIndex:5]];
但是如果我转到“更多”选项卡并重新排列选项卡栏项目,则 viewController 的索引也会更改。我有没有可能解决这个问题?
I just started developing with the iPhone SDK and I have a problem with switching to another tab with the UITabBar.
This is my current code, and it works so far:
myAppAppDelegate *appDel = (myAppAppDelegate *)[[UIApplication sharedApplication] delegate]
[appDel.tabBar setSelectedViewController:[appDel.tabBar.viewControllers objectAtIndex:5]];
But if i go to the more tab and rearrange the tabbar items, the index of the viewControllers change too. Is there any possibility how I could solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以想到的最直接的方法依赖于这样一个事实:当您的应用程序首次启动时,除非您正在执行某些操作来保存重新排序,否则您可以保存 UIViewControllers 的初始列表:
其中“initialOrdering”是 NSArray*然后您将在您发布的代码中使用它来代替 appDel.tabBar.viewControllers 。
The most straight forward means I can think off relies on the fact that when you application first starts, unless you are doing something to save the re-ordering, you could save off the initial list of UIViewControllers:
Where 'initialOrdering' is an NSArray* which you would then use instead of appDel.tabBar.viewControllers in the code you posted.
首先,如果您发现自己输入以下内容:
您可能会从更好的设计中受益。此代码可能来自视图控制器,在这种情况下,您将从视图控制器调用应用程序委托,并处理您不应该了解的内容(选项卡栏)。
更好的设计是委托给应用程序委托,然后应用程序委托为您切换选项卡。应用程序委托应该引用选项卡栏中的实际视图控制器(如果没有,您可以通过 IB 连接它们),以便您可以使用正确的对象调用
setSelectedViewController:
,而不是索引到选项卡中bar 的数组:现在,如果您不想打扰委托,您可以在应用程序委托上放置一个方法(如上面的方法),您的原始代码将变为:
同样,您需要将 IBOutlet 属性添加到您的应用程序委托中连接到 Interface Builder 中的 fooBarController 等。这将使您可以直接引用它们,而不是从数组中获取它们。
First of all, if you ever find yourself typing this:
You can probably benefit from a better design. This code probably comes from a view controller, in which case you are calling out to the App delegate from a view controller, and dealing with stuff you shouldn't have knowledge of (the tab bar).
A better design is to delegate out to the app delegate, and the app delegate switches the tab for you. The app delegate should have references to the actual view controllers in the tab bar (you can hook these up via IB if not) so you can call
setSelectedViewController:
with the correct object, rather than indexing into the tab bar's array:Now if you don't want to bother with delegation you can just put a method on the app delegate (like the one above) and your original code becomes:
Again you will need to add IBOutlet properties to your app delegate which you connect to the fooBarController etc. in Interface Builder. This will let you directly reference them rather than grabbing them out of an array.