From the previous page to the next page, it is generally used to pass attributes (a relatively simple way of passing values) .When we need to pass values from the next page forward, we generally think of agents and blocks.Of course, there are singletons.Today we Just learn how to pass value in Swift
We started with the simplest property pass value and used StoryBoard
First page
class AddressBookViewController: UITableViewController <span style = "color: # 3333FF;">, InfoDelegate </ span> {
var array = [AddressInfo] () // Given initial data array
<span style = "color: # FF0000;"> // func getData (ntf: NSNotification) {
self.array.append (ntf.object as AddressInfo)
} </ span>
override func viewDidLoad () {
super.viewDidLoad ()
// <span style = "color: # FF0000;"> NSNotificationCenter.defaultCenter (). addObserver (self, selector: Selector ("getData:"), name: "aaaa", object: nil) </ span>
var model1: AddressInfo = AddressInfo () // AddressInfo inherits from NSObject and has three properties
var dic = Dictionary <String, String> ()
dic ["name"] = "xiao ming"
dic ["age"] = "40"
dic ["phoneNumber"] = "1200001125"
model1.setValuesForKeysWithDictionary (dic)
self.array.append (model1)
}
override func didReceiveMemoryWarning () {
super.didReceiveMemoryWarning ()
}
// MARK:-Table view data source
override func numberOfSectionsInTableView (tableView: UITableView)-> Int {
return 1
}
override func tableView (tableView: UITableView, numberOfRowsInSection section: Int)-> Int {
return self.array.count
}
override func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)-> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier ("cell", forIndexPath: indexPath) as UITableViewCell
var item = self.array [indexPath.row] as AddressInfo
cell.textLabel.text = item.name
return cell
}
// MARK:-Navigation
override func prepareForSegue (segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if sender is UITableViewCell {
var cell = sender as UITableViewCell
var indexPath = tableView.indexPathForCell (cell)
var model = self.array [indexPath! .row]
var vc = segue.destinationViewController as InfoViewController
<span style = "color: # 3333FF;"> // vc.delegate = self </ span>
vc.info = model // Define attributes in the receiver to receive the value to be passed
} else {
var vc = segue.destinationViewController as InfoViewController
<pre name = "code" class = "plain"> <span style = "color: # 3333FF;"> // vc.delegate = self </ span>
}}
func addInfo (info: AddressInfo) {
self.array.append (info)
// self.tableView.reloadData
}
}
Next page
import UIKit
<span style = "color: # 000099;"> // protocol InfoDelegate {
// func addInfo (info: AddressInfo)
//} </ span>
class InfoViewController: UIViewController {
var info: AddressInfo?
<span style = "color: # 000099;">
// var delegate: protocol <InfoDelegate>? </ span>
@IBOutlet weak var nameTF: UITextField!
@IBOutlet weak var addButton: UIButton!
@IBAction func add (sender: AnyObject) {
var model = AddressInfo ()
var dic = Dictionary <String, String> ()
dic ["name"] = nameTF.text
dic ["age"] = ageTF.text
dic ["phoneNumber"] = phoneNumberTF.text
model.setValuesForKeysWithDictionary (dic);
<span style = "color: # FF0000;"> //NSNotificationCenter.defaultCenter().postNotificationName("aaaa ", object: model, userInfo: dic) </ span>
<span style = "color: # 000099;"> // self.delegate! .addInfo (model) </ span>
var alt = UIAlertView (title: "added", message: "name: \ (nameTF.text) has been inserted", delegate: self, cancelButtonTitle: "OK")
alt.show ()
}
@IBOutlet weak var updateButton: UIButton!
@IBAction func update (sender: AnyObject) {
}
@IBOutlet weak var phoneNumberTF: UITextField!
@IBOutlet weak var ageTF: UITextField!
override func viewDidLoad () {
super.viewDidLoad ()
if info == nil {
updateButton.enabled = false
} else {
nameTF.text = info! .name
ageTF.text = info! .age
phoneNumberTF.text = info! .phoneNumber
}
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning () {
super.didReceiveMemoryWarning ()
// Dispose of any resources that can be recreated.
}
}
Okay, let's talk about the proxy again. In fact, the proxy in SWIT is not much different from the proxy in OC.We also need to formulate an agreement on the latter page and write it after import UIKit
protocol InfoDelegate {
func addInfo (info: AddressInfo)
}
Define property proxy before viewDidLoad
Use proxy pass value in add method,
Observe the proxy on the first page, set up the proxy, and complete the proxy method, so that the proxy passes the value (blue code)
There is another method, Shi Yongxin, which is not only used for passing values from the next page to the previous page, but also for the following pages, with several pages in the middle to pass the value, the implementation code is simpler, above The red code block is this method
Finally, briefly talk about the singleton
import UIKit
class SingleData: NSObject {
var name: String?
class func defaultSingleData ()-> SingleData {
var once: dispatch_once_t = 0
var singleData: SingleData?
dispatch_once (& once, {
singleData = SingleData ()})
return singleData!
}
//updated version
class func defaultSingleData1 ()-> SingleData {
struct SSSingleData {
static var once: dispatch_once_t = 0
static var singleData: SingleData?
}
dispatch_once (& SSSingleData.once, {
SSSingleData.singleData = SingleData ()
})
return SSSingleData.singleData!
}
}
Interested students can use the single case as a small exercise
Swift learning (pass by value)