tabBarController 选定的索引在 viewController [Swift] 中不起作用
tabBarController selected index don't work in viewController [Swift]
在我的 iOS 应用程序中,我有一个包含 3 个项目的 tabBarController。当用户点击第二个项目时,会显示一个视图(CreateCommentsViewController),然后当用户点击关闭按钮时,我想更改 tabBarController 的选定索引(从 1 到 0),但它不能以这种方式工作:
class CreateCommentViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
func getTabBarController() -> UITabBarController? {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
guard let tabBarController = appDelegate.window?.rootViewController as? UITabBarController else { return nil }
return tabBarController
}
@IBAction func dismissNewCommentView(_ sender: UIBarButtonItem) {
let tabBarController = getTabBarController()!
self.dismiss(animated: true) {
tabBarController.selectedIndex = 0
}
}
}
还有应用委托代码:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window!.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: VIEW_CONTROLLER_IDS.TAB_BAR_CONTROLLER) as! UITabBarController
window!.makeKeyAndVisible()
return true
}
尝试更新您的操作(如果您使用 Swift 5 & 5.1 & 5.2):
@IBAction func dismissNewCommentView(_ sender: UIBarButtonItem) {
self.dismiss(animated: true) {
if let tabBarController = UIApplication.shared.windows.first?.rootViewController as? UITabBarController {
tabBarController.selectedIndex = 0
}
}
}
在我的 iOS 应用程序中,我有一个包含 3 个项目的 tabBarController。当用户点击第二个项目时,会显示一个视图(CreateCommentsViewController),然后当用户点击关闭按钮时,我想更改 tabBarController 的选定索引(从 1 到 0),但它不能以这种方式工作:
class CreateCommentViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
func getTabBarController() -> UITabBarController? {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
guard let tabBarController = appDelegate.window?.rootViewController as? UITabBarController else { return nil }
return tabBarController
}
@IBAction func dismissNewCommentView(_ sender: UIBarButtonItem) {
let tabBarController = getTabBarController()!
self.dismiss(animated: true) {
tabBarController.selectedIndex = 0
}
}
}
还有应用委托代码:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window!.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: VIEW_CONTROLLER_IDS.TAB_BAR_CONTROLLER) as! UITabBarController
window!.makeKeyAndVisible()
return true
}
尝试更新您的操作(如果您使用 Swift 5 & 5.1 & 5.2):
@IBAction func dismissNewCommentView(_ sender: UIBarButtonItem) {
self.dismiss(animated: true) {
if let tabBarController = UIApplication.shared.windows.first?.rootViewController as? UITabBarController {
tabBarController.selectedIndex = 0
}
}
}