使用AppDelegate之外的NIB创建UITabBarController? [英] Creating a UITabBarController using a NIB outside of AppDelegate?

查看:119
本文介绍了使用AppDelegate之外的NIB创建UITabBarController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS编程还是新手,尽管进行了大量的研究,但我还遇到了另一个障碍。

Still new to iOS programming, and despite copious amounts of research, I have run in to another roadblock.

我想要实现的目标:

我想要一个从主UI导航时加载的UITabBarController。我还想使用NIB来定义它的属性。

I want a UITabBarController that gets loaded when I navigate from the main UI. I would also like to use a NIB to define its properties.

我能找到的所有例子都把UITabBarController放在AppDelegate中,但是我不想加载它除非它被使用。我也不知道是否所有的UIGestureRecognizer都会保持活动状态,如果我只是模态化(我无法获得有效的实现)。

All of the examples I can find put the UITabBarController in the AppDelegate, but I would not like to load it unless it gets used. I also dont know if all of the UIGestureRecognizers would remain active if I just did it modally (I cant get a working implementation).

到目前为止我所拥有的

首先,我从AppDelegate加载初始加载视图

First, I load an initial loading view from AppDelegate

AppDelegate.h

AppDelegate.h

@class InitialViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *viewController;

@end

AppDelegate.m

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[InitialViewController alloc] initWithNibName:@"InitialViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}

从这个视图来看,我只是制作UI的骨架,我有两个按钮,一个是主界面,另一个是UITabBarController。

From this view, as I am just making a skeleton of the UI, I have two buttons, one goes to what would be the main interface, and the other to the UITabBarController.

InitialViewController.h

InitialViewController.h

@interface InitialViewController : UIViewController
- (IBAction)toMain:(id)sender;
- (IBAction)toTabs:(id)sender;

@property (strong, nonatomic) UIViewController *mviewController;
@property (strong, nonatomic) UIViewController *tviewController;
@end

InitialViewController.m

InitialViewController.m

- (IBAction)toMain:(id)sender {

self.mviewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
[[[UIApplication sharedApplication] delegate] window].rootViewController = self.mviewController;

}

- (IBAction)toTabs:(id)sender {
self.tviewController = [[tabViewController alloc] initWithNibName:@"tabViewController" bundle:nil];

[[[UIApplication sharedApplication] delegate] window].rootViewController = self.tviewController;

}

在加载MainViewController时,它的行为与我想要的完全一样。但是当我加载标签视图时,我在底部有一个长标签和黑色背景。我可以在viewdidload中添加内容,例如更改背景颜色,但没有链接到XIB中的选项卡的实际选项卡或视图。

On loading MainViewController, it behaves exactly like I want. But when I load the tab view, I get one long tab at the bottom and a black background. I can add in things in viewdidload, like changing the background color, but no actual tabs or views linked to the tabs in the XIB.

我怀疑我在两个方面缺少一些东西:在选项卡.h中,以及与界面构建器中的链接关联的一些链接。或者设置一个新的rootViewController是不够的。

I suspect there is something I am missing in two areas: in the tab .h, and some linking associated with that in interface builder. Or setting a new rootViewController isnt enough.

tabBarController.h

tabBarController.h

#import <UIKit/UIKit.h>

@interface iPodViewController : UITabBarController <UITabBarControllerDelegate>
@end

如果有人可以指出我正确的方向和/或告诉我有效的实现,我将非常感激。

If someone can point me in the right direction and/or show me an implementation that works, I would be most grateful.

- 作为注释,当我进入tabbar.xib并使用助手编辑器时,它会打开InitialViewController .h -

-- as a note, when I go in to the tabbar.xib, and use the assistant editor, it opens InitialViewController.h --

推荐答案

与其他视图控制器(例如UITableViewController)不同,您不应该继承UITabViewController。因此,与其他视图控制器不同,您不进行子类化,然后使用自定义视图使子类成为nib的所有者,指向nib中的视图。

Unlike other view controllers (e.g. UITableViewController) you should not subclass the UITabViewController. Therefore, unlike you other view controllers, you don't subclass and then make your subclass the owner of the nib, pointing at the view in the nib, with a customised view.

相反,对于您想要拥有UITabBarController的任何类,请在此类上添加一个普通的vanilla UITabBarController作为outlet属性。 (例如你的app委托)。

Instead, for whichever class that you want to own your UITabBarController, add a plain, vanilla UITabBarController as an outlet property on this class. (e.g. your app delegate).

然后创建一个nib文件并将UITabBarController对象拖到nib中。将nib的所有者设置为您要拥有标签栏控制器的类(例如,您的应用程序委托),并将您创建的属性作为属性连接到笔尖中的标签栏控制器。

Then create a nib file and drag a UITabBarController object into the nib. Set the owner of the nib to be the class that you want to own your tab bar controller (e.g. your app delegate) and connect the outlet you created as a property to the tab bar controller in the nib.

@interface myTabOwningClass

    @property (strong, nonatomic) IBOutlet UITabBarController myTabBarControllerOutlet;

现在,您想要创建并显示标签栏控制器,请使用以下方法:

Now at the point you want to create and display your tab bar controller, use the following method:

[[NSBundle mainBundle] loadNibNamed:@"MyTabControllerNib" owner:myTabOwningClass options:nil];

这将在拥有类上初始化属性(即我们示例中的myTabBarControllerOutlet)并加载选项卡栏来自笔尖的控制器,包括您在笔尖中定义的每个标签等的所有子视图控制器。

This will initialise the property (i.e. myTabBarControllerOutlet in our example) on the owning class and load the tab bar controller from the nib, including all sub view controllers for each tab etc. that you have defined in the nib.

这篇关于使用AppDelegate之外的NIB创建UITabBarController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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