简体   繁体   中英

how replace self.navigationItem.leftBarButtonItem = self.editButtonItem() } with button action

I replace BarButtonItem's self.editButtonItem() with button @IBAction..

override func viewDidLoad() {
    super.viewDidLoad()

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

I delete self.navigationItem.leftBarButtonItem = self.editButtonItem() above code

I wanna replace @IBAction button () { code }...

what's code?

let barButton = UIBarButtonItem(title: "Button", style: .Plain, target: self, action: "buttonAction:")
self.navigationItem.leftBarButtonItem = barButton

@IBAction func buttonAction(sender:AnyObject?)
{
     //do your thing
}

If you want to set the leftBarButtonItem action to a function, you can do this:

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationItem.leftBarButtonItem?.action = "functionName:"
}

func functionName(sender: AnyObject!) {
    //Do something
}

EDITED: If you want to set the tableview to edit mode, use the following:

@IBOutlet weak var editButton: UIButton!

@IBAction func doEdit(sender: AnyObject) {

    if (self.tableView.editing) {
        editButton.title = "Edit"
        self.tableView.setEditing(false, animated: true)
    } else {
        editButton.title = "Done"
        self.tableView.setEditing(true, animated: true)
    }
}

My Solution is this. Connect the nav button outlet action to this function

@IBAction func startEditBtnAct(_ sender: Any) {
        tableview.isEditing = !tableview.isEditing

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM