用户登录后更改菜单项

时间:2018-05-25 07:33:09

标签: ios swift swift4

无论用户是登录还是退出,我都想更改菜单项。 我在互联网上搜索了很多,但没有找到一个好的解决方案。

这是我的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 个答案:

答案 0 :(得分:0)

考虑将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)
}

答案 1 :(得分:0)

您可以使用唯一键 UserDefaults 中存储一个标记。 对于login user = true

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

因此,当您使用用户数组时,您可以使用KEY检索此UserDefaults标志,如果为true,则在菜单最后一个选项是注销(因为用户是登录),如果UserDefaults标志为false,则菜单最后一个选项是登录。