iOS Programming Cookbook
上QQ阅读APP看书,第一时间看更新

How it works...

First, we created the MasterViewController, which is the root view controller. We made it a subclass of UITableViewController to display a table view with list of cells, and when you select any cell, navigation controller will push a new DetailViewController as a details view.

In MasterViewController, we added a right bar button in the right item that we have explained earlier. First, we create a new UIBarButtonItem and set in the navigationItem.rightBarButtonItem. This adds a right button at the right and will be visible only to MasterViewController and not to all controllers in the navigation stack. The action of the right button will insert a new cell in the table view.

Since we added a segue from the cell to DetailViewController, once you select any cell from the table, a new DetailViewController will be pushed. In most cases, you will need to pass some information for the newly opened view controller, and in our case, we need to pass the selected item to DetailViewController. The prepareForSegue method is the best place to do so, and you can differentiate between segues (if you have multiple segues) using their identifiers. When you open DetailViewController, you will see a back button automatically added in the navigation bar to go back to the preceding view controller.