ios - 用户登录后更改菜单项
问题描述
无论用户登录还是注销,我都想更改菜单项。我在互联网上搜索了很多,但没有找到一个好的解决方案。
这是我SettingLauncher
返回视图和菜单项的类。
A : 在这里我选择我必须展示的项目swiftkeychainwrapper
import UIKit
import SwiftKeychainWrapper
class SettingLauncher:NSObject,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDel egateFlowLayout{
let cellId = "cellId"
let blackView = UIView()
let cellHeight : CGFloat = 50
var homeController: HomeController?
var defaultController : DefaultController?
//var productDetailController : ProductDetailController?
override init() {
super.init()
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(SettingCell.self, forCellWithReuseIdentifier: cellId)
}
let settings : [Setting] = {
let isLoggedIn : String? = KeychainWrapper.standard.string(forKey: "myKey")
// menu items
let settingOne = Setting(name: .Profile, imageName: "profile-icon")
let settingTwo = Setting(name: .Login, imageName: "login-icon")
let settingThree = Setting(name: .Orders, imageName: "orders-icon")
let settingFour = Setting(name: .Category,imageName: "category-icon")
let settingFive = Setting(name: .Cancel, imageName: "cancel-icon")
let settingSix = Setting(name: .SignOut, imageName: "login-icon")
// A
if isLoggedIn == "true" {
return [settingOne,settingThree,settingFour,settingSix]
}
else{
return [settingTwo,settingThree,settingFour]
}
}()
let collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
return cv
}()
//show menu
func showHambuger(){
if let window = UIApplication.shared.keyWindow {
blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)
blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleCancel)))
window.addSubview(blackView)
window.addSubview(collectionView)
let height : CGFloat = CGFloat(settings.count) * cellHeight
let y = window.frame.height - height
collectionView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: height)
blackView.frame = window.frame
blackView.alpha = 0
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.blackView.alpha = 1
self.collectionView.frame = CGRect(x: 0, y: y, width: self.collectionView.frame.width, height: self.collectionView.frame.height)
self.collectionView.reloadData()
}, completion: nil)
}
}
@objc func handleCancel(setting: Setting){
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.blackView.alpha = 0
if let window = UIApplication.shared.keyWindow {
self.collectionView.frame = CGRect(x:0,y: window.frame.height,width: self.collectionView.frame.width,height: self.collectionView.frame.height)
}
}) { (completed: Bool) in
if setting.name != .Cancel {
//self.productDetailController?.showControllerForSetting(setting: setting)
self.defaultController?.showControllerForSetting(setting: setting)
}
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return settings.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! SettingCell
let setting = settings[indexPath.item]
print(setting)
cell.setting = setting
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: cellHeight)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let setting = self.settings[indexPath.item]
handleCancel(setting: setting)
}
}
在我的 DefaultController 类中,我像这样调用函数
lazy var settingLauncher : SettingLauncher = {
let launcher = SettingLauncher()
launcher.defaultController = self
return launcher
}()
@objc func handleHambugerButton(){
settingLauncher.showHambuger()
}
谢谢您的帮助。
解决方案
考虑替换var
为func
并且不要忘记reload
您对用户状态更改的看法。我相信你会找到更好的UICollectionView
实施结构。
private var collectionViewSettingItems = [String]()
var isLoggedIn: Bool {
didSet {
setupItems()
collectionView.reloadData()
}
}
func setupItems() {
let isLoggedIn : String? = KeychainWrapper.standard.string(forKey: "myKey")
// menu items
let settingOne = Setting(name: .Profile, imageName: "profile-icon")
let settingTwo = Setting(name: .Login, imageName: "login-icon")
let settingThree = Setting(name: .Orders, imageName: "orders-icon")
let settingFour = Setting(name: .Category,imageName: "category-icon")
let settingFive = Setting(name: .Cancel, imageName: "cancel-icon")
let settingSix = Setting(name: .SignOut, imageName: "login-icon")
if isLoggedIn {
collectionViewSettingItems = [settingOne,settingThree,settingFour,settingSix]
}
else{
collectionViewSettingItems = [settingTwo,settingThree,settingFour]
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return collectionViewSettingItems.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! SettingCell
let setting = collectionViewSettingItems[indexPath.item]
print(setting)
cell.setting = setting
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: cellHeight)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let setting = self.settings[indexPath.item]
handleCancel(setting: setting)
}
推荐阅读
- java - Java Swing:“java.awt.AWTEventMulticaster.mouseMoved”错误
- linux - Linux libc++ seems to be broken
- sql-server - 在 WHERE 子句中获取今天的日期
- powershell - 带有文本“$__VAR__”的 PowerShell 转义字符串
- url - 如何使用 JasperSoft Studio 创建超链接作为静态文本的一部分?
- c++ - C++ 引用在函数范围内创建的实例
- powershell - PowerShell - 显示父对象和子对象的属性
- vue.js - 未捕获的错误:无法从“/”中找到模块“web/static/js/app”
- google-drive-api - 在 Google Colab 中安装 Google Drive 的持久授权
- javascript - Javascript:在脚本之间共享一个类对象