作者:壹滒_918 | 来源:互联网 | 2023-09-25 23:04
下面是3个视图控制器,用连线来进行跳转!还有一个是用id来进行跳转。如图选中线设置identifiter属性,把<去场景>按钮的identifiter改成c写一个A场景的视图控制器
下面是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里就有值了!