导航栏按钮未显示

时间:2013-07-02 10:20:06

标签: iphone ios uinavigationcontroller uibutton uinavigationitem

我有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];

4 个答案:

答案 0 :(得分: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的{​​{1}}:

-viewWillAppear

答案 1 :(得分:1)

我认为这里的问题是导航控制器。您无法将其添加为UIViewControllers视图的子视图。我正在使用故事板,您可以使用导航控制器嵌入View控制器。

在这里,您的“nav”不知道它应该关心您的navigationItem。您的视图控制器不在他的viewControllers列表中。

答案 2 :(得分:0)

我不认为添加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]];

希望这会对你有所帮助.. :)

答案 3 :(得分:-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];

更新:

使用以下代码

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;