我是新手。我正在使用这个side menu 。
在我的项目中,有 2 个角色:
- 经理
- 员工
我必须根据 JSON 数据设置侧边菜单和 RootVC。在经理侧菜单 A、B、C、D、E 中和在员工侧菜单 A、B、C、D 中。
这里我遇到了一个问题。如果角色 == 5(经理),如何设置 SideMenu 和 RootVc,如果角色 == 4(员工)如何设置 SideMenu 和 RootVc。
在这两种情况下,我都必须为经理和员工设置不同的 RootVC。
这里我画了一个设计image 。请有人帮我解决这个问题。
我的Appdelegate代码
@objc func loginUser() {
let userId = UserDefaults.standard.string(forKey: "userId")
let role = UserDefaults.standard.integer(forKey: "Role")
if userId == nil{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "GSLogOutViewController")
self.window?.rootViewController?.present(controller, animated: true, completion: nil)
self.window?.makeKeyAndVisible()
}else{
var initialViewController = UIViewController()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if role == 5{
initialViewController = storyboard.instantiateViewController(withIdentifier: "GSMyTeamViewController") as! GSMyTeamViewController
}
else if role == 4{
initialViewController = storyboard.instantiateViewController(withIdentifier: "GSDashboardViewController") as! GSDashboardViewController
}
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
}
员工 JSON 数据:
JSON: {
result = {
"Progress_status" = 0;
Role = 4;
Source = "default-image.jpg";
email = "appu.y@gmail.com";
userId = 32;
};
status = 200;
}
管理器 JSON 数据:
JSON: {
result = {
"Progress_status" = 0;
Role = 5;
Source = "default-image.jpg";
email = "daya@gmail.com";
userId = 4;
};
status = 200;
}
最佳答案
我通常这样处理这种情况:
1- 在侧面菜单表格 View 中添加经理和员工的所有选项(即:[A、B、C、D、E])
2- 检索 JSON 响应后,检查用户类型(经理或员工)
3-重写tableView委托(delegate)函数tableView(_:heightForRowAt:)
:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath.row {
case 4:
// 'E' row -> use ternary operator here to check the user role
// if it's Manager the cell height will be 40.0 (for example)
// if it's Employee the 'E' row height will be 0.0 (i.e: hidden)
return role == "Employee" ? 0.0 : 40.0
break
default:
//other rows
return 40.0
break
}
}
4- 检索 JSON 后不要忘记重新加载 tableView:
tableView.reloadData()
关于ios - 如何在swift中基于JSON数据设置RootViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51008247/