iOS Swift中的导航控制器至UITabbarcontroller问题与SWRevealViewController
在我的项目中,我有三个tabBar项home、notification和profile。和侧菜单控制器有主页,预订,配置文件和注销。侧栏菜单控制器是使用SWRevealViewController cocopods实现的 当我将侧栏菜单导航到主页选项卡时,已正确选择栏索引并正确导航。当从预订中导航时,它可以正常导航,但导航主页应用程序再次崩溃。控制台输出表示无法将“UINavigationController”(0x10ef79420)类型的值强制转换为“UITabBarController”(0x10ef79970) 因为bookings控制器是自定义视图控制器,其余的是选项卡栏控制器。当导航到预订屏幕时,视图控制器选项卡栏应隐藏,用户再次点击菜单按钮并导航到主页或任何其他控制器 由于预订控制器没有选项卡栏索引而崩溃。因此,如何在不崩溃的情况下导航到具有选定索引项的自定义控制器和tabbar控制器 这是我的截图: 我的故事板截图: 以下是我尝试过的代码:iOS Swift中的导航控制器至UITabbarcontroller问题与SWRevealViewController,ios,swift,uitableview,uitabbarcontroller,swrevealviewcontroller,Ios,Swift,Uitableview,Uitabbarcontroller,Swrevealviewcontroller,在我的项目中,我有三个tabBar项home、notification和profile。和侧菜单控制器有主页,预订,配置文件和注销。侧栏菜单控制器是使用SWRevealViewController cocopods实现的 当我将侧栏菜单导航到主页选项卡时,已正确选择栏索引并正确导航。当从预订中导航时,它可以正常导航,但导航主页应用程序再次崩溃。控制台输出表示无法将“UINavigationController”(0x10ef79420)类型的值强制转换为“UITabBarController”(
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的工作代码
(Swift 4)
您的Storyborad将是这样的,您不需要直接将UITabBarController
分配给RevalViewController()。frontViewController
需要这样使用
菜单控件的代码如下
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的代码与以下所有Home、Notification、Profile和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())
}
}
}
您的输出:
您是否只需要
主页
部分中的选项卡?@VRAwesome是的,我需要主页和配置文件屏幕中的选项卡,而不是预订屏幕中的选项卡。可能的问题是revealViewController()。frontViewController as!UITABBARC控制器
。似乎revealViewController()。frontViewController
可能是导航控制器,如上一个屏幕截图所示。为了帮助您进行调试,请尝试打印revalviewcontroller()的类型。frontViewController
,可能frontViewController不是您期望的控制器?太棒了!!你节省了我的时间。。谢谢你的回答,我还有一个疑问,不是这个问题。中的另一个firebase电子邮件更新swift@PvDev您想只更新电子邮件还是同时更新电子邮件和密码?只更新电子邮件而不是密码这里是我尝试的代码:我在控制台中得到的错误是imageData:::czsm imageData:::czsm10@gmail.comimageData::可选(0)写入文档时出错:可选(错误域=FIRAuthErrorDomain代码=17014“此操作是敏感的,需要最近的身份验证。请在重试此请求之前再次登录。”UserInfo={NSLocalizedDescription=此操作是敏感的,需要最近的身份验证。请在重试此请求之前再次登录。错误\u name=错误\u需要\u最近的\u登录})