2011-11-26 11 views
12

ナビゲーションバーの右側に2つのボタンの配列を追加しようとしていますが、コードを実行すると例外が発生します。navigationItem.rightBarButtonItemにUIButtonの配列を追加すると、NSInvalidArgumentExceptionが発生します。

'NSInvalidArgumentException'、理由は:「 - [UIButton isSystemItem]:認識されていないセレクタはインスタンスに送信

私のコードは本当に非常に単純です:私は、iPhone 5.0上でそれを実行しています

UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,45)]; 
    label.backgroundColor=[UIColor clearColor]; 
    label.text = @"Test 2 Buttons"; 

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button1.frame = CGRectMake(00.0f, 0.0f, 32.0f, 32.0f); 

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button2.frame = CGRectMake(00.0f, 0.0f, 32.0f, 32.0f); 

    NSArray *rightBarButtons = [[NSArray alloc] initWithObjects:button2, button1, nil]; 


    UINavigationItem* navItem = self.navigationItem; 
    navItem.titleView = label; 
    navItem.rightBarButtonItems = rightBarButtons; 
    [rightBarButtons release]; 
    [label release]; 

シミュレータ。 ありがとうございます。 Al

答えて

27

UIButtonsを直接追加することはできません。最初にUIBarButtonItemsとラップする必要があります。配列を渡すだけなので、コンパイラの警告は表示されません。

initWithCustomView:を使用してバーボタンアイテムを作成し、ボタンをカスタム表示として渡します。または、ボタンの内容に応じて、バーボタンアイテムを直接作成します。

+1

返信いただきありがとうございます。私はそれに気づいたはずです。とても単純です:-) – Alan

+0

'UIButton'を' UIBarButtonItem'に置き換えて動作します。 'initWithCustomView'で作成する必要はありません。 UIBarButtonItemsの配列を 'self.navigationItem.rightBarButtonItemS'に割り当て、最後に** S ** **を末尾に付けてください。 –

+0

@ proca2.0はい、したがって、**または、あなたのボタンに何があるかによって、バーボタンを作成してくださいアイテムを直接。 "** – jrturton

1

UIButtonオブジェクトではないオブジェクトUIBarButtonItemの配列を与えることを意図しています。 UIBarButtonItemはUIButtonもUIViewも継承しないことに注意してください。

10

ここでのトリックは、UIButtonオブジェクトの代わりにUIBarButtonItemオブジェクトを使用することです。 UIBarButtonItemsはそのようUIButtonsから作成することができます。UIBarButtonItemsはかなりそこに見えることを意味しているとき

UIBarButtonItem *myItem = [[UIBarButtonItem alloc] initWithCustomView:uibuttonObject]; 

しかし、ナビゲーションバーにUIButtonsを使用すると、一般的に悪い考えです。 UIBarButtonItem Class Referenceにアクセスすることを検討してください。

+0

実際にそれを綴ってくれてありがとう。 –

関連する問題