【发布时间】:2012-02-17 04:58:17
【问题描述】:
对 iOS 编程还是新手,尽管进行了大量研究,但我遇到了另一个障碍。
我想要实现的内容:
我想要一个在我从主 UI 导航时加载的 UITabBarController。我还想使用 NIB 来定义它的属性。
我能找到的所有示例都将 UITabBarController 放在 AppDelegate 中,但除非它被使用,否则我不想加载它。我也不知道如果我只是以模态方式执行所有 UIGestureRecognizers 是否会保持活动状态(我无法获得有效的实现)。
我目前所拥有的
首先,我从 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 --
【问题讨论】:
-
我找到了一个解决方案,但是,我不了解其中的一部分功能。第一个问题是我继承了
UITabbarController,所以我创建了一个新的UIViewController来加载标签。现在,在- (void)viewDidLoad中,我从以下位置获取选项卡:`thetabs = [[UITabBarController alloc] initWithNibName:@"tabcontrol" bundle:nil]; [[NSBundle mainBundle] loadNibNamed:@"tabcontrol" owner:self options:nil];窗口 = [[[UIApplication sharedApplication] 委托] 窗口]; window.rootViewController = 选项卡; ` 但我不知道为什么initWithNibName什么都不做而NSBundle做。
标签: xcode user-interface ios4 uitabbarcontroller