Reputation: 2612
I am familiar with this code: self.navigationItem.leftBarButtonItem = self.editButtonItem
.
See this code:
class ThirdViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tblTasks: UITableView! // declaring this allows you to reload the tableView to update your items
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
I have the code that allows for the edit button to be present, yet no edit button is to be seen in my app nor the storyboard.
I am a Swift beginner, if someone could point me in the right direction that would be awesome.
Upvotes: 3
Views: 2205
Reputation: 481
If you add it into navigationItem
you should embed that view controller into UINavigationController
to see that button.
You will not see that button in Storyboard because you set it in code. In Storyboard you setup only initial state of the controller.
How to setup editing mode in UIViewController
subclass:
There is editButtonItem
variable in UIViewController
. If you add it on screen, setEditing(_:,animated:)
will be triggered when user tap that button.
Also it will change button text between Edit
and Done
according to ViewController's isEditing
state.
You can get current state from self.isEditing
Do not forget to call super.setEditing(_:animated:)
, or it will not change state of the button and view controller.
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = self.editButtonItem
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
// custom code to handle edit mode
}
editButtonItem (iOS 10.0+)
isEditing (iOS 2.0+)
setEditing(_:animated:) (iOS 2.0+)
Upvotes: 2
Reputation: 11435
You can create your own edit button, like:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit", style: .Plain, target: self, action: Selector("editTableView"))
And then:
func editTableView()
{
if self.navigationItem.rightBarButtonItem.title == "Edit"
{
self.navigationItem.rightBarButtonItem.title = "Done"
//edit your tableView Here!
}
else
{
self.navigationItem.rightBarButtonItem.title = "Edit"
//when youre finishing editing
}
}
Upvotes: 0
Reputation: 5939
The stock "Edit" button is only available on UITableViewController
. For other view controller types you'll have to create a button yourself.
Upvotes: 5