0

ユーザーがナビゲーション バーのボタンをクリックしたときに呼び出されるメソッドがあります。このようにボタンを追加すると、メソッドが呼び出されます。

  UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"myEditButton"]
                                                                   style:UIBarButtonItemStyleBordered
                                                                  target:self 
                                                                  action: @selector(enterEditMode:)];
  editButton.title = @"Edit";
  [self.navigationItem setRightBarButtonItem:editButton animated:YES];

ユーザーがボタンをクリックすると、enterEditMode: メソッドが呼び出されます。

ただし、このコードを使用すると、結果は添付ファイルのようになります。テキストはありませんが、画像が青いボタンの上に表示されます。標準のシステム編集ボタンは、青ではなく黒にする必要があるため使用できません。AFAIA 標準の編集バー ボタンの色を黒に変更することはできませんか?

そのため、xib で、ボタン画像である UIImageView とテキストの UILabel を含む親 UIView を作成しました。次に、次のようなボタン アイテムを作成します。

UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithCustomView:self.editButtonParentView];
[editButton setTarget:self];
[editButton setAction: @selector(enterEditMode:)];
editButton.style = UIBarButtonItemStyleBordered;
editButton.target = self;
self.editButtonLabel.text = @"Edit"
[self.navigationItem setRightBarButtonItem:editButton animated:YES];

editButtonParentView は nib の partent ビューへの IBOutlet です。

これは完全に表示されますが、ユーザーがクリックしても enterEditMode: が呼び出されないという問題があります。

なぜ呼び出されないのですか?

ありがとう

ここに画像の説明を入力

4

1 に答える 1

0

このリンクは、システムの [編集] ボタンと同じ外観で、必要な背景色を取得する方法を見つけた場所です。このコードを試してください:

// から:

#import "UIBarButtomItem+Tinted.h"

@implementation UIBarButtonItem (Tinted)

+ (UIBarButtonItem *)newBarButtonItemWithTint:(UIColor*)color andTitle:(NSString*)itemTitle andTarget:(id)theTarget andSelector:(SEL)selector
{
    UISegmentedControl *button      = [[UISegmentedControl alloc] initWithItems:@[itemTitle]];
    button.momentary                = YES;
    button.segmentedControlStyle    = UISegmentedControlStyleBar;
    button.tintColor                = color;

    [button addTarget:theTarget action:selector forControlEvents:UIControlEventValueChanged];
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}

@end
于 2012-08-06T18:06:59.263 に答える