2013-06-27 193 views
0

這裏是我的導航控制器的根視圖控制器,我想顯示在工具欄上的init方法:爲什麼不會出現這個UIToolBar?

-(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; 
} 

工具欄根本沒有顯示出來。有人能指出我在做錯什麼嗎?非常感激。

回答

0

移動你toolbarItems的初始化到你實際上調用(initWithTableViewStyle:)初始化和self.navigationController.toolbarHidden = NO;到viewWillAppear中爲self.navigationController會在你的初始者中無效。

+0

謝謝,這工作。 –

0

因爲你不叫它!

要調用

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

,你應該叫

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

據我所知,initWithNibName沒有叫。優酷電話initWithStlye

即使在那時 - 您的hvc不是導航層級的一部分。在init中,self.navigationController的含義應該爲零。只要將它作爲rootviewcontroller分配給後來新創建的UINavigationController,它就會獲得值。

相關問題