2013-07-02 169 views
0

我有UINavigationController。然後我需要顯示導航欄按鈕。我用下面的代碼:導航欄按鈕不顯示

UINavigationController *nav=[[UINavigationController alloc] init]; 

[self.view addSubview:nav.view]; 

UIImage *info_iphone=[UIImage imageNamed:@"button.png"]; 
UIButton *infobtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 48, 30)]; 
[infobtn setBackgroundImage:info_iphone forState:UIControlStateNormal]; 
[infobtn addTarget:self action:@selector(show_info:) forControlEvents:UIControlEventTouchUpInside]; 

self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:infobtn]; 
+2

這不是創建UINavigationController的正確方法。查看教程和文檔,並在這裏http://simplecode.me/2011/09/04/an-introduction-to-uinavigationcontroller/。 – iCoder

+0

http://stackoverflow.com/questions/15379131/how-to-add-2-buttons-on-navigation-bar/15379448#15379448 – Ravindhiran

+0

NavigationBar只顯示。但該按鈕不可見。我不是爲什麼? – user2474320

回答

1

示例代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.viewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil]; 
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 
    self.window.rootViewController = navController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

YourViewController-viewWillAppear

UIImage *info_iphone=[UIImage imageNamed:@"button.png"]; 
UIButton *infobtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 48, 30)]; 
[infobtn setBackgroundImage:info_iphone forState:UIControlStateNormal]; 
[infobtn addTarget:self action:@selector(show_info:) forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:infobtn]; 
self.navigationItem.rightBarButtonItem = rightButton; 
+0

它不工作 – user2474320

+0

@ user2474320:它不會工作方法是錯誤的。你有沒有在Appdelegate.m中添加該行? – Bhavin

+0

在裏面工作 - 查看會出現。謝謝 – user2474320

-1

使用該婁代碼...

UIImage *info_iphone=[UIImage imageNamed:@"button.png"]; 
UIButton *infobtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 48, 30)]; 
[infobtn setBackgroundImage:info_iphone forState:UIControlStateNormal]; 
[infobtn addTarget:self action:@selector(show_info:) forControlEvents:UIControlEventTouchUpInside]; 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:infobtn]; 
[infobtn release]; 

UPDATE:

使用婁代碼

UIBarButtonItem *btnSave = [[UIBarButtonItem alloc]init]; 
btnSave.target = self; 
btnSave.action = @selector(btnSave_Clicked:); 
btnSave.title = @"Save"; 
btnSave.style=UIBarButtonItemStyleDone; 
self.navigationController.topViewController.navigationItem.rightBarButtonItem = btnSave; 
+0

@ user2474320嘿嘗試我更新的代碼...:) –

+0

@ user2474320設置此行'nav.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infobtn];'並發表評論,它的工作與否.. :) –

+0

@ user2474320使用上面更新的代碼可能會工作我希望...使用'UIBarButton' –

1

我覺得這裏的問題是導航控制器。您不能將其添加爲UIViewControllers視圖的子視圖。我正在使用故事板,並且您可以使用導航控制器嵌入視圖控制器。

在這裏,你的「導航」不知道它應該關心你的navigationItem。你的視圖控制器不在他的viewControllers列表中。

0

我不認爲添加UINavigationController作爲子視圖會爲你工作。 相反,你可以添加的UINavigationController作爲子視圖(或RootViewController的)在你的AppDelegate文件的UIWindow在didFinishLaunchingWithOptions方法如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ 

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 

    // Override point for customization after application launch. 

    self.viewController = [[[SampleViewController alloc] initWithNibName:@"SampleViewController" bundle:nil] autorelease]; 

    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:self.viewController]; 

    self.window.rootViewController = nav; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

比你SampleViewController的viewDidLoad方法中添加BarButton如下:

UIImage *info_iphone=[UIImage imageNamed:@"button.png"]; 
UIButton *infobtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 48, 30)]; 
[infobtn setBackgroundImage:info_iphone forState:UIControlStateNormal]; 
[infobtn addTarget:self action:@selector(show_info:) forControlEvents:UIControlEventTouchUpInside]; 

[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc]initWithCustomView:infobtn]]; 

希望這會幫助你.. :)