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];
4

4 回答 4

1

我认为这里的问题在于导航控制器。您不能将其添加为 UIViewControllers 视图的子视图。我正在使用故事板,您可以在其中嵌入带有导航控制器的视图控制器。

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

于 2013-07-02T10:42:01.033 回答
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;
于 2013-07-02T10:32:29.127 回答
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]];

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

于 2013-07-02T10:53:18.753 回答
-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;
于 2013-07-02T10:36:21.217 回答