在iOS Swift中使用SWRevealViewController的UINavigation控制器解决UITabbarcontroller问题 [英] UINavigation controller to UITabbarcontroller issue with SWRevealViewController in iOS Swift

查看:95
本文介绍了在iOS Swift中使用SWRevealViewController的UINavigation控制器解决UITabbarcontroller问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有三个tabBar项目主页,通知和配置文件.侧面菜单控制器具有主页,预订,配置文件和注销信息.侧栏菜单控制器是通过使用SWRevealViewController cocopods完成的.

In my project i have three tabBar item home, notification and profile. And side menu controller has home, bookings, profile and logout. Side bar menu controller was done by using SWRevealViewController cocopods.

当我将侧栏菜单导航到主页选项卡栏索引时,已正确选择并正确导航.从预订导航时,它可以正常导航,但再次导航家庭应用程序时会崩溃.控制台输出显示无法将类型'UINavigationController'(0x10ef79420)的值强制转换为'UITabBarController'(0x10ef79970).

When i navigating side bar menu to home tab bar index was selected correctly and navigating properly. While navigating from bookings it navigates properly but again navigating home app gets crashed. And console output says Could not cast value of type 'UINavigationController' (0x10ef79420) to 'UITabBarController' (0x10ef79970).

由于预订控制器是自定义视图控制器,其余的是标签栏控制器.导航到预订屏幕视图时,控制器的标签栏应隐藏,用户再次点击菜单按钮,然后导航到家庭或任何其他控制器.

Since bookings controller is custom view controller and remaining are tab bar controller. And when navigating to booking screen view controller tab bar should be hide and user tap again menu button and navigating to home or any other controller.

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

And crashed due to booking controller does not has tab bar index. So how can navigate without crash to custom controller and tabbar controller with selected index item.

这是我的屏幕截图:

我的故事板截图:

这是我尝试过的代码:

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)

        }



    }


}

推荐答案

这是SWRevealViewController的工作代码,其中 UINavigationController UITabBarController 4)

您的Storyborad就像这样,您无需直接将UITabBarController分配给revealViewController().frontViewController需要像这样使用.

Your Storyborad it will be like this you not directly assign UITabBarController to revealViewController().frontViewController need to Use Like this.

MenuViewController 的代码

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")
    }
}
}

其他剩余ViewController的代码与以下所有房屋,通知,配置文件和预订的代码相同

Code for Other Remaining ViewControllers Same as Following for All Home, Notification, Profile and Booking

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())
    }

  }
}

您的输出:

这篇关于在iOS Swift中使用SWRevealViewController的UINavigation控制器解决UITabbarcontroller问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆