2

カスタム ナビゲーション バー ボタン項目を実装しようとしています。ユーザーが項目をクリックして関数が呼び出されると、セレクターがあります。バー項目を設定するUIBarButtonItemと問題なく動作しますが、境界線と適切なサイズのないボタンにカスタム イメージを使用する必要があります。

だからviewdidloadで私は電話する

- (void)viewDidLoad 
{
    [super viewDidLoad];
    //gear button on navigation Bar
    UIImage* imageback2 = [UIImage imageNamed:@"ICON - Gear-BarstyleItem@2x.png"];
    CGRect frameimgback2 = CGRectMake(0, 0, 40, 40);

    UIButton *backButton2 = [[UIButton alloc] initWithFrame:frameimgback2];
    [backButton2 setBackgroundImage:imageback2 forState:UIControlStateNormal];
    [backButton2 addTarget:self
                    action:@selector(setColorButtonTapped:)
          forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithCustomView:backButton2];


    UIBarButtonItem *btn3 =[[UIBarButtonItem alloc] initWithImage:imageback2 style:UIBarButtonItemStylePlain target:self action:@selector(setColorButtonTapped:)];

    self.navigationItem.rightBarButtonItem = btn2;
}

#pragma mark Callbacks

- (IBAction)setColorButtonTapped:(id)sender{
    if (_colorPicker == nil) {
        self.colorPicker = [[ColorPickerController alloc] initWithStyle:UITableViewStylePlain];
        _colorPicker.delegate = self;
        self.colorPickerPopover = [[UIPopoverController alloc] initWithContentViewController:_colorPicker];
    }
    [self.colorPickerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

これは私に与えます:

-[UIButton view]: unrecognized selector sent to instance 0x9466620
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton view]: unrecognized selector sent to instance 0x9466620'

私が設定した場合

 self.navigationItem.rightBarButtonItem = btn2;

 self.navigationItem.rightBarButtonItem = btn3;

正常に動作しますが、ボタンには境界線と背景色があり、画像のサイズが小さくなっています

では、なぜエラーが発生し、このボタンをカスタムボタンにするにはどうすればよいでしょうか?

編集:

次のようなvoid関数を追加すると

- (void)gearTapped{
    [self setColorButtonTapped:self];
}

ボタンでセレクターを変更してもエラーは発生しませんが、ポップアップが画面の中央に1行だけ表示されます

[backButton2 addTarget:self
                    action:@selector(gearTapped)
          forControlEvents:UIControlEventTouchUpInside];

ここに画像の説明を入力

通常はこのように動作するはずですが、カスタムボタンを使用します ここに画像の説明を入力

4

3 に答える 3

6

アクションの引数UIButtonとしてインスタンスを渡しています。次に、ここでインスタンスを期待するメソッドにパラメーターとしてsender渡します。senderUIBarButtonItem

[self.colorPickerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

アクションで引数を使用する場合はsender、オブジェクトの 2 つの異なるクラスに対して個別のアクションを作成する必要があります。お気に入り:

- (void)loadColorPicker
{
    if (_colorPicker == nil) {
        self.colorPicker = [[ColorPickerController alloc] initWithStyle:UITableViewStylePlain];
        _colorPicker.delegate = self;
        self.colorPickerPopover = [[UIPopoverController alloc] initWithContentViewController:_colorPicker];
    }
}
- (void)colorBarButtonItemTapped:(UIBarButtonItem *)sender
{
    [self loadColorPicker];
    [self.colorPickerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

- (void)colorButtonTapped:(UIButton *)sender
{
    [self loadColorPicker];
    [self.colorPickerPopover presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

次に、ターゲットとアクションを割り当てる場所:

[backButton2 addTarget:self
                action:@selector(colorButtonTapped:)
      forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *btn3 =[[UIBarButtonItem alloc] initWithImage:imageback2 style:UIBarButtonItemStylePlain target:self action:@selector(colorBarButtonItemTapped:)];

お役に立てれば!

于 2012-09-26T18:20:34.763 に答える
0
UIView* btnView = [[UIView alloc] initWithFrame: backButton2.bounds];
[btnView addSubview: backButton2];

UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithCustomView:btnView];

//in case you are not using arc
[btnView release];

乾杯...

于 2012-09-26T16:41:05.483 に答える
0

あなたのエラーはおそらく、/UIButtonを使用して初期化するのではなく、設計された初期化子を使用して初期化する必要があるためです(そして、そのフレームを個別に設定します)。allocinitWithFramebuttonWithType:

于 2012-09-26T16:43:25.327 に答える