2013-02-01 64 views
0

使用xcode4.2的Iam我在單視圖應用程序中爲tabbar添加了3個項目。必須應用單視圖應用程序的tabbar

的問題是我不能想象的輸出,而不是一個黑色的屏幕顯示在iOS模擬器

誰能幫我請

我的代碼是

@synthesize window = _window; 
@synthesize viewController = _viewController; 
@synthesize appNavigation = _appNavigation; 

- (void)dealloc 
{ 
    [_window release]; 
    [_viewController release]; 
    [_appNavigation release];  
    [super dealloc]; 
} 



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
     self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; 

      UITabBarController *tabController = [[UITabBarController alloc] init]; 
      NSMutableArray *tabsArray = [[NSMutableArray alloc] init]; 
      `enter code here`ViewController *homeScreen = [[ViewController alloc] init]; 
      homeScreen.navigationItem.title = @"App Title"; 
      _appNavigation = [[UINavigationController alloc]initWithRootViewController:homeScreen]; 
      _appNavigation.tabBarItem.title = @"Home"; 
      [_appNavigation.tabBarItem setImage:[UIImage imageNamed:@"Home_Button.png"]]; 
      [tabsArray addObject:_appNavigation]; 
      [_appNavigation release]; 

      BookmarksViewController *bookMark = [[BookmarksViewController alloc] init]; 
      bookMark.navigationItem.title = @"Bookmarks"; 
      _appNavigation = [[UINavigationController alloc] initWithRootViewController:bookMark]; 
      [_appNavigation.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:2]; 
      _appNavigation.tabBarItem.title = @"Bookmarks"; 
      [tabsArray addObject:_appNavigation]; 
      [_appNavigation release]; 

      AppSettingsController *settings = [[AppSettingsController alloc] initWithStyle:UITableViewStyleGrouped]; 
      settings.navigationItem.title = @"Settings"; 
      _appNavigation = [[UINavigationController alloc] initWithRootViewController:settings]; 
      _appNavigation.tabBarItem.title = @"Settings"; 
      [_appNavigation.tabBarItem setImage:[UIImage imageNamed:@"Settings_Button.png"]]; 
      [tabsArray addObject:_appNavigation]; 
      [_appNavigation release]; 

      SearchViewController *searchView = [[SearchViewController alloc] init]; 
      searchView.navigationItem.title = @"Ranga"; 
      _appNavigation = [[UINavigationController alloc] initWithRootViewController:searchView]; 
      _appNavigation.tabBarItem.title = @"Search"; 
      [_appNavigation.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemSearch tag:4]; 
      [tabsArray addObject:_appNavigation]; 
      [_appNavigation release]; 


      tabController.viewControllers = tabsArray; 


LoginViewController *loginView = [[LoginViewController alloc] init]; 

      [self.window addSubview:tabController.view]; 

[tabController presentModalViewController:loginView animated:NO]; 

    [self.window makeKeyAndVisible]; 
    return YES; 
} 


@end 
+0

請張貼您的代碼。 –

+0

我已經採取單視圖應用程序出故事板,我有xib文件there.i分別添加3個視圖控制器我有.h .m和.xib文件。 – user1986984

回答

1

只需更新您的應用程序代碼這條路。

1碼變化

開放AppDelegate.h文件應該像

#import <UIKit/UIKit.h> 

@class FirstViewController,SecondViewController; 


@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate> 


@property (strong, nonatomic) UIWindow *window; 

//these two view controllers (screens) for adding to tabs. 
@property (strong, nonatomic) FirstViewController *firstViewController; 
@property (strong, nonatomic) SecondViewController *secondViewController; 

//this will be the tab bar-controller 
@property (strong, nonatomic) UITabBarController *baseTabBarController; 

@end 

現在您AppDelegate.m文件中的代碼應該是這樣

#import "AppDelegate.h"  
#import "FirstViewController .h" 
#import "SecondViewController.h" 


@implementation AppDelegate 

@synthesize window = _window; 
@synthesize firstViewController; 
@synthesize secondViewController; 
@synthesize baseTabBarController; 



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

    // create tab bar 
    baseTabBarController = [[UITabBarController alloc]init]; 

    baseTabBarController.delegate=self; 

    // initialize first screen 
    firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController " bundle:nil]; 
    UINavigationController *homeView = [[UINavigationController alloc]initWithRootViewController:firstViewController]; 
    firstViewController.title = @"Home "; 

    // initialize second screen 
    secondViewController = [[SecondViewController alloc] initWithNibName:@"FavViewController" bundle:nil]; 
    secondViewController.title = @"My Favourite"; 
    UINavigationController *favouriteView = [[UINavigationController alloc] initWithRootViewController:secondViewController]; 

    //add both views in array 
    NSArray *controllers = [NSArray arrayWithObjects:homeView,favouriteView, nil]; 
    //add array to tab bar 
    baseTabBarController.viewControllers = controllers; 
    //add tab bar to window 
    self.window.rootViewController = baseTabBarController; 


    [self.window makeKeyAndVisible];  

    return YES; 
} 

2. in U我改變

現在去廈門國際銀行文件爲我們要添加TAB兩個視圖控制器, 只是改變這種方式在Interface Builder

底酒吧=標籤欄

enter image description here

就是這樣!您將在應用程序的底部獲得兩個選項卡。 enter image description here

隨意問我是否有任何問題。

+0

謝謝你我知道了。我有一個小小的懷疑。第六個bottonbar沒有設置爲tabbar,我也可以查看tabbar添加。那麼它的用途是什麼? – user1986984

+0

@ user1986984在這裏很棒:)如果我的答案幫助了你,PL接受答案(在左邊打一個綠色的刻度標記),它的工作原理是在IB中不添加tabbar,但它會干擾你的UI項目,因爲它會在運行時以編程方式添加TabBar,所以我們在添加TabBar時設計我們的佈局,因此它在運行時會與我們一樣設計整個U – swiftBoy

+0

好的謝謝你的信息.. – user1986984

相關問題