繁体   English   中英

如何在单击 Swift 中的重新加载/重置按钮时在选项卡栏中重新加载项目的视图 controller

[英]How to reload item's view controller in tab bar on click of reload/reset button in Swift

我想在单击重置按钮时仅重新加载选项卡栏中 controller 中选定的视图 controller,如下图所示。

我尝试了许多行代码,但它们从未对我有用。 如何在单击按钮时重新加载视图 controller。

@IBAction func reset_Btn(_ sender: Any) {
    
    let refreshAlert = UIAlertController(title: "Reset", message: "All data will be lost.", preferredStyle: UIAlertController.Style.alert)

    refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in
    
        let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "tabBar") as? UITabBarController
        self.navigationController?.pushViewController(vc!, animated: false)
        
        
    }))

    refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
          
    }))

    present(refreshAlert, animated: true, completion: nil)
    
    
    
}

1

为什么需要重新加载视图控制器?

只需将所有变量重置为零,或者如果您正在使用数组,只需.removeAll()并重新加载您的 tableView

为什么? 好吧,如果你用这个

  let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "tabBar") as? UITabBarController
    self.navigationController?.pushViewController(vc!, animated: false)

它不会重新加载您的视图控制器,而是会创建一个新的 controller,如果您不从堆栈控制器中删除它们,您之前的 controller 至少不会消失

pushViewController 使用导航 controller 不会工作,我假设你的根视图 controller 是 UITabbarViewController,

如果你还想推他们,试试这个

self.tabBarController?.show(<#T##vc: UIViewController##UIViewController#>, sender: <#T##Any?#>)

只记得我上面说的,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM