代码之家  ›  专栏  ›  技术社区  ›  PvDev

iOS Swift中的ui导航控制器到uitabarcontroller与swrevalview控制器的问题

  •  4
  • PvDev  · 技术社区  · 6 年前

    在我的项目中,我有三个选项卡项home、notification和profile。侧菜单控制器有主页、预订、配置文件和注销。侧栏菜单控制器是通过使用swrevalview控制器cocopods完成的。

    当我将侧栏菜单导航到主选项卡栏时,索引被正确选择并正确导航。当从预订导航时,它会正确导航,但再次导航家庭应用程序会崩溃。控制台输出显示无法将“uinavigationcontroller”(0x10EF79420)类型的值强制转换为“uitabarcontroller”(0x10EF79970)。

    因为预订控制器是自定义视图控制器,剩下的是选项卡栏控制器。当导航到预订屏幕视图时,应隐藏控制器选项卡,用户再次点击菜单按钮并导航到主页或任何其他控制器。

    并且由于预订控制器崩溃,没有标签栏索引。因此,如何在不崩溃的情况下导航到自定义控制器和带有选定索引项的选项卡控制器。

    这是我的截图:

    bookings screen controller menu controller where swrevealviewcontroller used

    我的故事板截图:

    enter image description here

    这是我尝试过的代码:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
        //        tableView.deselectRow(at: indexPath, animated: true)
    
        let row = indexPath.row
    
        if row == 0{
    
    
            let tabBarController = revealViewController().frontViewController as! UITabBarController
    
    
            let storyboard = UIStoryboard(name: "Home", bundle: nil)
    
            let obj = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
    
            let navController = UINavigationController.init(rootViewController: obj)
            tabBarController.selectedIndex = (indexPath as NSIndexPath).row
            tabBarController.tabBar.isHidden = false
            self.revealViewController().pushFrontViewController(tabBarController, animated: true)
    
    
    
        } else if row == 1{
    
            let tabBarController = revealViewController().frontViewController as! UITabBarController
    
            let storyboard = UIStoryboard(name: "Bookings", bundle: nil)
            let obj = storyboard.instantiateViewController(withIdentifier: "BookingsViewController") as! BookingsViewController
            let navController = UINavigationController.init(rootViewController: obj)
        //            tabBarController.selectedIndex = 1
        //            tabBarController.tabBar.isHidden = false
            self.revealViewController().pushFrontViewController(navController, animated: true)
    
    
    
        } else if row == 2 {
    
            let tabBarController = revealViewController().frontViewController as! UITabBarController
    
            let storyboard = UIStoryboard(name: "Profile", bundle: nil)
            let obj = storyboard.instantiateViewController(withIdentifier: "profileViewController") as! profileViewController
            let navController = UINavigationController.init(rootViewController: obj)
            tabBarController.selectedIndex = (indexPath as NSIndexPath).row
            tabBarController.tabBar.isHidden = false
            self.revealViewController().pushFrontViewController(tabBarController, animated: true)
    
    
        } else if row == 3 {
            print(indexPath)
            // Log out user from Firebase
            AuthService.signOut(onSuccess: {
                // Present the Sign In VC
         //                PrefsManager.sharedinstance.logoutprefences()
                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                let signInVC = storyboard.instantiateViewController(withIdentifier: "signInViewController")
                            self.present(signInVC, animated: true)
    
          //                self.navigationController?.pushViewController(signInVC, animated: true)
    
            }) { (errorMessage) in
    
                ProgressHUD.showError(errorMessage)
    
            }
    
    
    
        }
    
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  4
  •   Nikunj Kumbhani    5 年前

    这是swrevalview控制器的工作代码 UINavigationController UITabBarController (斯威夫特4)

    你的故事宝藏会是这样的你不会直接分配 UitababarController(UitababarController) revealViewController().frontViewController 需要这样使用。

    enter image description here

    代码 菜单视图控制器 这样地

    import UIKit
    
    class menuViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    @IBOutlet weak var tblTableView: UITableView!
    @IBOutlet weak var imgProfile: UIImageView!
    
    var ManuNameArray:Array = [String]()
    var iconArray:Array = [UIImage]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        ManuNameArray = ["Home","Booking","Profile","Logout"]
        iconArray = [UIImage(named:"home")!,UIImage(named:"message")!,UIImage(named:"map")!,UIImage(named:"setting")!]
    
        imgProfile.layer.borderWidth = 2
        imgProfile.layer.borderColor = UIColor.green.cgColor
        imgProfile.layer.cornerRadius = 50
    
        imgProfile.layer.masksToBounds = false
        imgProfile.clipsToBounds = true 
        // Do any additional setup after loading the view.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return ManuNameArray.count
    
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MenuCell", for: indexPath) as! MenuCell
    
        cell.lblMenuname.text! = ManuNameArray[indexPath.row]
        cell.imgIcon.image = iconArray[indexPath.row]
    
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
        let revealviewcontroller:SWRevealViewController = self.revealViewController()
    
        let cell:MenuCell = tableView.cellForRow(at: indexPath) as! MenuCell
        print(cell.lblMenuname.text!)
    
        if cell.lblMenuname.text! == "Home"
        {
            print("Home Tapped")
    
            let mainstoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    
            // Here TabbarController is StoryboardID of UITabBarController
            if let tabBarController = mainstoryboard.instantiateViewController(withIdentifier: "TabbarController") as? UITabBarController{
    
                tabBarController.selectedIndex = 0
                revealviewcontroller.pushFrontViewController(tabBarController, animated: true)
            }
        }
        if cell.lblMenuname.text! == "Booking"
        {
            print("message Tapped")
    
            let mainstoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let newViewcontroller = mainstoryboard.instantiateViewController(withIdentifier: "BookingViewController") as! BookingViewController
            let newFrontController = UINavigationController.init(rootViewController: newViewcontroller)
    
            revealviewcontroller.pushFrontViewController(newFrontController, animated: true)
        }
        if cell.lblMenuname.text! == "Profile"
        {
            let mainstoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    
            if let tabBarController = mainstoryboard.instantiateViewController(withIdentifier: "TabbarController") as? UITabBarController{
    
                tabBarController.selectedIndex = 2
                revealviewcontroller.pushFrontViewController(tabBarController, animated: true)
            }
        }
        if cell.lblMenuname.text! == "Logout"
        {
           print("Logout Tapped")
        }
    }
    }
    

    其他剩余视图控制器的代码与以下所有主页、通知、配置文件和预订的代码相同

    import UIKit
    
    class ProfileViewController: UIViewController {
    
    @IBOutlet weak var btnMenuButton: UIBarButtonItem!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        if revealViewController() != nil {
    
            btnMenuButton.target = revealViewController()
            btnMenuButton.action = #selector(SWRevealViewController.revealToggle(_:))
    
            view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
            view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
        }
    
      }
    }
    

    您的输出:

    enter image description here enter image description here