这几天都在写播放器… 相关的内容写完再上传吧

本文来源:https://learnappmaking.com/pass-data-between-view-controllers-swift-how-to/

Use var controller

假设我们有两个ViewController名为FirstSecond

Second里面有一个UILabel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Second: UIViewController {

var text: String?

override func viewDidLoad() {
super.viewDidLoad()

let btn = UIButton(frame: CGRect(x: 0, y: 500, width: 200, height: 200))
btn.backgroundColor = .red
btn.addTarget(self, action: #selector(b), for: .touchUpInside)
view.addSubview(btn)

let label = UILabel(frame: CGRect(x: 0, y: 500, width: 200, height: 200))
label.text = text
view.addSubview(label)
}

@objc func b() {
var vc = Frist()
vc.text = "hhh"
navigationController?.pushViewController(vc, animated: true)
}
}

那么从First传递到Second

1
2
3
4
5
@objc func b() {
var vc = Second()
vc.text = "hhh"
navigationController?.pushViewController(vc, animated: true)
}

其中道理都懂

Here’s what happens in that piece of code:

  • You create a constant called vc and assign it an instance of Second. You pass the right XIB name in the initializer to make sure the view controller uses the correct XIB file.
  • You then assign a string to the property text on vc. This is the actual passing of the data between the view controllers!
  • Finally, you push the new view controller onto the navigation stack with pushViewController(_:animated:). The change is animated, so when it’s executed you’ll see the new view controller “slide in” from the right of the iPhone screen.

那么从Second传到First怎么办呢,我们当然可以再次使用这种方法

That’s all there is to it. But… this approach for passing data isn’t the most ideal. It has a few major drawbacks:

  • The MainViewController and SecondaryViewController are now tightly coupled. You want to avoid tight-coupling in software design, mostly because it decreases the modularity of your code. Both classes become too entangled, and rely on each other to function properly, with often leads to spaghetti code.
  • The above code example creates a retain cycle. The secondary view controller can’t be removed from memory until the main view controller is removed, but the main view controller can’t be removed from memory until the secondary view controller is removed. (A solution would be the weak property keyword.)
  • Two developers can’t easily work separately on MainViewController and SecondaryViewController, because both view controllers need to have an understanding about how the other view controller works. There’s no separation of concerns.

这会导致两个ViewController的高度耦合,同时也增加了栈

我们可以使用Delegation来传递数据

Use delegation

Passing Data Back With Delegation

我们先创建一个protocol

1
2
3
protocol sendMethod{
func sendMessage(var contact:String)
}

然后让FirstSecond遵守senfMethod

1
2
3
4
5
6
7
var delegate:sendMethod?
self.delegate!.sendMessage(textField.text!)
func sendMessage(contact: String) {
label.text = contact
}
let vc = Second()
vc.delegate = self

然后转过去就行了

Use closure

这是在First

1
2
3
4
var completionHandler:((String) -> Int)? = { text in
print("text = \(text)")
return text.characters.count
}

那么在Second中实现

1
2
let vc = First()
let result = vc.completionHandler("hhhh")

就可以在不同的ViewController中互用方法