将在 Interface Builder 中创建的 UITabBarController 设置为委托 [英] Set UITabBarController created in Interface Builder as delegate

查看:16
本文介绍了将在 Interface Builder 中创建的 UITabBarController 设置为委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Tab Bar 模板创建了我的 iOS 应用程序,所以这里是带有栏按钮的 UITabBarController.一个问题是如何将其设置为委托.我在 SO 发现它必须在 AppDelegate 中以编程方式设置,但我相信这是不可能的,因为我无法访问 Tab Bar Controller 作为出口.

I created my iOS app with Tab Bar template, so here is UITabBarController with bar buttons. An issue is how to set it as delegate. I found at SO that it has to be set programmatically in AppDelegate, but I believe it's impossible, because I've got no access to Tab Bar Controller as outlet.

我为 @interface(ViewController 和 AppDelegate)添加了适当的值,但不知道下一步该做什么.

I added proper value to @interface (both ViewController and AppDelegate), but doesn't know what to do next.

@interface ViewController : UIViewController <UITabBarControllerDelegate>

我希望我不必摆脱整个应用程序模板,并且可以将在 IB 中创建的 Tab Bar Controller 设置为委托.

I hope I don't have to get rid of whole app template and it's possible to set Tab Bar Controller created in IB to be delegate.

正是我想让它委托在标签选择事件上创建,如下所示:

Exactly I want to make it delegate to create on tab select event like this:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;

有什么想法吗?

推荐答案

我不记得确切的 Xcode 的 Tab Bar 模板设置,但是在你的 AppDelegate 中你可以访问你的窗口的 rootViewController,将其转换为 UITabBarController,然后将其委托设置为您的 AppDelegate 或任何其他视图控制器.

I don't remember exactly the Xcode's Tab Bar template set up, but in your AppDelegate you can access to your window's rootViewController, cast it to a UITabBarController, and then set its delegate to your AppDelegate or to any other view controller.

类似这样的:

UITabBarController *tabBarController = 
    (UITabBarController *)[[self window] rootViewController];
[tabBarController setDelegate:self]; // In this example your app delegate would implement the UITabBarControllerDelegate protocol.

编辑

如果你想将你的 ViewController 实例设置为委托:

If you want to set your ViewController instance as the delegate:

UITabBarController *tabBarController = 
        (UITabBarController *)[[self window] rootViewController];
// I assume you have your ViewController instance set as the first view controller of your tab bar controller
// No need for a cast here since objectAtIndex: returns id, but of course you must implement the UITabBarController protocol in your ViewController.
    [tabBarController setDelegate:[[tabBarController viewControllers] objectAtIndex:0]]];

编辑 2从您的 ViewController 本身,您可以将选项卡栏控制器的委托设置为 rdelmar 注释.请记住,这不能在 init 方法中完成,因为视图控制器尚未在选项卡栏控制器中.正确的位置是 viewDidLoad 但因此在 ViewController 视图加载之前不会执行...

EDIT 2 From your ViewController itself you can set the tab bar controller's delegate as rdelmar comments. Just keep in mind that this cannot be done in the init method because the view controller is not in the tab bar controller yet. The proper place would be viewDidLoad but therefore it will not be executed until the ViewController view loads...

self.tabBarController.delegate = self;

这篇关于将在 Interface Builder 中创建的 UITabBarController 设置为委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆