おそらくNotificationCenterメソッドで可能です。
コードだけですが以下のように実装します。
- SubViewのButton2のaddTargetに通知を送る
let button2: UIButton = {
let button = UIButton()
button.text = "Button2"
button.addTarget(self, action: #selector(notificationCenter), for: .touchUpInside)
}
//ステップ1: notificationCenterの名前を定義
static let moveToThreeView = NSNotification.Name(rawValue: "moveToThreeView")
@objc func notificationCenter() {
//ステップ2: ここにMainView1に戻るメソッド
//ステップ3: notificationCenter送信実装(SubViewのクラス名.moveToThreeView)
NotificationCenter.default.post(name: SubView.moveToThreeView, object: nil)
}
2, MainViewに受信メソッドを追加
override func viewDidLoad() {
super.viewDidLoad()
//ステップ4: Subviewから送られたnotificationCenterを受信
NotificationCenter.default.addObserver(self, selector: #selector(moveToThreeView), name: SubView.moveToThreeView, object: nil)
}
@objc func moveToThreeView() {
//ステップ5:ここにView3に遷移するメソッド
}
以下のリンクは参考までに
【Swift】NotificationCenterの使い方
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/03 05:11