为什么这个UIToolBar不会出现?

时间:2013-06-27 08:00:46

标签: ios objective-c uitoolbar

这是导航控制器的根视图控制器的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;
}

这是app delegate中的委托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;
}

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

3 个答案:

答案 0 :(得分:0)

因为你没有打电话给它!

您正在致电

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

你应该在哪里打电话

BOHomeViewController* hvc = [[BOHomeViewController alloc] initWithNibName:<"Some Name"> bundle:<"Some Bundle">];

答案 1 :(得分:0)

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

答案 2 :(得分:0)

据我所见,initWithNibName没有被召唤。 Yoc致电initWithStlye

即便如此 - 在那个时间点你的hvc不是导航层次结构的一部分。意味着self.navigationController在init期间应为nil。一旦将其作为rootviewcontroller分配给后来新创建的UINavigationController,它就会获得值。