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

サンプルコード:

- (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 に答える
1

ここでの問題はナビゲーションコントローラーにあると思います。UIViewControllers ビューのサブビューとして追加することはできません。私はストーリー ボードを使用しており、View Controller を Navigation Controller に埋め込むことができます。

ここで、「nav」は、navigationItem を気にする必要があることを知りません。あなたのView Controllerは彼のviewControllersリストにありません。

于 2013-07-02T10:42:01.033 に答える
-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 に答える