在iOS中,每个ViewController都是单独的进程,无法在它们之间共享内存数据; 同时,在屏幕上只能有一个ViewController作为焦点,用户想与下层的ViewController交互就必须先关闭(从左侧滑出)上层的ViewController。
有没有一种方法可以结合多个ViewController?有!
我们先来解决第一个问题:如何同时与多个ViewController交互? 原理是这样的:在屏幕上呈现多个ViewController,然后手动布局它们。 本文使用Swift编程、通过Storyboard可视化布局
-
要呈现第二个ViewController,你需要用segue连接它们。 -
设置好你的segue样式 -
设置好你的Presentation -
你的Storyboard看起来应该像这样: -
为第二个ViewController添加对应的Swift文件: -
 将你Storyboard里的视图连接到代码中,并在viveDidAppear中添加
视图.superview?.frame = .init(x: 0, y: 0, width: 0, height: 0)
运行App,点击按钮,你应该看到如下效果??
 🎉恭喜你,你已经学会了如何同时与两个ViewController交互~~(在这个例子中,第二个ViewController看起来简直就像第一个ViewController的一部分)~~ 接下来:如何在两个ViewController间传递内存数据? 你可以参考这篇文章
如果对你有帮助,别忘了点个赞👍
|