Sam*_*Sam 2 uiviewcontroller modalviewcontroller swift dismissviewcontroller
学习一些视图控制器基础知识,并专注于如何使用按钮关闭模式。
In my slimmed-down example, I have a two view setup with the initial view and the modal. The first view has a button that successfully pops up the modal. On the modal, there is a button that should dismiss itself.
According to other posts and documentation, I should be able to run simple code attached to the button like this:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func CloseModal(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
When I tap the "Close Modal" button nothing happens. What am I missing here? Am I putting this code in the wrong place? Currently, it's in the main ViewController.swift file.
另一种方法是对主视图控制器使用展开序列。只需在第一个视图控制器中添加 \xe2\x80\x9cunwind action\xe2\x80\x9d 即可:
\n\nclass ViewController: UIViewController {\n\n @IBAction func unwindHome(_ segue: UIStoryboardSegue) {\n // this is intentionally blank\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n在本示例中,只需为该展开操作指定一个有意义的名称unwindHome,这样,如果稍后您有多个展开操作,您就可以轻松区分它们。
然后,您可以control从第二个场景中的按钮拖动到 \xe2\x80\x9cexit\xe2\x80\x9d 控件并选择适当的展开操作:
\n\n\n\n这有几个好的方面:
\n\n\xe2\x80\x9cclose\xe2\x80\x9d 按钮不再关心您如何呈现它,它会展开,但是是适当的(例如,如果您稍后将初始转场更改为显示/推送转场,展开转场仍将无需更改任何代码即可工作)。
因为您\xe2\x80\x99现在使用segue来展开,prepare(for:sender:)所以如果您最终需要这样做,所呈现的视图控制器可以使用它来发送回数据。
如果需要,您可以展开多个场景。例如,如果 A 呈现 B,B 呈现 C,显然您可以从 C 展开到 B,但如果您愿意,也可以从 C 一直展开到 A。
因此,在dismiss工作的同时,放松转场是另一种选择。
| 归档时间: |
|
| 查看次数: |
4813 次 |
| 最近记录: |