2011-09-07 34 views
17

tôi đặt nút thanh bên trái của tôi về UINavigationController như chỉnh sửa nút sử dụng mãLàm cách nào để tôi có thể xác định sự kiện được nhấp của nút self.editButtonItem?

leftBarButton = self.editButtonItem; 

tôi muốn thay đổi một số tắt/bật tính chất của các nút khác liên quan đến hành động nhấp chuột vào nút chỉnh sửa với.

Làm cách nào tôi có thể tìm thấy nút Chỉnh sửa có được nhấn hay không?

Trả lời

31

Hành động của nút chỉnh sửa gửi trình điều khiển chế độ xem của bạn thông báo setEditing:animated. Ghi đè lên điều này trong lớp con của bạn để thực hiện các tác vụ khác khi vào hoặc rời khỏi chế độ chỉnh sửa.

Đảm bảo gọi triển khai super ở cuối để quản lý phần còn lại của quá trình chuyển đổi sang chế độ xem chỉnh sửa.

+0

Cảm ơn thông tin của bạn ... Nhưng, nếu tôi gõ 'setEditing: Phương pháp animated' , hình động và nút trừ màu đỏ không xuất hiện – Confused

+0

Bạn phải gọi '[super setEditing: editing animated: animated]' ở cuối ghi đè của bạn. – jrturton

+0

Cảm ơn .. nó hoạt động ... – 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 
} 

Cập nhật:

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

sẽ được gọi

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

Cảm ơn vijay .. Nhưng tôi muốn biết kịch bản cho nút self.editButtonItem – Confused

18

Vì vậy, cuối cùng tôi đã nhận các giải pháp ...

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

} 

Phương pháp này sẽ được gọi ra ngoài thay đổi hành vi ban đầu của nút self.editButtonItem.

+0

Phương pháp thứ hai không bao giờ được sử dụng. –

7

Trong Swift:

@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

Trong Swift bạn có thể làm theo phương thức dưới đây:

@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

nghiêm túc, một chi nhánh 'if (editing)' khi bạn có thể vừa nói 'tableView.setEditing (chỉnh sửa, hoạt ảnh: hoạt hình)' và nó có hoạt động cho cả hai trường hợp không? – Alnitak

+0

nó hoạt động nhưng về mặt kỹ thuật u là chính xác nhờ đề xuất –

+0

Hoạt động độc đáo trên UITableView bên trong UIViewController. – yoshitech

Các vấn đề liên quan