Swift details 22-----------basic usage of uinavigationcontroller and several ways of page passing value

Source: Internet
Author: User

Basic usage of Uinavigationcontroller and several ways of page passing

本文介绍UINavigationController基本用法,因为涉及多页面顺便介绍页面传值

1. Handwriting Code creation UINavigationController

Handwriting creation is simple, first creating a project, which is loaded from storyboard the default. The default load mode is removed first.

Then AppDelegate.swift create in the didFinishLaunchingWithOptions
The code is as follows:

Func Application (Application:uiapplication, Didfinishlaunchingwithoptions launchoptions: [NSObject: Anyobject]?) -Bool {//Override point for customization after application launch.        //Use code here to create Navigationcontrollerwindow =UIWindow(Frame:uiscreen. Mainscreen(). Bounds) window?. Makekeyandvisible()Uinavigationbar. Appearance(). Tintcolor=Uicolor. Whitecolor()Uinavigationbar. Appearance(). Bartintcolor=Uicolor(Red:231.0/255.0, Green:95.0/255.0, Blue:53.0/255.0, Alpha:0.3)//Modify the navigation bar background color        Uinavigationbar. Appearance(). Titletextattributes= [Nsforegroundcolorattributename:Uicolor. Whitecolor()]//Set font color for navigation bar, etc.Let ROOTCV = Viewcontroller (); Let Nav =Uinavigationcontroller(ROOTVIEWCONTROLLER:ROOTCV) window?. Rootviewcontroller= Navreturn true}

Don't look at so much code, actually not so much. The UINavigationBar beginning is the style set for this navigation bar. Completely do not use the default, mainly on 5 lines, the first two lines created window and let the window display, after three lines create a navigation controller, and set up with the controller, and then window set the root controller into this navigation controller. This will be OK.

After the operation found a black box, violence resolved, set the background color on the line. And then add another one BarButtonItem or something.

Self. View. BackgroundColor= Uicolor. Whitecolor() Self. Navigationitem. Rightbarbuttonitem= Uibarbuttonitem (title:"Next", Style:. Plain, Target:self, Action:"GoNext:") Self. Title="Navigation title"Set the navigation bar title (not self. Navigationcontroller?. TitleOh! ) Self. Navigationitem. Backbarbuttonitem= Uibarbuttonitem (title:"I have changed the return.", Style:. Plain, Target:nil, Action:nil)

The last sentence here is to modify the returned BarButtonItem text, here set the title, plus a rightBarButtonItem registered event goNext we come to realize.

func goNext(sender:AnyObject){        print("去下一个咯")        "Third", bundle: nil).instantiateViewControllerWithIdentifier("three"as UIViewController        selftrue)    }

A look at this again dizzy, the middle of those is what ah. First we storyboard created a controller (handwritten read, in order to learn more knowledge), here the instantiateViewControllerWithIdentifier three following settings

Then push go in. The navigation controller is used in a stack of ways advanced after out, with push and pop. That storyboard is very simple to put one Label and one Button , and then introduce that, first look at the effect

Then we register an event for that button, and here we just drag it.
Implemented as follows

@IBAction func showTabBar(sender: AnyObject) {        let mainVc = UIStoryboard(name"Main"bundle: nil).instantiateViewControllerWithIdentifier("second") as! UINavigationController        self.presentViewController(mainVc, animated: true) { () ->in        }    }

This is still storyboard loaded from the controller, but it is used presentViewController because the navigation controller does not let the push navigation controller, this side is mainly like the demo by storyboard creating a navigation controller

2. Create Uinavigationcontroller through Storyboard

If you use the same as me Main.storyboard , you can first delete the default controller, from the right to drag out a UINavigationController , this will default with one UITableViewController , delete, drag a normal UIViewController . Select UINavigationController Right-drag to the UIViewController selection rootViewController . You can then drag BarButtonItem it yourself or write it in your code. I dragged it in here.

This is the case, because I dragged over this side will not be dragged, registered events are also dragged, the same as the button. will no longer demonstrate.

This time the effect is running.

Here is presentViewController the return with the popupdismissViewControllerAnimated

@IBAction func goBack(sender: UIBarButtonItem) {        self.dismissViewControllerAnimated(true) { () ->in            print("返回")        }    }

Navigation Controller basic operation is very simple, see below to download the positive and negative values of different pages

3. Several ways of page passing value
    • Positive and negative values (protocol)

You see that page we put three buttons and an input box, and the button registers the event. Then handwritten a new controller, inside is a back button and a few label also have an input box.

When we click on the first button to enter the next controller, push in, the controller has a method.

func  String){        lb.text = tmpStr    }

So before we push, we call this method to pass the value on the front, and of course you can assign a value to his common property directly.

let vc = ViewControllerFour()letvalue = tf1.textvc.passParams(value!)        true)

It's easy to lb.text = tmpStr assign values to the values passed in label .

Reverse value the proxy is used here, first declare a protocol

protocol ParameterDelegate{    String)}

Then implement this protocol on our first page. Implement the corresponding method

    String){        tf1.text = tmpStr    }

Then on the second page, declare the proxy as your own variable

vardelegate:ParameterDelegate?

To set yourself as a proxy for the second page before push

  let vc = ViewControllerFour() vc.delegate = self  //通过代理进行反向传值letvalue = tf1.text vc.passParams(value!)        true)

Notice no more than just one more word

OK, so it's done. Look at the effect!

    • The second method (passing values via segue)

Directly through storyboard the drag, saying that storyboard the interface is really convenient, need to study

I'm going to drag a controller and connect it, and I'm done here, I'll show you the connection.


And then in the code,

  //通过segue进行的传值    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {        if"toshow" {            letas! ViewControllerTwoShow            vc.tmpStr = tf1.text            //vc.setValue(tf1, forKey: "tmpStr")        }    }

Variables are defined in the target controller tmpStr (the code is shared later)

Look at the effect

    • By way of notification
    let vc = ViewControllerFour()        vc.delegate = self  //通过代理进行反向传值        self.presentViewController(vc, animated: true) { () ->in//发布一条通知NSNotificationCenter.defaultCenter().postNotificationName("DefaultNotif"object:self.tf1.text)        }

First sign up for a notification to show when the interface is complete. Receive notifications on another interface viewWillAppear .

 override func viewWillAppear(animated: Bool) {        //self.navigationController?.hidesBarsOnSwipe = true        self.navigationController?.setNavigationBarHidden(truetrue//隐藏导航栏        NSNotificationCenter.defaultCenter().addObserver(self"doSomeT:""DefaultNotif"nil)    }

Correspondence method

func doSomeT(title:NSNotification){        lb1.text = title.objectas? String    }

Look at the effect:

Probably so much, finally the source, the language is not very good estimate not to speak clearly, we can see the source code. After the source has put this address (think it is OK to give a star OH)
Githubswiftstudy

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Swift details 22-----------basic usage of uinavigationcontroller and several ways of page passing value

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.