2011-09-07 88 views

回答

31

編輯按鈕的動作會向您的視圖控制器發送setEditing:animated消息。在您的子類中重寫此操作以在進入或退出編輯模式時執行其他操作。

確保在最後調用super實現來管理轉換到編輯視圖的其餘部分。

+0

感謝您的信息...但是,如果我使用'setEditing:animated'方法,動畫&紅色減號按鈕沒有出現 – Confused

+0

您必須在覆蓋結束時調用'[super setEditing:editing animated:animated]'。 – jrturton

+0

謝謝..它的工作原理... – Confused

1
UIBarButtonItem *barBut=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(doSomething)]; 


self.navigationItem.leftBarButtonItem=barBut; 

[barBut release]; 


.h 
-(void)doSomething; 

.m 

-(void)doSomething{ 

    NSLog(@"dooooooooooooo"); 
     //ur stuff 
} 

更新:

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

將被稱爲

editButtonItem 

Returns a bar button item that toggles its title and associated state between Edit and Done. 

- (UIBarButtonItem *)editButtonItem 

Discussion 
If one of the custom views of the navigationItem property is set to the returned object, the associated navigation bar displays an Edit button if editing is NO and a Done button if editing is YES. The default button action invokes the setEditing:animated: method. 

    Availability 
    Available in iOS 2.0 and later. 

    See Also 
    @property editing 

    – setEditing:animated: 


    Declared In 
    UIViewController.h 

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIViewController_Class/Reference/Reference.html

+0

謝謝維傑..但我想知道self.editButtonItem按鈕的場景 – Confused

18

所以最後我得到了解決辦法...

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

    [super setEditing:editing animated:animated]; 

    if(editing) { 
     //Do something for edit mode 
    } 

    else { 
     //Do something for non-edit mode 
    } 

} 

將調用此方法,但不更改self.editButtonItem按鈕的原始行爲。

+0

第二種方法不應該被使用。 –

7

在斯威夫特:

@IBOutlet weak var tableView: UITableView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    .... 
    self.navigationItem.leftBarButtonItem = self.editButtonItem() 
} 

override func setEditing(editing: Bool, animated: Bool) { 
    // Toggles the edit button state 
    super.setEditing(editing, animated: animated) 
    // Toggles the actual editing actions appearing on a table view 
    tableView.setEditing(editing, animated: true) 
} 
2

在斯威夫特,你可以按照下面的方法:

@IBOutlet weak var tableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     navigationItem.rightBarButtonItem = editButtonItem() 
    } 

    override func setEditing(editing: Bool, animated: Bool){ 

     super.setEditing(editing, animated: animated) 
     tableView.setEditing(editing, animated: true) 


    } 
+0

認真,當你可以剛纔說'tableView.setEditing(編輯,動畫:動畫)'''if(編輯)'分支並使它適用於兩種情況? – Alnitak

+0

它的工作原理,但技術上你是正確的,謝謝你的建議 –

+0

很好地在UIViewController裏面的UITableView。 – yoshitech