Swift 成功向服务器提交数据后,如何切换选项卡栏索引?

Swift 成功向服务器提交数据后,如何切换选项卡栏索引?,swift,uitabbarcontroller,Swift,Uitabbarcontroller,此代码是用于向服务器发送数据的保存按钮。成功发送后。如何让视图控制器返回到选项卡栏控制器并将其切换到索引2。selectedIndex不工作 if (result == "success") { self.view.endEditing(true) let alert = UIAlertController(title: "Updated", message: "Update Successfully",

此代码是用于向服务器发送数据的保存按钮。成功发送后。如何让视图控制器返回到选项卡栏控制器并将其切换到索引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

initialViewController的类型是什么?UIViewController类型的选项卡栏控制器值没有选择成员索引。。。对不起,我认为它的视图控制器?编辑了答案,请现在检查