我不认为将 UINavigationController 添加为子视图对您有用。相反,您可以在 didFinishLaunchingWithOptions 方法中的 AppDelegate 文件中将 UINavigationController 作为子视图(或作为 rootviewcontroller)添加到 UIWindow,如下所示:
- (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]];
希望这会对你有所帮助.. :)