swift controller之间通过segue传值

2016-05-11  本文已影响0人  Boom了嘿

1)、新建两个UIViewController,通过脱线的形式建立连接。点击传递值得ViewControllrt,按住Ctrl,向被传递值的ViewController脱线。

2)、选择Show Detail。设置一个Identifier,为ShowGiftDetail

3)、在collection的点击事件里面实现传值

letgiftDetailSegueIdentifier ="ShowGiftDetail"

funccollectionView(collectionView:UICollectionView, didSelectItemAtIndexPath indexPath:NSIndexPath) {

//二级栏目返回时直接变成非选中状态

productTag.deselectItemAtIndexPath(indexPath, animated:true)

// segueURL是将要传递的值

varsegueURL = ""

if  let tagList = tags{

segueURL = tagList[indexPath.section].products[indexPath.row].link

}

//按照segue的Identifer进行传值

self.performSegueWithIdentifier(giftDetailSegueIdentifier, sender: segueURL)

}

overridefuncprepareForSegue(segue:UIStoryboardSegue, sender:AnyObject?) {

//按照segue的Identifier进行辨别

ifsegue.identifier==giftDetailSegueIdentifier{

//创建一个GiftDetailController进行赋值、传值

letcontroller = segue.destinationViewControlleras!GiftDetailViewController

controller.giftURL= senderas?String

}

}

4)、如果在DetailViewController中返回到首页,只需添加一个Button,添加一个事件

@IBActionfuncreturnFirstPageController(sender:AnyObject) {

//返回到前一页

self.presentingViewController?.dismissViewControllerAnimated(true, completion:nil)

}

上一篇 下一篇

猜你喜欢

热点阅读