bugfix> 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()
}

ご協力ありがとうございました。

回答 2 件
  • 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つ保存できますUserDefaults とユニークキー。 ログインユーザー= trueの場合

    ユーザーがログアウトすると、UserDefaults値はfalseに変更されます。

    したがって、ユーザー配列の場合、このUserDefaultsフラグをKEYで取得でき、trueの場合、メニューの最後のオプションはログアウト(ユーザーがログインしているため)であり、UserDefaultsフラグがfalseの場合、メニューの最後のオプションはログインです。

あなたの答え