0

カスタムビューがあり、UIButtonがあります。このビューをnavigationItemのrightbarButtonItemに追加します。問題ありませんが、このボタンにアクションを追加する必要があります。

これが私のコードです。

-(void)showMessage:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"next" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    [alert show];
}

- (void)viewDidLoad {
    UpDownButtonView *buttonView = [[UpDownButtonView alloc] initWithNibName:@"UpDownButtonView" bundle:nil];
    [buttonView.upButton addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];
    [buttonView.downButton addTarget:self action:@selector(prev:) forControlEvents:UIControlEventTouchUpInside];
    buttonView.view.frame = CGRectMake(0,0,95,34);
    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:buttonView.view]; 

    self.navigationItem.rightBarButtonItem = item;
    [self setTitle:@"Details"];

    [super viewDidLoad];
}

ありがとう。

4

1 に答える 1

1

UIButton を使用したカスタム ビューは必要ありません。必要なのは、UIBarButtonItem を割り当てて初期化し、ナビゲーション バーに配置することです。

コードは次のようになります。

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", @"Back Button") style:UIBarButtonItemStylePlain target:self action:@selector(doneButtonPressed:)];

self.navigationItem.leftBarButtonItem = backButton;
[backButton release];
于 2010-12-17T09:48:48.577 に答える