【问题标题】:Have to apply tabbar for single view application必须为单视图应用程序应用标签栏
【发布时间】:2013-02-01 01:19:48
【问题描述】:

我使用 xcode4.2 我在单视图应用程序中为标签栏添加了 3 个项目。

问题是我无法在 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

【问题讨论】:

  • 我在没有故事板的情况下采用了单视图应用程序,我在那里得到了 xib 文件。我分别添加了 3 个视图控制器,我得到了 .h .m 和 .xib 文件。

标签: iphone ios xcode4.2


【解决方案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。在 UI 变化中

现在为我们想要添加 TAB 的两个视图控制器转到 xib 文件, 只需在界面生成器中更改这种方式

底栏 = 标签栏

就是这样!!您将在应用程序底部获得两个选项卡。

如果您有任何问题,请随时问我。

【讨论】:

  • 谢谢你我明白了。我有一个小疑问。即使bottonbar没有设置为tabbar,我也可以查看添加的tabbar。那么它有什么用?
  • @user1986984 非常感谢 :),如果我的回答对您有帮助,PL 接受作为答案(在左侧打一个绿色的勾号),它可以在 IB 中不添加标签栏的情况下工作,但它会扰乱你的 UI 项目,因为它在运行时以编程方式添加 TabBar,所以我们通过添加 TabBar 来设计布局,因此它在运行时与我们设计整个 U 时相同
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-19
  • 1970-01-01
  • 1970-01-01
  • 2011-08-24
  • 2011-02-14
  • 2011-01-03
  • 1970-01-01
相关资源
最近更新 更多