Swift代理传值,和闭包传值

A-B 然后 B 将值传给A ,A是一个CollectionView B改变A的个数
oc中如果不能访问在 protocol 上面添加@objc
A代码
import UIKit
class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,SecondVCDelegate {
@IBOutlet weak var collectionView : UICollectionView?
var count = 5;
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return count;
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCollectionViewCell", forIndexPath: indexPath)
    
    return cell;
}


func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat{
    return 5;
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat{
    return 5;
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
return CGSizeMake((UIScreen.mainScreen().bounds.size.width-15)/3, (UIScreen.mainScreen().bounds.size.width-15)/3);
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let secondVC = SecondViewController()

// secondVC.delegate = self
secondVC.myValue = {(count)->Void in
self.count = count
self.collectionView?.reloadData()
}
let nav_ctrl = UINavigationController.init(rootViewController: secondVC)
self.presentViewController(nav_ctrl, animated: true) {

    }
}

func updateCount() {
    count = 20
    self.collectionView?.reloadData()
}

}

B 代码:

import UIKit
protocol SecondVCDelegate {
func updateCount();
}

typealias SendValue = (count:Int)->Void

class SecondViewController: UIViewController {
var delegate:SecondVCDelegate?
var myValue:SendValue?
override func viewDidLoad() {
super.viewDidLoad()
let backBtn = UIButton.init(frame: CGRectMake(0, 0, 60, 44))
backBtn.setTitle("返回", forState: .Normal)
backBtn.setTitleColor(UIColor.blueColor(), forState: .Normal)
backBtn.addTarget(self, action: #selector(SecondViewController.back), forControlEvents: .TouchUpInside)
let backItem = UIBarButtonItem.init(customView: backBtn)

    self.navigationItem.leftBarButtonItem = backItem
}

func back() {
    self.dismissViewControllerAnimated(true) {
        self.myValue!(count: 6)

// self.delegate?.updateCount()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

你可能感兴趣的:(Swift代理传值,和闭包传值)