开发者

How can i identify self.editButtonItem button's clicked event?

I set my left bar button of UINavigationController as edit button using the code

leftBarButton = self.editButtonItem;

I want to change some disable/enable properties of other but开发者_运维知识库tons with respect to the edit button's click action.

How can i find whether the Edit button is pressed or not?


The edit button's action sends your view controller the setEditing:animated message. Override this in your subclass to perform other actions when entering or leaving edit mode.

Be sure to call the super implementation at the end to manage the rest of the transition to editing view.


So finally i got the solution...

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

}

This method will be called with out changing the original behavior of self.editButtonItem button.


In 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)
}


In Swift you can follow the below methods:

  @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)


    }


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
}

updated:

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

will be called

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜