从VC展开到TabBarController并更改选项卡索引 [英] Unwind from a VC to a TabBarController and change tab index
问题描述
我具有以下情节提要布局:
I have the following Storyboard layout:
应用启动时,它会转到TabIndex 1处的HomeView VC
.
When the app starts it goes to HomeView VC
at TabIndex 1.
在HomeView
中,我有一个按钮,然后使用
In the HomeView
I have a button and I segue to the TestView
using
performSegue(withIdentifier: "goToStartTest", sender: self)
现在我想从TestView
转到HistoryView
.我现在看到的唯一方法是创建一个从TestView
到TabBarController
的序列.这样会弹出HomeView
,而标签栏保持完整.我从TestView
使用
Now I want to go from TestView
to HistoryView
. The only way I can see to do that right now is to create a segue from TestView
to TabBarController
. This brings up HomeView
with Tab Bar intact. I do this from TestView
using
performSegue(withIdentifier: "testToRes", sender: self)
但是,这使我只能使用HomeView
,我需要HistoryView
.
However, that leaves me on HomeView
, I need HistoryView
.
我所做的研究指向使用Unwind并检测segue的来源并采取相应的措施.
The research I've done points to using Unwind and detecting where segue came from and acting accordingly.
我在Swift 5中使用最新的Xcode.
I'm using the latest Xcode with Swift 5.
推荐答案
像这样设置您的HomeViewController
:
class HomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
var goToHistory = false
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if goToHistory {
goToHistory = false
// Use whichever index represents the HistoryViewController
self.tabBarController?.selectedIndex = 1
}
}
@IBAction func unwindAndGoToHistory(_ segue: UIStoryboardSegue) {
goToHistory = true
}
}
在您的TestViewController
中,或者:
-
将
UIButton
连接到Exit
图标以展开到unwindAndGoToHistory:
.
Wire a
UIButton
to theExit
icon to unwind tounwindAndGoToHistory:
.
OR
通过将View Controller
图标连接到Exit
图标(再次选择unwindAndGoToHistory:
)来创建程序化展开,然后在 Document Outline 视图中找到此序列,然后在 Attributes Inspector 中为其指定一个标识符,例如"unwindToHistory"
.准备好进行搜寻时,请按performSegue(withIdentifier: "unwindToHistory", sender: self)
触发它.
Create a programmatic unwind by wiring from the View Controller
icon to the Exit
icon (again selecting unwindAndGoToHistory:
), and then find this segue in the Document Outline view and give it an identifier such as "unwindToHistory"
in the Attributes Inspector. When you're ready to segue, trigger it with performSegue(withIdentifier: "unwindToHistory", sender: self)
.
然后,当触发取消同步时,iOS将弹出TestViewController
并调用unwindAndGoToHistory()
.在那里,goToHistory
设置为true
.之后,将触发viewWillAppear()
.由于goToHistory
现在是true
,因此代码将设置tabBarController
的selectedIndex
切换到HistoryViewController
的选项卡.
Then, when the unwind segue is triggered, iOS will pop the TestViewController
and call unwindAndGoToHistory()
. In there, goToHistory
gets set to true
. Afterwards, viewWillAppear()
will be triggered. Since goToHistory
is now true
, the code will set the selectedIndex
of the tabBarController
to switch to the HistoryViewController
's tab.
注意::这会将展开动画显示回
HomeViewController
,然后视图将自动切换到HistoryViewController
.如果 相反,您想立即切换到HistoryViewController
,而无需 动画,请覆盖viewWillAppear
而不是viewDidAppear
.
Note: This will show the unwind animation back to
HomeViewController
, then the view will automatically switch toHistoryViewController
. If instead you'd like to switch toHistoryViewController
immediately with no animation, overrideviewWillAppear
instead ofviewDidAppear
.
这篇关于从VC展开到TabBarController并更改选项卡索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!