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

查看:118
本文介绍了将在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>

我希望我不必摆脱整个应用程序模板,可以设置Tab Bar在IB中创建的控制器将被委派。

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.

正好我想让它委派在tab选项上创建这样的事情:

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.

像这样: / p>

Something like this:

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天全站免登陆