iOS开发(swift):页面跳转传值(续)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zcc9618/article/details/83215824

副标题:.xib文件的界面与.storyboard的界面相互跳转

一、.storyboard文件的界面跳转到.xib文件的界面

0.回顾:沿用上一篇文章里.storyboard的界面。现在要实现点击绿色界面(.storyboard)按钮跳转至新的蓝色界面(.xib)。

1.下面新建蓝色界面文件(.xib)

勾选xib

建好后会产生两个文件

前台界面(BlueViewController.xib)

后台代码:

import UIKit

class BlueViewController: UIViewController {

    override init(nibName nibNameOrNil: String?,bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

2.修改绿色界面(.storyboard)文件代码

import UIKit

class GreenViewController: UIViewController {

    @IBOutlet weak var resultTextField: UITextField!
    
    //页面跳转传值 法1:segue
    //var message:String = ""
    //页面跳转传值 法1:segue
    
    @IBAction func closeMe(_ sender: Any) {
        
        //页面跳转传值 法1、2:用于关闭绿色页面,回到红色页面
        //self.dismiss(animated: true, completion: nil)
        //页面跳转传值 法1、2:用于关闭绿色页面,回到红色页面
        
        let vc:BlueViewController=BlueViewController(nibName:"BlueViewController",bundle:nil)
        self.present(vc, animated: true, completion: nil)
        
        
    }
    
    override func viewDidLoad() {
        
        //页面跳转传值 法1:segue
        //resultTextField.text = message
        //页面跳转传值 法1:segue
        
        //页面跳转传值 法2:DataStore
        let store=DataStore.sharedStore()
        resultTextField.text = store.message
        //页面跳转传值 法2:DataStore
        
    }

}

*******************************************************************************************************

二、.xib文件的界面跳转到.storyboard文件的界面

1..storyboard文件中新拖入一个界面,背景色改为黄色

右侧属性栏Storyboard ID设为“yellowView”

2.在蓝色界面(.xib)上新增按钮

蓝色界面(.xib)后台代码:

import UIKit

class BlueViewController: UIViewController {

    //绿色界面跳转至蓝色界面代码
    override init(nibName nibNameOrNil: String?,bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //新增:蓝色界面跳转至黄色界面代码
    @IBAction func gotoYellow(_ sender: Any) {
        let storyboard = UIStoryboard(name:"Main",bundle:nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "yellowView")
        self.present(vc, animated: true, completion: nil)
    }
    

}

*************************************************************************************************

猜你喜欢

转载自blog.csdn.net/zcc9618/article/details/83215824