2013-04-09 19 views
0

我有一個導航控制器。我想將編輯按鈕放在工具欄的底部而不是頂部導航欄中。在底部工具欄中編輯項Xcode和Objective C

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:在工具欄上它並不像顯示在頂部導航的編輯按鈕完成按鈕。它只是保持不變。

但是,您可以刪除某個項目,但無法使用底部按鈕將狀態重置爲正常。

我需要能夠從此導航控制器返回到前一個控制器。但編輯按鈕隱藏後退按鈕。

enter image description here enter image description here enter image description here enter image description here

編輯。

我知道我可以通過代碼添加工具欄。

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]; 
+0

您是否在使用'XIB創建ToolBar? – viral 2013-04-09 10:54:26

+0

我有一個故事板。我在構建器中創建了底部的工具欄和項目。但是你可以使用[self.tableView addSubview:toolBar];設置工具欄並查看我的編輯問題。 – 2013-04-09 11:02:35

+0

得到它的工作? – viral 2013-04-09 11:34:07

回答

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; 
} 

讓我知道,如果你的作品或沒有。

0

我能夠通過使用UIViewController的editButtonItem方法來做到這一點更簡單:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Place table view Edit button in toolbar, after existing buttons. 
    NSMutableArray *items = [self.toolbarItems mutableCopy]; 
    [items addObject:self.editButtonItem]; 
    [self setToolbarItems:items animated:YES]; 
} 

這獲取工具欄按鈕的現有陣列(如果有的話),使陣列的一個可變的副本,以便它可以改變。然後它將UIViewController的內置編輯按鈕添加到工具欄。

結果是一個功能正常的編輯/完成工具欄按鈕,可以打開和關閉編輯模式,而無需編寫自己的操作方法來處理該模式。

相關問題