【问题标题】:iOS swift, When touchesEnded called in UIView, presencetViewController other xib or ViewController in StoryboardiOS swift,当在 UIView 中调用 touchesEnded 时,PresencetViewController 其他 xib 或 Storyboard 中的 ViewController
【发布时间】:2017-11-02 14:18:56
【问题描述】:

我想在xib中调用touchEnded函数时展示ViewController,但我不知道如何实现它。

这是我项目的一部分。

// ViewController.swift

class ViewController: UIViewController {

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

        // add custom xib view (day view)
        let view = Bundle.main.loadNibNamed("ScheduleView", owner: self, options: nil)?.first as! ScheduleView
        view.frame = self.view.frame

        self.view.addSubview(view)
    }


}

// ScheduleView.swift

class ScheduleView.swift: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)

        print("touchesBegan")
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)

        print("touchesEnd")

        // move scene in here
        // show secondViewController
    }



}

// secondeViewController.swift

class secondeViewController.swift: UIViewController {

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

    }


}

【问题讨论】:

    标签: ios swift uiview uiviewcontroller presentviewcontroller


    【解决方案1】:

    只有视图控制器可以呈现另一个视图控制器。 您可以使用委托模式来执行此操作。

    创建一个新协议

    protocol PresentVCDelegate {
        func presentVC()
    }
    

    在 ScheduleView 中添加一个属性

    var presentVCDelegate: PresentVCDelegate
    

    在您的 ViewController 中,遵守此协议并将 View 的委托设置为 self

    //in view will appear
    view.presentVCDelegate = self
    

    别忘了遵守协议

    extension ViewController: PresentVCDelegate {
        func presentVC() {
            //present your secondViewController here
        }
    }
    

    最后在 ScheduleView 中的 touchEnd 方法中,调用委托方法

    self.presentVCDelegate.presentVC()
    

    【讨论】:

      【解决方案2】:

      这是我的解决方案。

      // ViewController.swift
      
      class ViewController: UIViewController {
      
          override func viewWillAppear(_ animated: Bool) {
              super.viewWillAppear(animated)
      
              // add custom xib view (day view)
              let view = Bundle.main.loadNibNamed("ScheduleView", owner: self, options: nil)?.first as! ScheduleView
              view.frame = self.view.frame
      
              //in view will appear
              view.presentVCDelegate = self
      
              self.view.addSubview(view)
          }
      
      
      }
      
      extension ViewController: PresentVCDelegate {
          func presentVC() {
              //present your secondViewController here
          }
      }
      
      // ScheduleView.swift
      
      protocol PresentVCDelegate {
          func presentVC()
      }
      
      class ScheduleView: UIView {
          var presentVCDelegate: PresentVCDelegate
          /*
          // Only override draw() if you perform custom drawing.
          // An empty implementation adversely affects performance during animation.
          override func draw(_ rect: CGRect) {
              // Drawing code
          }
          */
      
          override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
              super.touchesBegan(touches, with: event)
      
              print("touchesBegan")
          }
          override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
              super.touchesEnded(touches, with: event)
      
              print("touchesEnd")
      
              // move scene in here
              // show secondViewController
      
          }
      
      
      
      }
      
      // secondeViewController.swift
      
      class secondeViewController: UIViewController {
      
          override func viewWillAppear(_ animated: Bool) {
              super.viewWillAppear(animated)
      
          }
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2013-11-04
        • 1970-01-01
        • 2020-09-04
        • 1970-01-01
        • 2013-01-24
        • 2014-02-03
        • 1970-01-01
        • 2013-04-14
        • 1970-01-01
        相关资源
        最近更新 更多