gpt4 book ai didi

ios - tabBarController 选定的索引在 viewController 中不起作用 [Swift]

转载 作者:行者123 更新时间:2023-11-29 05:13:06 26 4
gpt4 key购买 nike

在我的 iOS 应用程序中,我有一个包含 3 个项目的 tabBarController。当用户点击第二个项目时,会出现一个 View (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
}
}
}

还有应用程序委托(delegate)代码:

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 - tabBarController 选定的索引在 viewController 中不起作用 [Swift],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59489193/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com