2

使用 XCode 4.5 & iOS6

我创建了一个带有 UITabBar (NIB) 的 UINavigationController,并且第一次启动的选项卡垂直定位不正确。当您单击第二个选项卡并再次单击第一个选项卡时,垂直定位就可以了。

所以......第一次运行完成后如何正确定位第一个选项卡?

查看错误的定位:

http://img231.imageshack.us/img231/2159/badbf.png

我的代码:

AppDelegate.h

@interface bib_AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

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

在 AppDelegate.m

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

    // change defaul selected icon tabbar color to orange
    [[UITabBar appearance] setSelectedImageTintColor:[UIColor orangeColor]];

    // Override point for customization after application launch.
    UIViewController *viewController1 = [[agendaViewController alloc] initWithNibName:@"agendaViewController" bundle:nil];
    UIViewController *viewController2 = [[messagesViewController alloc] initWithNibName:@"messagesViewController" bundle:nil];

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[viewController1, viewController2];

    self.mainControllercode = [[UINavigationController alloc] initWithRootViewController:self.tabBarController];
    self.window.rootViewController = self.mainControllercode;

    [self.window makeKeyAndVisible];
    return YES;
}

议程视图控制器.h

#import <UIKit/UIKit.h>

@interface agendaViewController : UIViewController
@end

议程视图控制器.m

#import "agendaViewController.h"

@interface agendaViewController ()

@end

@implementation agendaViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    self.title = NSLocalizedString(@"Agenda", @"Agenda");
    self.tabBarItem.image = [UIImage imageNamed:@"83-calendar"];
}
return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

编辑1:

我用你可以看到的 Storyboards 创建了一个示例项目。我想在没有 Storyboard 的情况下拥有相同的功能,请在此处下载:

http://www.freefilehosting.net/atestsb

谢谢

4

1 回答 1

1

你正在以错误的方式解决这个问题。

UITabBarController 应该有一个 UINavigationControllers 的集合,然后将它们的根控制器设置为主 Nib。然后每个选项卡处理自己的导航堆栈。

您当前将 UITabBarController 放在 UINavigationController 的根目录中。当您在导航堆栈中移动时,这将导致问题以及删除标签栏。

查看此链接以获取更多详细信息以编程方式处理它:

http://www.xdracco.net/howto-implement-uinavigationcontroller-uitabbarcontroller-programmatically/

于 2012-11-01T14:56:03.400 回答