从前一页面向后一页面传值一般用到属性(比较简单的传值方式),当需要从后一页面向前传值时,我们一般想到的是代理和block,当然还有单例,今天我们就学习用Swift如何传值
我们从最简单的属性传值开始,使用了StoryBoard
第一个页面
class AddressBookViewController: UITableViewController<span style="color:#3333FF;">, InfoDelegate</span> { var array = [AddressInfo]() //给定初始数据数组 <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继承于NSObject,有三个属性 var dic = Dictionary<String, String>() dic["name"] = "小明" 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 //在接受方定义属性接收需要传递的值 } else { var vc = segue.destinationViewController as InfoViewController <pre name="code" class="plain"><span style="color:#3333FF;">// vc.delegate = self</span>
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:"已添加", message:"已经将name:\(nameTF.text)插入", 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. } }好的,我们再说说代理,其实Swit中的代理和OC中的代理相差不大,我们一样要在后一页面制定协议,写在import UIKit之后
protocol InfoDelegate {
func addInfo(info: AddressInfo)
}
在viewDidLoad之前定义属性代理
在add方法中使用代理传值,
在第一个页面遵守代理,设置代理,完成代理方法,这样就实现了代理传值(蓝色代码)
还有一种方法释永信更广,不单单用于后一页面向前一页面传值,还能用于,后面的页面,中间隔着几个页面进行传值,实现代码更为简单,上面红色代码块就是此方法
最后简单说一下单例
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! } //升级版本 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! } }有兴趣的同学可以使用单例做个小练习
原文:http://blog.csdn.net/gcztian/article/details/43534505