Summary of UINavigationController
UINavigationController is a common Controller Used for view switching in IOS development. in object management, UINavigationController uses the stack method to manage the view levels. rootViewController is at the bottom layer of the stack. in addition, many methods are provided for switching and managing views.
Common methods include pushViewController and popViewController. Note that the attribute of the seue corresponding to UINavigationController must be set to push.
See the following code:
Var window: UIWindow? Func application (application: UIApplication, didfinishlaunchingwitexceptions launchOptions: NSDictionary ?) -> Bool {self. window = UIWindow (frame: UIScreen. mainScreen (). bounds) self. window !. BackgroundColor = UIColor. whiteColor () self. window !. MakeKeyAndVisible () // RootTableViewController defines a controller var root for tableView management as RootTableViewController () var navCtrl = UINavigationController (rootViewController: root) // you can specify to put the RootTableViewController at the bottom of the stack. Self. window !. RootViewController = navCtrl return true}Here, self. window !. RootViewController = navCtrl specifies the current rootViewController.
Create a WebView and push it to the management view of UINavigationController:
// Click override func tableView (tableView: UITableView!, called by a cell in tableView !, DidSelectRowAtIndexPath indexPath: NSIndexPath !) {// IndexPath indicates the row var row = indexPath of the cell to be clicked. row as Int var data = self. dataSource [row] as XHNewsItem // For inbound stack operations, var webView = WebViewController () webView. detailID = data. newsID self. navigationController. pushViewController (webView, animated: true )}The result of code execution above is: click a cell in tableView to create a new WebViewController and press it into the view stack of UINavigationController.
The content of the WebView is displayed on the UI. at the same time, the corresponding return button will be automatically displayed in the navigation bar. Clicking this button will complete the stack export operation for this WebView, and then roll back to the previous tableView.
The rollback operation calls the popViewController method.
For UINavigationController, you can also use storyboard to add it. The steps are as follows:
1. Select a viewController and then Editor-> Embed in> Navigation Controller to set the viewController to the lowest view of the UINavigationController.
2. Select the table view cell in tableView on the storyboard, right-click it, and link it to the WebView to be redirected. Set the Identifier and push modes of the segue .:
3. In didSelectRowAtIndexPath, you can call the javasmseguewithidentifier () method to trigger the segue action to complete the view switching operation. The Code is as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;"> func tableView (tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {var data = dataSource [indexPath. row] as NewsItem selectedUrl = data. newsId // method 1: use the storyboard to add the webview self. extends mseguewithidentifier ("web", sender: self )}Data transmission between two views is implemented by rewriting the prepareForSegue method.
Override func prepareForSegue (segue: UIStoryboardSegue, sender: AnyObject ?) {If segue. identifier = "web" {var vc = segue. destinationViewController as WebViewController vc. newsId = selectedUrl! // You can use the newsId variable in webViewController }}
We will find that using storyboard is the most difficult and error-prone method. However, adding UINavigationController using code may be very useful in some specific scenarios.
You can give it a try.
Finally, you must remember that UINavigationController uses a stack-like push and pop method to switch the view. The call method is pushViewController and popViewController.
The UITabBarController is a horizontal view switch. The call method is presentViewController and dismissViewController.