簡體   English   中英

如何將UITabBarController添加到現有項目

[英]How do you add a UITabBarController to an existing project

我有一個iPhone項目,該項目以基於UIView的標准Window開頭...當用戶單擊其按鈕以使用UITabBarController進入新視圖時(類似於iTunes Connect應用程序登錄后的行為)。 Apple文檔中沒有示例代碼示例可以滿足我的要求,但我知道它是可能的,因為Apple已在自己的應用程序中完成了(另一個示例是iPhone的MobileMe iDisk應用程序)。

我已經嘗試過標准的-presentModalViewController:animated:方法,但是該方法不起作用,因為在UITabBarController中沒有可以附加的視圖。

接下來,我將嘗試在App Delegate中使用兩個窗口XIB,以查看是否可以使用該方法。

如果您知道如何回答我的這個小問題,我將不勝感激。 =)

我最終要做的是:

在我的應用程序委托中,我的界面中包含以下內容:

@interface myAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow * window;
    LauncherViewController * startup;
    UITabBarController * tabs;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet LauncherViewController * startup;
@property (nonatomic, retain) IBOutlet UITabBarController * tabs;
@end

在實現文件中,將以下內容添加到應用程序啟動功能中:

- (BOOL)application:(UIApplication *)application 
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    [self.window addSubview:self.startup.view];
    [self.window makeKeyAndVisible];

    NSNotificationCenter * notifier = [NSNotificationCenter defaultCenter];

    [notifier addObserver:self
                 selector:@selector(launch)
                     name:MyAppLoginInitializedNotification
                   object:nil];

    [notifier addObserver:self
                 selector:@selector(logout)
                     name:MyAppLogoutNotification
                   object:nil];

    return YES;
}

- (void) launch {
    [self.startup.view removeFromSuperview];
    [self.window addSubview:tabs.view];
    [self.window makeKeyWindow];
}

- (void) logout {
    [self.tabs.view removeFromSuperview];
    [self.window addSubview:startup.view];
    [self.window makeKeyWindow];
}

我的主要XIB包含定義為LauncherViewController的標准UIViewController以及通用的UITabBarController。 一旦我的主啟動程序控制器對用戶憑據進行身份驗證並發送MyAppLoginInitializedNotification ,應用程序委托就從啟動程序切換到選項卡視圖,使我能夠繼續執行我的應用程序邏輯。

UITabBarController實際上只是UIViewController的子類,因此-presentModalViewController:animated:應該起作用:

UITabBarController *someController = [[UITabBarController alloc] init];
someController.viewControllers = /* your View Controllers here */
[self presentModalViewController:someController animated:NO];

如果我正確理解了您的問題,那么您想在問題中提到的第一個視圖之后啟動UITabBarController視圖,我會附加一個鏈接,執行您所需的相同操作,只是在UITabBarController視圖出現之前您有一個Extra視圖,希望它將給你一個指南。

http://www.mobisoftinfotech.com/blog/iphone/iphone-tabbar-uitabbarcontroller-tutorial/

我認為您不必在nib文件中重新添加UITabBarController。 只需用代碼創建它,並按照上面的海報所述添加它,您就可以開始使用了。 這是一些對我有用的代碼。

    UITabBarController *nextController = [[UITabBarController alloc] init];

    FirstController *firstView = [[FirstController alloc] initWithNibName:@"FirstView" bundle:nil];
    SecondController *secondView = [[SecondController alloc] initWithNibName:@"SecondView" bundle:nil];
    ThirdController *thirdView = [[ThirdController alloc] initWithNibName:@"ThirdView" bundle:nil];

    [nextController setViewControllers:[NSArray arrayWithObjects:firstView, secondView, thirdView, nil] animated:NO];

到這一點為止,它應該是相同的,但是我將Tabbar控制器推入了uinavgiationcontroller中,所以這可能與我們有所不同。 我這樣做如下:

    [self.navigationController pushViewController:nextController animated:YES];

暫無
暫無

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

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