0

ナビゲーションコントローラーがあります。上部のナビゲーション バーではなく、ツールバーの下部に編集ボタンを配置したいと考えています。

 self.navigationItem.leftBarButtonItem = self.editButtonItem;

私が抱えている問題は、編集ボタンを下部ツールバーに追加するときです。このような:

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source
    [self.christmasGifts removeGiftAtIndexPath:indexPath];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}   
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {

[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:YES];

//Do not let the user add if the app is in edit mode.
if(editing)
    self.navigationItem.rightBarButtonItem.enabled = YES;
else
    self.navigationItem.rightBarButtonItem.enabled = YES;
 }

次に、編集ボタン setEditing: をリンクします。ツールバーには、上部ナビゲーションの [編集] ボタンのような完了ボタンは表示されません。それは同じままです。

ただし、アイテムを削除することはできますが、下のボタンで状態を通常に戻すことはできません。

このナビゲーション コントローラーから前のコントローラーに戻れるようにする必要があります。しかし、編集ボタンは戻るボタンを隠します。

ここに画像の説明を入力 ここに画像の説明を入力 ここに画像の説明を入力 ここに画像の説明を入力

編集。

コードでツールバーを追加できることはわかっています。

UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
                           initWithTitle:@"Edit"
                           style:UIBarButtonItemStyleBordered
                           target:self
                           action:@selector(setEditing:)];

[[UIBarButtonItem appearance] setTintColor:[UIColor colorWithRed:70/255.0f green:155/255.0f blue:19/255.0f alpha:1.0]];

NSArray *arrBtns = [[NSArray alloc]initWithObjects:editButton,anotherButton, nil];
self.toolbarItems = arrBtns;

[self.navigationController setToolbarHidden:NO];

あるいは

  UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 500, 400, 40)];
  [self.tableView addSubview:toolBar];
4

2 に答える 2

2

あなたのViewController.hで、宣言します (そして でそれらを接続しますXIB)

@property (strong, nonatomic) IBOutlet UIToolbar *toolbar;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *toolbarbutton;

- (IBAction)toolbarbuttonTouched:(id)sender; // Connect with UIBarButtonItem in XIB

ViewController.mに、次の関数を貼り付けます。

- (IBAction)toolbarbuttonTouched:(id)sender
{
    if ([self.toolbarbutton.title isEqualToString:@"Edit"])
    {
        [self.tableView setEditing:YES animated:YES];
        self.toolbarbutton.title = @"Done";
    }
    else
    {
        [self.tableView setEditing:NO animated:YES];
        self.toolbarbutton.title = @"Edit";
    }
}

これで準備完了です。

ポイント

  1. プロパティUIBarButtonItemの初期値はtitle「編集」に設定する必要があります
  2. コードはeditingStyle == UITableViewCellEditingStyleDelete完全に機能するはずです

編集

次のコードは必要ありません。

self.navigationItem.leftBarButtonItem = self.editButtonItem;

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {

[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:YES];

//Do not let the user add if the app is in edit mode.
if(editing)
    self.navigationItem.rightBarButtonItem.enabled = YES;
else
    self.navigationItem.rightBarButtonItem.enabled = YES;
 }

それがあなたのために働くかどうか、私に知らせてください。

于 2013-04-09T11:19:50.907 に答える