An article 10 ways to learn page values (top) 1, the simplest AB side forward value effect is as follows:
The simplest forward pass value. gif
First, we first create two view controllers Viewcontroller.swift and Subviewcontroller.swift
Set the first interface
First create good one textfeild in the first interface to write the text we want to preach
Import Uikitclass Viewcontroller:uiviewcontroller {Let TextField = Uitextfield () override Func Viewdidload () { Super.viewdidload () Self.view.backgroundColor = Uicolor.whitecolor () Textfield.frame = CGRectMake (7, 100 , Textfield.borderstyle =. Roundedrect Self.view.addSubview (TextField)}}
Here we set when clicking on the screen to jump to the second interface
and perform a value-transfer operation
Set a public property in the Subviewcontroller to receive the data
var string:string?
Set the TAP screen simultaneous value in Viewcontroller
When you tap the screen, execute the method override Func Touchesbegan (Touches:set, withevent event:uievent?) {//mark: Create object for second interface Let svc = Subviewcontroller ()//mark: Forward value via property svc.string = SELF.TEXTF Ield.text//Jump to the second interface Self.presentviewcontroller (SVC, Animated:true, Completion:nil)}
Create a good one label in the second interface to receive the value
Add a Label control
The control does not expose private let Textlabel = UILabel () override Func Viewdidload () {super.viewdidload () Self.view. BackgroundColor = Uicolor.cyancolor () Textlabel.frame = CGRectMake (7, $, I, a) textlabel.textalignment = . Center Self.view.addSubview (Textlabel)
Assigns values passed in to the control when the view is about to be displayed
Override func Viewwillappear (Animated:bool) {super.viewwillappear (animated)//When the view is going to be displayed, go to assign the value passed in, to the control Self.textLabel.text = string}
When the screen is clicked, the first interface is returned
Override Func Touchesbegan (Touches:set, withevent event:uievent?) {//Return to previous page self.dismissviewcontrolleranimated (True, Completion:nil)}
2, the use of properties for the reverse value of the AB page to pass the effect as follows:
Simple reverse-pass value. gif
First, we first create two view controllers Viewcontroller.swift and Subviewcontroller.swift
The first step
Set good one fixed label in Viewcontroller, set the text to display as: Location Services, and a display state of Statuslabel
Step Two
Define a property in Subviewcontroller to receive the target object of the return value
var vc: ViewController?
Then place a uiswitch, set the corresponding method of the switch
To set the response method of a switch func swaction (sw:uiswitch) {//judgment state if Sw.on {//mark: This step implements a reverse pass value, returning the status value to the a page s Elf.vc?. Lsstatuslabel.text = "On"}else{self.vc? Lsstatuslabel.text = "OFF"}}
Step Three
Set the Touchbegan method inside the Viewcontroller
Override Func Touchesbegan (Touches:set, withevent event:uievent?) {//mark: Instantiate the object of a second interface let svc = Subviewcontroller ()//give the B page a value, tell b the target of the callback data is who SVC.VC = self Click on the screen to jump to the second interface Self.presentviewcontroller (SVC, Animated:true, Completion:nil)}
3, the use of Nsuserdefult two-way transmission value effect is as follows:
Principle: Nsuserdefaults is a system to plist file encapsulation of a class, we can read and write files through this class, in homedirectory/library/preferences/ Under the XXX.userdefaults.plist. Using the characteristics of the nsuserdefaults, the value of one interface to be sent to the plist file, and then another interface from the plist file read out, the completion of the value-based operation
The first step
Create Good Viewcontroller and Subviewcontroller
Placement of label in Viewcontroller
Step Two
Create 5 buttons in Subviewcontroller, add a tag value for each button, and implement an alternating check, with the following code:
for i in 0..<5 { let button = uibutton (type: . System) button.settitle ("section \ (i + 1) a button ", forstate: . Normal) button.frame = CGRectMake (7, cgfloat (150 + i * 100), 400, 100) //set Tag value for each button button.tag = 100 + i //Click events button.addtarget (self, action: #selector (Self.buttonaction (_:)), forcontrolevents: . Touchupinside) self.view.addsubview (button) }
Set Click events for alternating selection effects
For I in 0..<5 {let btn = Self.view.viewWithTag (+ i) as! UIButton btn.selected = false} sender.selected = True
Use Nsuserdefaults in the Click event to write the tag value of the button to the plist file for data logging
First, get the Nsuserdefaults object, which is a singleton object
let userDefaults = NSUserDefaults.standardUserDefaults()
Make a data record of the button's tag value
let selectedIndex = String(sender.tag)
Write Plist file
userDefaults.setObject(selectedIndex, forKey: "selectedIndex")
Write back a file
userDefaults.synchronize()
Step Three
From the Viewwillappear function of Viewcontroller We read the plist file and complete the reverse pass value
Get the Nsuserdefaults object first
let userDefaults = NSUserDefaults.standardUserDefaults()
Read the data and get the tag value of the corresponding button in the Subviewcontroller
let si = userDefaults.objectForKey("selectedIndex")
At this point Si is the anyobject type, we convert him to int type
let tag = si?.intValue
Then modify the text of the label in the Viewcontroller
self.label.text = "第\(tag! - 99)个按钮是选中转态"
Here, the reverse value of the process is completed, the second interface which button is selected in the first interface will be displayed, the effect is as follows
However, jumping back from the first interface to the second interface is that the button becomes all unchecked, and the following is the implementation of the forward value
The same is to get the Nsuserdefaults object, then read the data, finally get the tag value to find the Object button, set the button is selected, here also need to do the reader to complete the next content
4. Using the system to reverse the value of the closure packet
This approach is to use the system's own closures
Self.presentviewcontroller (Uiviewcontroller, Animated:bool) {code}
This pass-through value will have a delay, because it is done after the page switch and then go to the value of the transfer
The effect is as follows:
System closure pass value. gif First step
First create good viewcontroller and Subviewcontroller
Viewcontroller Layout Good one label is used to display received data in Subviewcontroller layout good one textfeild used to write data
Step Two
Create a Receive property in Subviewcontroller
var vc: ViewController?
Step Three
Write the Touchbegan method in Viewcontroller
Gets the Subviewcontroller object, opens the closure in the Presentviewcontroller method, and passes the Viewcontroller to the VC of Subviewcontroller
Override Func Touchesbegan (Touches:set, withevent event:uievent?) {Let svc = Subviewcontroller ()//pass through the view controller itself to facilitate backhaul using Self.presentviewcontroller (SVC, Animated:true) {//This method executes SVC.VC = self} After the next page is ejected
Fourth Step
Write the Touchbegan method in Subviewcontroller
Open a closure in the Self.dismissviewcontrolleranimated method to pass the text in the property TF back to the Textlabel of the property VC
Override Func Touchesbegan (Touches:set, withevent event:uievent?) {self.dismissviewcontrolleranimated (true) {///Use this closure to pass data self.vc! back and forth. Textlabel.text = Self.tf.text}}
5. Custom closed Packet Transfer value
And the idea of using the system to transmit values is the same, the custom closure of the value is to declare a closure, through the closure of the page to pass the value of each other
The effect is the same as the 4th type
The first step
Create Good Viewcontroller and Subviewcontroller
Layout good one label and Textfeild, respectively, in VC and SUBVC
Declare a closure in the Subviewcontroller
var backValueClusore:((text:String)->Void)?
Step Two
Write the Touchbegan method in Viewcontroller
Gets the Subviewcontroller object, opens the closure in the Presentviewcontroller method, and passes the Viewcontroller to the VC of Subviewcontroller
Override Func Touchesbegan (Touches:set, withevent event:uievent?) {Let svc = Subviewcontroller () self.presentviewcontroller (SVC, Animated:true, Completion:nil)//ma RK: Assign the text in the closure to Self.textlabel Svc.backvalueclusore = {(text:string)->void in self.textLabel.text = Te XT}
Step Three
Write the Touchbegan method in Subviewcontroller
Open a closure in the Self.dismissviewcontrolleranimated method to pass the text in the property TF back to the Textlabel of the property VC
Override Func Touchesbegan (Touches:set, withevent event:uievent?) {//mark: Directly executes the closure self.dismissviewcontrolleranimated (true, Completion:nil) self.backvalueclusore! (text:self.tf.text!)
Article transferred from Demoer's Pinterest
An article 10 ways to learn page values (top)