在 AppDelegate 之外使用 NIB 创建 UITabBarController?
对于 iOS 编程来说,我还是个新手,尽管进行了大量的研究,但我还是遇到了另一个障碍。
我想要实现的内容:
我想要一个在我从主 UI 导航时加载的 UITabBarController。我还想使用 NIB 来定义其属性。
我能找到的所有示例都将 UITabBarController 放入 AppDelegate 中,但我不想加载它,除非它被使用。我也不知道如果我只是以模态方式执行此操作,所有 UIGestureRecognizer 是否都会保持活动状态(我无法获得有效的实现)。
到目前为止我所拥有的
首先,我从 AppDelegate 加载初始加载视图
AppDelegate.h
@class InitialViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *viewController;
@end
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。
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
- (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 中选项卡的实际选项卡或视图。
我怀疑我在两个区域中缺少一些东西:在选项卡 .h 中,以及与界面生成器中相关的一些链接。或者设置一个新的 rootViewController 是不够的。
tabBarController.h
#import <UIKit/UIKit.h>
@interface iPodViewController : UITabBarController <UITabBarControllerDelegate>
@end
如果有人能给我指出正确的方向和/或向我展示一个可行的实现,我将不胜感激。
-- 作为注释,当我进入 tabbar.xib 并使用助理编辑器时,它会打开 InitialViewController.h --
Still new to iOS programming, and despite copious amounts of research, I have run in to another roadblock.
What I want to implement:
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.
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).
What I have so far
First, I load an initial loading view from AppDelegate
AppDelegate.h
@class InitialViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *viewController;
@end
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;
}
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
@interface InitialViewController : UIViewController
- (IBAction)toMain:(id)sender;
- (IBAction)toTabs:(id)sender;
@property (strong, nonatomic) UIViewController *mviewController;
@property (strong, nonatomic) UIViewController *tviewController;
@end
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;
}
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.
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
#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.
-- as a note, when I go in to the tabbar.xib, and use the assistant editor, it opens InitialViewController.h --
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与其他视图控制器(例如 UITableViewController)不同,您不应该子类化 UITabViewController。因此,与其他视图控制器不同,您不需要子类化,然后使子类成为笔尖的所有者,并使用自定义视图指向笔尖中的视图。
相反,对于您想要拥有 UITabBarController 的任何类,请添加一个普通的普通 UITabBarController 作为此类的插座属性。 (例如您的应用程序委托)。
然后创建一个 nib 文件并将 UITabBarController 对象拖到 nib 中。将笔尖的所有者设置为您想要拥有选项卡栏控制器的类(例如您的应用程序委托),并将您创建的插座作为属性连接到笔尖中的选项卡栏控制器。
现在,当您想要创建并显示标签栏控制器时,请使用以下方法:
这将初始化所属类上的属性(即我们示例中的 myTabBarControllerOutlet),并从 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.
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).
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.
Now at the point you want to create and display your tab bar controller, use the following method:
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.