确定 UITabBar Item 属于哪个 UIViewController 子类
我在 appDelegate 中实例化了一个 UITabBarController,但我需要一种方法来知道用户何时按下不同的选项卡栏项目(选项卡栏上的按钮)。
-UITabBarDelegate 协议来救援(使用所需的 didSelectViewController 方法)!
在 Interface Builder 中连接所有内容后,如何获取对与按下的此选项卡栏项目相对应的实际 UIViewController 子类实例的引用?
我需要这个引用,因为每次按下选项卡栏项目时,我都需要调用 UIViewControllers 子类之一中的方法。
有什么建议吗?
- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"%@", [[self.tabBarController selectedViewController] nibName]); // nil, no success here
if ([theTabBarController selectedIndex] == 1) {
MySecondViewController *reference = (MySecondViewController *) viewController;
if ([reference isKindOfClass:[UINavigationController class]]) {
NSLog(@"OMG. It's a UINavigationController class??!"); // kicks in for some reason, shouldn't reference be a MySecondViewController
}
}
I have a UITabBarController instansiated in my appDelegate, but I need a way to know when the user presses the different tab bar items (buttons on the tab bar).
-UITabBarDelegate protocol to the rescue (with the required didSelectViewController-method)!
With everything wired up in Interface Builder, how do I obtain a reference to the actual UIViewController subclass instance that corresponds to this tab bar item that was pressed?
I need this reference because I need to call a method in one of my UIViewControllers subclasses every time that tab bar item is pressed.
Any suggestions?
- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"%@", [[self.tabBarController selectedViewController] nibName]); // nil, no success here
if ([theTabBarController selectedIndex] == 1) {
MySecondViewController *reference = (MySecondViewController *) viewController;
if ([reference isKindOfClass:[UINavigationController class]]) {
NSLog(@"OMG. It's a UINavigationController class??!"); // kicks in for some reason, shouldn't reference be a MySecondViewController
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能我没有正确理解你的问题,但似乎你所要求的只是“viewController”参数,该参数被传递到你提到的方法调用中
UITabBarController 也有一个属性来获取相同的信息
Possibly I am not understanding your question correctly, but it seems that what you're asking for is simply the "viewController" parameter which is passed into the method call you've mentioned
UITabBarController also has a property to get the same info