2009-07-01 49 views
24

在我的表视图控制器有iPhone,在表视图钩子编辑/完成按钮点击

self.navigationItem.leftBarButtonItem = self.editButtonItem; 

这在左上角产生一个普通的编辑/完成按钮。因此,一旦用户点击“编辑”,按钮标题变为“完成”,并且表项可能被删除或重新排序。一旦用户实际点击“完成”,我希望收到通知。有没有钩?

背景:我想坚持条目的顺序,即下次用户拉起这个视图时,我想以最近最少使用的顺序来呈现条目。

回答

43

对于那些谁仍然在这个问题interesed(或回答:P)

UITableView API

显示有一个- (void)setEditing:(BOOL)editing animated:(BOOL)animate方法 每次按下该编辑/完成按钮时都会调用这些方法。你必须简单地通过(BOOL)editing参数检查一个被使用。最后但并非最不重要的是,您必须从最初的编辑/完成按钮调用正确的方法。

只是这种方法添加到您的UITableView类

- (void)setEditing:(BOOL)editing animated:(BOOL)animate 
{ 
    [super setEditing:editing animated:animate]; 
    if(editing) 
    { 
     NSLog(@"editMode on"); 
    } 
    else 
    { 
     NSLog(@"Done leave editmode"); 
    } 
} 
2

这是当一个栏按钮已被推到得到通知的标准方式:

self.editButtonItem.target = self; 
self.editButtonItem.action = @selector(buttonPushed:); 

... 

- (void) buttonPushed:(id)sender 
{ 
// do stuff here 
} 
+0

嗯,我知道。关键是我不想干涉按钮的功能(修改列表条目,切换其标题等)。我只是想知道在“完成”状态下点击它的时间。 – 2009-07-01 20:26:18

+1

UIBarButtonItems不会从UIControl派生出来,因此您不能只向其添加另一个目标。您始终可以捕捉动作并自行维护状态。这并不难。或者,按下按钮(上图),然后设置一个“忽略”标志,然后转回并合成一个一次性事件回到按钮,让它做它的事情。有关合成触摸事件的详细信息,请参阅:http://cocoawithlove.com/2008/10/synthesizing-touch-event-on-iphone.html – Ramin 2009-07-01 22:08:17

0

它是可以改变的动作。点击编辑按钮后,显示删除按钮,而不是显示拒绝/验证/修改按钮。并改变相应的行动,而不是删除的选项

感谢 Mindus

3

对于那些不希望覆盖的UITableView谁(例如,如果你使用的UITableViewController),下面是我用一个简单和干净的解决方案。它基本上涉及创建您自己的编辑按钮项目,并使用tableView的editing标志来跟踪编辑与完成。对于结冰,当表格为空时添加新项目时会显示一个“+”按钮(而不是“编辑”)。

- (void) updateEditButtonVisibility 
{ 
    // tableItems represents the data structure that s 
    if ([tableItems count] > 0) 
    { 
     UIBarButtonSystemItem editButtonType = self.tableView.editing ? UIBarButtonSystemItemDone : UIBarButtonSystemItemEdit; 
     UIBarButtonItem *editButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:editButtonType 
                         target:self 
                         action:@selector(editButtonTouched)]; 

     self.navigationItem.rightBarButtonItem = editButtonItem; 
     [editButtonItem release]; 
    } 
    else 
    { 
     UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                         target:self 
                         action:@selector(addButtonTouched)]; 
     self.navigationItem.rightBarButtonItem = addButtonItem; 
     [addButtonItem release]; 
    } 
} 

- (void) editButtonTouched 
{ 
    // edit/done button has been touched 

    [self.tableView setEditing:!self.tableView.editing animated:YES]; 
    [self updateEditButtonVisibility]; 
} 

- (void) addButtonTouched 
{ 
    // logic to allow user to add new items goes here 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    [self updateEditButtonVisibility]; 
} 
相关问题