2016-11-30 108 views
0

控制器之間切換時保持值I具有login screen和在成功登錄,我通過navigation controller傳遞對象的細節側杆menu controller(冠捷控制器)。在邊欄菜單中,我有兩個選擇,並且當從其他view controller切換到頂部view controller,被刪除的值(按我的理解,我傳遞值從loginVC,它可能不會持有這些值)。的Xcode(SWIFT 3.0) - 如何邊欄菜單

由於現在邊欄菜單transistion的工作完全正常。但是,當我從AnotherVC切換回HomeVC,它不是拿着它們從LoginVC傳遞的值。

有人可以幫助我解決這個問題。

下面是我的代碼片段

故事板: enter image description here

代碼段:

On login button click: (LOGIN VC)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "loginSuccessIdentifier" { 
      if let navController = segue.destination as? UINavigationController { 
      if let destinController = navController.topViewController as? HomeViewController { 
       destinController.loggedInUser = sender as! UserDetails! 
      } 
      } 
     } 
    } 

首頁VC:

var loggedInUser:UserDetails! //UserDetails class is defined separately. Contains variables like id,firstname, lastname etc. I'm displaying those values on HomeVC 
override func viewDidLoad() { 
     super.viewDidLoad() 

     addSlideMenuButton() 

     tokenLbl.adjustsFontSizeToFitWidth = true 
     nameLbl.adjustsFontSizeToFitWidth = true 
     idLbl.adjustsFontSizeToFitWidth = true 

     if(loggedInUser != nil) 
     { 
      firstLbl.text = loggedInUser.token 
      secondLbl.text = loggedInUser.lastname 
      idLbl.text = String(loggedInUser.agentId) 
     } 
    } 

SIDEBARMENU VC:

protocol SlideMenuDelegate { 
    func slideMenuItemSelectedAtIndex(_ index : Int32) 
} 

class MenuViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet var tblMenuOptions : UITableView! 
    @IBOutlet var btnCloseMenuOverlay : UIButton! 
    var arrayMenuOptions = [Dictionary<String,String>]() 
    var btnMenu : UIButton! 
    var delegate : SlideMenuDelegate? 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tblMenuOptions.tableFooterView = UIView() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 
     updateArrayMenuOptions() 
    } 

    func updateArrayMenuOptions(){ 
     arrayMenuOptions.append(["title":"HOME VC", "icon":"Icon1"]) 
     arrayMenuOptions.append(["title":"ANOTHER VC", "icon":"Icon2"]) 

     tblMenuOptions.reloadData() 
    } 

    @IBAction func onCloseMenuClick(_ button:UIButton!){ 
     btnMenu.tag = 0 

     if (self.delegate != nil) { 
      var index = Int32(button.tag) 
      if(button == self.btnCloseMenuOverlay){ 
       index = -1 
      } 
      delegate?.slideMenuItemSelectedAtIndex(index) 
     } 

     UIView.animate(withDuration: 0.3, animations: {() -> Void in 
      self.view.frame = CGRect(x: -UIScreen.main.bounds.size.width, y: 0, width: UIScreen.main.bounds.size.width,height: UIScreen.main.bounds.size.height) 
      self.view.layoutIfNeeded() 
      self.view.backgroundColor = UIColor.clear 
      }, completion: { (finished) -> Void in 
       self.view.removeFromSuperview() 
       self.removeFromParentViewController() 
     }) 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cellMenu")! 

     cell.selectionStyle = UITableViewCellSelectionStyle.none 
     cell.layoutMargins = UIEdgeInsets.zero 
     cell.preservesSuperviewLayoutMargins = false 
     cell.backgroundColor = UIColor.clear 

     let lblTitle : UILabel = cell.contentView.viewWithTag(101) as! UILabel 
     let imgIcon : UIImageView = cell.contentView.viewWithTag(100) as! UIImageView 

     imgIcon.image = UIImage(named: arrayMenuOptions[indexPath.row]["icon"]!) 
     lblTitle.text = arrayMenuOptions[indexPath.row]["title"]! 

     return cell 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     let btn = UIButton(type: UIButtonType.custom) 
     btn.tag = indexPath.row 
     self.onCloseMenuClick(btn) 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return arrayMenuOptions.count 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1; 
    } 
} 

BASE VC:(幻燈片代表)

class BaseViewController: UIViewController, SlideMenuDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func slideMenuItemSelectedAtIndex(_ index: Int32) { 
     let topViewController : UIViewController = self.navigationController!.topViewController! 
     print("View Controller is : \(topViewController) \n", terminator: "") 
     switch(index){ 
     case 0: 
      print("HomeVC\n", terminator: "") 

      self.openViewControllerBasedOnIdentifier("HomeVC") 

      break 
     case 1: 
      print("AnotherVC\n", terminator: "") 

      self.openViewControllerBasedOnIdentifier("AnotherVC") 

      break 
     default: 
      print("default\n", terminator: "") 
     } 
    } 

    func openViewControllerBasedOnIdentifier(_ strIdentifier:String){ 
     let destViewController : UIViewController = self.storyboard!.instantiateViewController(withIdentifier: strIdentifier) 

     let topViewController : UIViewController = self.navigationController!.topViewController! 

     if (topViewController.restorationIdentifier! == destViewController.restorationIdentifier!){ 
      print("Same VC") 
     } else { 
      self.navigationController!.pushViewController(destViewController, animated: true) 
     } 
    } 

    //to add slide option button 
    func addSlideMenuButton(){ 
     let btnShowMenu = UIButton(type: UIButtonType.system) 
     btnShowMenu.setImage(self.defaultMenuImage(), for: UIControlState()) 
     btnShowMenu.frame = CGRect(x: 0, y: 0, width: 25, height: 25) 
     btnShowMenu.addTarget(self, action: #selector(BaseViewController.onSlideMenuButtonPressed(_:)), for: UIControlEvents.touchUpInside) 
     let customBarItem = UIBarButtonItem(customView: btnShowMenu) 
     self.navigationItem.leftBarButtonItem = customBarItem; 
    } 

    func defaultMenuImage() -> UIImage { 
     var defaultMenuImage = UIImage() 

     UIGraphicsBeginImageContextWithOptions(CGSize(width: 30, height: 22), false, 0.0) 

     UIColor.black.setFill() 
     UIBezierPath(rect: CGRect(x: 0, y: 3, width: 30, height: 1)).fill() 
     UIBezierPath(rect: CGRect(x: 0, y: 10, width: 30, height: 1)).fill() 
     UIBezierPath(rect: CGRect(x: 0, y: 17, width: 30, height: 1)).fill() 

     UIColor.white.setFill() 
     UIBezierPath(rect: CGRect(x: 0, y: 4, width: 30, height: 1)).fill() 
     UIBezierPath(rect: CGRect(x: 0, y: 11, width: 30, height: 1)).fill() 
     UIBezierPath(rect: CGRect(x: 0, y: 18, width: 30, height: 1)).fill() 

     defaultMenuImage = UIGraphicsGetImageFromCurrentImageContext()! 

     UIGraphicsEndImageContext() 

     return defaultMenuImage; 
    } 

    func onSlideMenuButtonPressed(_ sender : UIButton){ 
     if (sender.tag == 10) 
     { 
      // To Hide Menu If it already there 
      self.slideMenuItemSelectedAtIndex(-1); 

      sender.tag = 0; 

      let viewMenuBack : UIView = view.subviews.last! 

      UIView.animate(withDuration: 0.3, animations: {() -> Void in 
       var frameMenu : CGRect = viewMenuBack.frame 
       frameMenu.origin.x = -1 * UIScreen.main.bounds.size.width 
       viewMenuBack.frame = frameMenu 
       viewMenuBack.layoutIfNeeded() 
       viewMenuBack.backgroundColor = UIColor.clear 
       }, completion: { (finished) -> Void in 
        viewMenuBack.removeFromSuperview() 
      }) 

      return 
     } 

     sender.isEnabled = false 
     sender.tag = 10 

     let menuVC : MenuViewController = self.storyboard!.instantiateViewController(withIdentifier: "MenuViewController") as! MenuViewController 
     menuVC.btnMenu = sender 
     menuVC.delegate = self 
     self.view.addSubview(menuVC.view) 
     self.addChildViewController(menuVC) 
     menuVC.view.layoutIfNeeded() 


     menuVC.view.frame=CGRect(x: 0 - UIScreen.main.bounds.size.width, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height); 

     UIView.animate(withDuration: 0.3, animations: {() -> Void in 
      menuVC.view.frame=CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height); 
      sender.isEnabled = true 
      }, completion:nil) 
    } 
} 

回答

1

爲無害的數據,你只需要一個單一的會議期間舉行,您可以存儲它作爲您的App Delegate中的一個實例屬性。讀/寫你只需創建一個新的參考AppDelegate中

let appDelegate = UIApplication.shared.delegate as! AppDelegate 

appDelegate.yourProperty = "saveStuff" 

如果您希望將數據持久化,這是不是一個大的數據量,NSUserDefaults的嘗試。您只需確保您的對象符合NSCoder協議。

https://developer.apple.com/reference/foundation/userdefaults 

如果您希望將數據持久化,這是不是一個大的數據量,數據應該是安全的,你應該使用鑰匙扣服務。

https://developer.apple.com/reference/security/1658642-keychain_services 
+0

感謝scord。但我可以保存整個類對象的用戶默認值或應用程序委託屬性? – ASN

+0

是的,你可以保存整個對象。你不應該保存整個視圖控制器。而是保存數據,並在需要時使用該數據重新初始化您的視圖控制器。 – scord