zoukankan      html  css  js  c++  java
  • swift场景转换2和委托

    下面是3个视图控制器,用连线来进行跳转!还有一个是用id来进行跳转。如图选中线设置identifiter属性 ,把<去场景>按钮的identifiter 改成c

    写一个A场景的视图控制器

    import UIKit
    
    class ViewController: UIViewController {
    
        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.
        }
    //这是id 按钮的关联函数 @IBAction func jumpToB(sender: UIButton) { //还可以传值 performSegueWithIdentifier(
    "B", sender: "kangkang") } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if(segue.identifier=="B"){ print(sender) } else if(segue.identifier=="c"){ print("to 场景") } } }

    用委托实现更新数据如下图!点击button去b,然后点传值返回

    写一个a场景的视图控制器


    import UIkit
    class
    Aviewcontroll:UIViewController{ //var str:String="lable" @IBOutlet weak var mylabel: UILabel! override func viewDidLoad() { super.viewDidLoad() print("数据初始化lab:",mylabel.text) // mylabel.text= str // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } @IBAction func tojumpB(sender: UIButton) { let vc = self.storyboard?.instantiateViewControllerWithIdentifier("myAScene") as! BviewControll vc.delegate=self presentViewController(vc, animated: true, completion: nil) } //说明用上面的方式进行跳转时,不会执行prepareForSegue方法 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // let target = segue.destinationViewController as! BviewControll // target.delegate = self } } extension Aviewcontroll : BDelegate{ func b(controller: BviewControll, data:String){ print("(data)") mylabel.text = data // str = data // print("数据:",str) } }

    再写一个b场景的视图控制器

    import UIKit
    
    class BviewControll:UIViewController{
         var delegate: BDelegate?
        override func viewDidLoad() {
            super.viewDidLoad()
            
            // Do any additional setup after loading the view.
        }
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            
        }
        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    //      let target = segue.destinationViewController as! Aviewcontroll
    //       self.delegate = target
            
        }
        
        @IBAction func jumptoA(sender: UIButton) {
    //返回my self data 数据
    delegate?.b(self, data:"my self data") dismissViewControllerAnimated(true, completion: nil) } }

    再写一个delegate

    import Foundation
    
    protocol BDelegate{
    
        //    func someMethod()
        //这是为了满足ios开发中委托的标准写法,
        //并不是强制要求这样写,简单来说就是为了装逼
        
        //装逼标准:1.名字:delegate前面的内容,
        //2.第一个参数是对应的类型
        //3.真正需要传递的数据
        func b(controll:BviewControll,data:String)
    
    }

    这样就可以传值返回了label里就有值了!

  • 相关阅读:
    物理课件开发记录之一2013年10月25日
    以as中的泛型数组举一例看帮助手册优化的好处
    flash的显示列表的机制探索一
    组合模式
    actionscript中重写注意事项
    用adobe air写一个金鱼监控程序
    adobe air桌面应用程序在前端显示,类似于暴风的总是在桌面的最上方
    windows7下的cmd命令之powercfg命令,不常用的
    设置默认访问项目的客户端的浏览器版本(IE版本)
    防火墙设置对外开放端口
  • 原文地址:https://www.cnblogs.com/kangniuniu/p/5043522.html
Copyright © 2011-2022 走看看