0

这是我试图在其上显示工具栏的导航控制器的根视图控制器的 init 方法:

-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nil bundle:nil];
    if(self){
        //GUI implementation
        self.navigationController.toolbarHidden = NO;
        UIBarButtonItem *flexiableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                                       target:self
                                                                                       action:nil];
        UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                                                                               target:self
                                                                               action:nil];
        self.toolbarItems = [NSArray arrayWithObjects:flexiableItem, item1, nil];

        UIBarButtonItem* addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                                   target:self
                                                                                   action:@selector(addEmployee)];

        self.navigationItem.rightBarButtonItem = addButton;
        self.navigationItem.leftBarButtonItem = self.editButtonItem;
    }
    return self;
}

这是我application:DidFinishLaunching在应用程序委托中的委托方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    BOHomeViewController* hvc = [[BOHomeViewController alloc] initWithStyle:UITableViewStylePlain];

    UINavigationController* navbar = [[UINavigationController alloc] initWithRootViewController:hvc];
    [self.window setRootViewController:navbar]; 

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

工具栏根本不显示。有人可以指出我做错了什么吗?非常感激。

4

3 回答 3

0

因为你不叫它!

你在打电话

BOHomeViewController* hvc = [[BOHomeViewController alloc] initWithStyle:UITableViewStylePlain];

你应该打电话的地方

BOHomeViewController* hvc = [[BOHomeViewController alloc] initWithNibName:<"Some Name"> bundle:<"Some Bundle">];
于 2013-06-27T08:27:30.800 回答
0

将您的toolbarItems 的初始化移动到您实际调用的初始化程序(initWithTableViewStyle:)和self.navigationController.toolbarHidden = NO;viewWillAppear,因为self.navigationController在您的初始化程序中将是nil。

于 2013-06-27T08:28:00.977 回答
0

据我所知,initWithNibName是没有叫的。约克打电话initWithStlye

即便如此 - 在那个时间点你的 hvc 不是导航层次结构的一部分。这意味着 self.navigationController 在初始化期间应该为零。一旦将它作为 rootviewcontroller 分配给后来新创建的UINavigationController.

于 2013-06-27T08:30:23.877 回答