我不認爲添加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]];
希望這會幫助你.. :)
這不是創建UINavigationController的正確方法。查看教程和文檔,並在這裏http://simplecode.me/2011/09/04/an-introduction-to-uinavigationcontroller/。 – iCoder
http://stackoverflow.com/questions/15379131/how-to-add-2-buttons-on-navigation-bar/15379448#15379448 – Ravindhiran
NavigationBar只顯示。但該按鈕不可見。我不是爲什麼? – user2474320