We will pass data from class second back to ViewController.swift.
In class second, we want to pass a string get from textfield to
ViewController.
In ViewController.swift, add this text to class declaration.
DataEnteredDelegate
Copy this function to above last close bracket.
func setback(info: String){
la.text = info
}
This function receive string pass back and set to Label la.
Add this line in function of button to open second viewcontroller.
vc.delegate = self
func next(sender: UIButton){
let vc = second()
vc.passingtext = text
vc.delegate = self
self.presentViewController(vc, animated: true, completion: nil)
}
In class
second, copy to below import.
protocol DataEnteredDelegate: class {
func setback(info: String)
}
Add this line to below
class open bracket.
weak var delegate: DataEnteredDelegate? = nil
We will get string
from Textfield and pass back, function for back button like this.
let year = textfield.text
if(year == ""||year == " "){
toast("Please enter the year")
}
else{
self.delegate?.setback(year!)
self.dismissViewControllerAnimated(true, completion: nil)
}
We get string from textField, check if it blank, show inform to user, if
not blank, pass to previous ViewController by call setback function. Need to
use protocol on top of class.
Run ro see result.
No comments:
Post a Comment