【问题标题】:UIBarButton click to set flag value using Swift 4.2UIBarButton 单击以使用 Swift 4.2 设置标志值
【发布时间】:2019-07-13 15:53:12
【问题描述】:

我的场景,我有两个 UIBarButton 和动作 method,在这里,每当我点击 DoneCancel 按钮时,我都会移动到另一个 ViewController。一旦用户点击 Done barbutton 我需要设置一些标志值并验证它的另一个 ViewController for button 点击或未点击。

我的 ViewController 一

let barButtonItem = UIBarButtonItem(image: UIImage(named: "backImgs"),
                                            style: .plain,
                                            target: self,
                                            action: #selector(menuButtonTapped))
self.navigationItem.rightBarButtonItem = barButtonItem

@objc fileprivate func menuButtonTapped() { // here I need to set flag value }

我的 ViewController 二

class ViewControllertwo: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //Here need to validate flag values to button clicked or not 
    }
}

【问题讨论】:

  • 其实你叫什么地方ViewControllertwo
  • 添加一些额外的代码,我的意思是// here I need to set flag value相关推送代码
  • 你是否为两个 barbuttons 使用了相同的目标
  • 创建按钮时,设置它们的tag 属性。然后,在menuButtonTapped中,检查标签是否匹配,然后执行你需要的代码。

标签: ios swift3 uibarbuttonitem


【解决方案1】:

我部分理解您的问题,在这里您需要使用tag 概念,例如

 override func viewDidLoad() {
    super.viewDidLoad()
let barButtonItem = UIBarButtonItem(image: UIImage(named: "backImgs"),
                                        style: .plain,
                                        target: self,
                                        action: #selector(menuButtonTapped(_:)))
    barButtonItem.tag = 20
    let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(menuButtonTapped(_:)))
    cancelButton.tag = 10
    self.navigationItem.rightBarButtonItem = barButtonItem
    self.navigationItem.leftBarButtonItem = cancelButton
   }

处理你的目标函数就像

  @objc fileprivate func menuButtonTapped(_ sender: UIBarButtonItem) {
  // if you dont want the tag concept, use title property for check which button tapped  //print("get Tapped button title  == \(sender.title)")
    //if sender.tag == 20{
        // clicked for another VC button, add your segue code here
   // }else{
        // pressed cancel button
   // }
     let vcTwo = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllertwo") as! ViewControllertwo
    vcTwo.getSelectedTag = sender.tag
    self.navigationController?.pushViewController(vcTwo, animated: true)

}

在您的 VC2 上创建一个全局 Int 以获取其来源的标签,

** ViewControllertwo**

class ViewControllertwo : UIViewController {

    var getSelectedTag   = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        if getSelectedTag == 20 {
            //pressed menu Tapped
        }
    }
}

【讨论】:

    【解决方案2】:

    很难从您的问题中看出,但假设您从 ViewControllerTwo 呈现 ViewControllerOne,您将要为此使用委托模式。这类似于您使用表/集合视图的方式,您实际上是在告诉 ViewControllerTwo 成为 ViewControllerOne 的委托,以便它可以对按下的按钮做出反应。

    首先创建一个协议,该协议定义 ViewControllerOne 可以发送给其委托的消息:

    protocol ViewControllerOneDelegate: AnyObject {
      func viewControllerOneDidTapDone(_ viewController: ViewControllerOne)
      func viewControllerOneDidTapCancel(_ viewController: ViewControllerOne)
    }
    

    然后扩展 ViewControllerTwo 来实现你的协议:

    extension ViewControllerTwo: ViewControllerOneDelegate {
      func viewControllerOneDidTapDone(_ viewController: ViewControllerOne) {
        // Set your flag or do whatever you need to do on 'Done'.
        // Then dismiss viewController.
      }
      func viewControllerOneDidTapCancel(_ viewController: ViewControllerOne) {
        // Dismiss viewController
      }
    }
    

    在 ViewControllerOne 中,将委托保留为弱属性,并在按下按钮时调用委托方法:

    class ViewControllerOne: UIViewController {
      weak var delegate: ViewControllerOneDelegate?
    
      @objc private func donePressed() {
        delegate?.viewControllerOneDidTapDone(self)
      }
    
      @objc private func cancelPressed() {
        delegate?.viewControllerOneDidTapCancel(self)
      }
    }
    

    最后,在 ViewControllerTwo 的某个地方,您需要将自己设置为 ViewControllerOne 的代理。这可能是在创建 ViewControllerOne 时:

    class ViewControllerTwo: UIViewController {
      ...
    
      private func presentViewControllerOne() {
        let viewControllerOne = ViewControllerOne(nibName:nil, bundle: nil)
        viewControllerOne.delegate = self
        // Present or push viewControllerOne
      }
    
      ...
    }
    

    【讨论】:

      【解决方案3】:

      据我了解,解决方案是 -

      ViewContorllerOne

      class ViewControllerOne : UIViewController {
      
          var isMenubuttonTapped : Bool = false
      
          override func viewDidLoad() {
              super.viewDidLoad()
      
              self.actionToPushOnViewControllerTwo()
      
              NotificationCenter.default.addObserver(self, selector: #selector(actioFire), name: NSNotification.Name.init("MenuButtonTapped"), object: nil)
          }
      
          //Call from any where in viewControllerOne
          func actionToPushOnViewControllerTwo() {
              let viewControllerTwo : ViewControllerTwo = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerTwo") as! ViewControllerTwo
              self.present(viewControllerTwo, animated: true, completion: nil)
          }
      
          @objc func actioFire(_ notification: Notification) {
              print(notification.userInfo!["isMenuButtonTapped"] as Any)
      
              if let isMenuButtonTapped = notification.userInfo!["isMenuButtonTapped"] as? Bool {
                  self.isMenubuttonTapped = isMenuButtonTapped
              }
          }
      }
      

      ViewControllerTwo

      class ViewControllerTwo : UIViewController {
      
              override func viewDidLoad() {
                  super.viewDidLoad()
      
                  let barButtonItem = UIBarButtonItem(image: UIImage(named: "backImgs"),
                                                      style: .plain,
                                                      target: self,
                                                      action: #selector(menuButtonTapped))
                  self.navigationItem.rightBarButtonItem = barButtonItem
              }
      
              @objc fileprivate func menuButtonTapped() {
                  // here I need to set flag value
      
                  self.dismiss(animated: true) {
                      NotificationCenter.default.post(name: Notification.Name("MenuButtonTapped"),
                                                      object: nil,
                                                      userInfo:["isMenuButtonTapped": true])
                  }
              }
          }
      

      另一个简单的解决方案是

      self.dismiss(animated: true) {
                  if let tabController = self.presentingViewController as? UITabBarController {
                      if let navController = tabController.selectedViewController as? UINavigationController {
                          if let secondTab = navController.viewControllers.first as? HomeViewController {
                              secondTab.tfData = "YES"
                          }
                      } else {
                          if let secondTab = tabController.selectedViewController as? HomeViewController {
                              secondTab.tfData = "YES"
                          }
                      }
                  }
              }
      

      【讨论】:

      • 谢谢。我会在这里检查并更新你@Nilesh R Patel
      • 嗨,你能告诉我如何通过 isMenubuttonTapped 来关闭视图控制器吗? @Nilesh R 帕特尔
      • @devmikle 你能告诉我流程是什么吗?因为有点混乱如果你是 push 或呈现 viewControllerTwo 比你可以传递 isMenubuttonTapped fag 使用 let viewControllerTwo : ViewControllerTwo = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerTwo") as! ViewControllerTwo viewControllerTwo.isMenubuttonTapped = self.isMenuButtonTapped self.navigationController?.pushViewController(viewControllerTwo, animated: true)
      • 我有两个视图控制器。 VC_one 和 Popup 存在。在这里,如果我们单击 VC_one tableview 单元格,我将按当前模型 vc 显示弹出窗口(不是推送导航)。现在,如果我单击 Popup present bar 按钮,它将关闭并显示 VC_one。当解雇发生时,我需要将一些值传递给 VC_one。仅供参考:VC_one,我正在维护标签栏。 @Nilesh R 帕特尔
      • 超级。是否可以传递更多值? @Nilesh R 帕特尔
      猜你喜欢
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多