簡體   English   中英

UITabBarController為空嗎?

[英]UITabBarController is null?

我試圖從頭開始創建一個基於選項卡欄的應用程序,但是遇到了一些問題。 基本上,我有AppDelegate.h,AppDelegate.m和MainView.xib。 在.h文件中,我有:

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) IBOutlet UITabBarController *tabBarController;

@end

在我的.m中,我有:

@synthesize window = _window;
@synthesize tabBarController = _tabBarController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

在我的xib文件中,我擁有文件所有者(屬於Appdelegate類),並且從那里連接了標簽欄控制器。 問題是我的控制器返回null,因此未設置為rootViewController。 我在設置應用程序時缺少什么步驟/想法? 提前致謝!

修改后的修正:因為我是從一個空的應用程序啟動的,所以我沒有設置“ Main nib file base name”,所以我只需要將其設置為我要加載的筆尖即可。

您需要創建將通過“標簽欄控制器”上的標簽訪問的視圖控制器。 然后,您必須將它們分配給選項卡欄的viewControllers屬性。 您可以在將選項卡欄設置為窗口的rootViewController之前執行此rootViewController

MyViewController1 *vc1 = [[MyViewController1 alloc] init];
MyViewController2 *vc2 = [[MyViewController2 alloc] init];
NSArray *controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
self.tabBarController.viewControllers = controllers;

希望對您有所幫助!

通常,appDelegate對象不是UIResponder的子類,而是NSObject的子類。

您是否將TabBar實例掛接到xib文件中的第一個響應者? 考慮到這不會起作用,因為第一響應者只是一個代理對象,並且將類類型設置為它不會導致實例化該對象。

而是在xib文件中創建一個AppDelegate對象,然后將TabBar鈎到該對象。

希望能幫助到你。

由於我是從一個空的應用程序啟動的,所以我沒有設置“ Main nib file base name”(主筆尖文件基本名稱),因此我只需要將其設置為我要加載的筆尖即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM