代码之家  ›  专栏  ›  技术社区  ›  Kaveh Naseri

用户登录后更改菜单项

  •  0
  • Kaveh Naseri  · 技术社区  · 6 年前

    我想更改用户登录或注销的菜单项。 我在网上搜索了很多,但没有找到一个好的解决方案。

    这是我的 SettingLauncher 类返回视图和菜单项。

    答:在这里,我选择要展示的物品 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()
    }
    

    谢谢你的帮助。

    2 回复  |  直到 6 年前
        1
  •  0
  •   Hassan Shahbazi    6 年前

    考虑更换 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)
    }
    
        2
  •  0
  •   Anji Mendpara    6 年前

    你可以将一面旗帜存储在 用户默认值 具有 唯一密钥 . 对于登录用户=true

    当用户注销时,UserDefaults值应更改为false。

    所以,当你使用用户数组时,你可以用键检索这个UserDefaults标志,如果为true,那么菜单中的最后一个选项是logout(因为用户是login),如果UserDefaults标志为false,那么菜单中的最后一个选项是login。