Vấn đề ở đây là biến số currentUser
của bạn thuộc loại Bool
, là loại giá trị. Vì vậy, chuyển nó từ bộ điều khiển xem đầu tiên của bạn đến bộ điều khiển xem thứ hai của bạn sẽ thực tế tạo ra một cá thể Bool
mới. Những gì bạn cần là để vượt qua một tham chiếu từ bộ điều khiển xem đầu tiên của bạn để điều khiển xem thứ hai của bạn (xem Value and Reference Types để biết thêm chi tiết về giá trị và tham chiếu với Swift).
Do đó, tùy theo nhu cầu/sở thích của bạn, bạn có thể chọn một trong số ba ví dụ sau.
1. Các đấm bốc kiểu
Ở đây, chúng tôi "hộp" của chúng tôi Bool
bên trong một lớp và thông qua một tài liệu tham khảo về điều đó thể hiện lớp với bộ điều khiển xem thứ hai.
1.1. Tạo một lớp CurrentUser
:
class CurrentUser {
var someBooleanValue = true {
didSet {
print(someBooleanValue)
}
}
}
1,2. Tạo một lớp con UIViewController
cho bộ điều khiển xem đầu tiên:
import UIKit
class ViewController1: UIViewController {
let currentUser = CurrentUser()
override func viewDidLoad() {
super.viewDidLoad()
currentUser.someBooleanValue = false
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let viewController2 = segue.destinationViewController as? ViewController2 {
viewController2.currentUser = currentUser
}
}
}
1,3. Tạo một lớp con UIViewController
cho bộ điều khiển xem thứ hai:
import UIKit
class ViewController2: UIViewController {
var currentUser: CurrentUser?
// Link this IBAction to a UIButton or a UIBarButtonItem in the Storyboard
@IBAction func toggleBoolean(sender: AnyObject) {
if let currentUser = currentUser {
currentUser.someBooleanValue = !currentUser.someBooleanValue
}
}
}
2. Việc đóng cửa phong cách
Ở đây, chúng ta có được một tài liệu tham khảo yếu của điều khiển xem đầu tiên của chúng tôi trong một đóng cửa và vượt qua đóng cửa này để bộ điều khiển xem thứ hai.
2.1. Tạo một lớp con UIViewController
cho bộ điều khiển xem đầu tiên:
import UIKit
class ViewController1: UIViewController {
var currentUser = true {
didSet {
print(currentUser)
}
}
override func viewDidLoad() {
super.viewDidLoad()
currentUser = false
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let viewController2 = segue.destinationViewController as? ViewController2 {
let closureToPerform = { [weak self] in
if let strongSelf = self {
strongSelf.currentUser = !strongSelf.currentUser
}
}
viewController2.closureToPerform = closureToPerform
}
}
}
2,2. Tạo một lớp con UIViewController
cho bộ điều khiển xem thứ hai:
import UIKit
class ViewController2: UIViewController {
var closureToPerform: (() -> Void)?
// Link this IBAction to a UIButton or a UIBarButtonItem in the Storyboard
@IBAction func toggleBoolean(sender: AnyObject) {
closureToPerform?()
}
}
3. Phong cách giao thức-đại biểu
Ở đây, chúng ta thực hiện điều khiển xem đầu tiên của chúng tôi phù hợp với một số giao thức và vượt qua một tham chiếu yếu nó vào bộ điều khiển xem thứ hai.
3.1. Tạo giao thức tùy chỉnh:
protocol MyDelegate: class {
func changeValue()
}
3.2. Tạo một lớp con UIViewController
cho bộ điều khiển xem đầu tiên và làm cho nó phù hợp với các giao thức theo thời gian:
import UIKit
class ViewController1: UIViewController, MyDelegate {
var currentUser = true {
didSet {
print(currentUser)
}
}
override func viewDidLoad() {
super.viewDidLoad()
currentUser = false
}
func changeValue() {
currentUser = !currentUser
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let viewController2 = segue.destinationViewController as? ViewController2 {
viewController2.delegate = self
}
}
}
3,3.Tạo một lớp con UIViewController
cho bộ điều khiển xem thứ hai:
import UIKit
class ViewController2: UIViewController {
weak var delegate: MyDelegate?
// Link this IBAction to a UIButton or a UIBarButtonItem in the Storyboard
@IBAction func toggleBoolean(sender: AnyObject) {
delegate?.changeValue()
}
}
Tôi không thể sử dụng chức năng cho kiến thức của mình, tôi cần thực hiện điều này trong câu lệnh 'if'. Xin lỗi vì không làm cho nó rõ ràng hơn. – AlexCatch
Với 2 phương pháp này, bạn có thể gửi dữ liệu từ Bộ điều khiển Xem thứ nhất đến thứ hai và từ phương thức thứ hai của bạn đến lần đầu tiên. Nó không phải là một vấn đề để gọi điều này trong một tuyên bố "nếu". – derdida
Tôi đã thử phương pháp đầu tiên, tôi không thể sử dụng chức năng ghi đè trong câu lệnh if, biến không được tìm thấy trong bộ điều khiển chế độ xem thứ hai. – AlexCatch