如何成功向服务器提交数据后切换标签栏索引?
问题描述:
此代码是用于将数据发送到服务器的保存按钮。发送成功后。如何让视图控制器返回到标签栏控制器并将其切换到索引2? .selectedIndex不起作用。如何成功向服务器提交数据后切换标签栏索引?
if (result == "success") {
self.view.endEditing(true)
let alert = UIAlertController(title: "Updated", message: "Update Successfully", preferredStyle: UIAlertControllerStyle.alert)
let okButton = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
alert.addAction(okButton)
self.present(alert, animated: true, completion: nil)
let appDelegate = UIApplication.shared.delegate! as! AppDelegate
let initialViewController =self.storyboard!.instantiateViewController(withIdentifier: "tabBarVC")
appDelegate.window?.rootViewController = initialViewController
appDelegate.window?.makeKeyAndVisible()
self.tabBarController?.selectedIndex = 2 // this code is not working
}
答
试试这个 -
let initialViewController = self.storyboard!.instantiateViewController(withIdentifier: "tabBarVC") as! UITabBarController
appDelegate.window?.rootViewController = initialViewController
appDelegate.window?.makeKeyAndVisible()
initialViewController.selectedIndex = 2
+0
UIViewController类型的值没有成员selectedIndex ...对不起,我认为它的视图控制器? –
+0
编辑答案,现在检查 –
什么是initialViewController的类型? –
标签栏控制器 –