ContainerView和ViewController之间的数据

时间:2018-03-29 05:19:11

标签: ios swift

我想在容器视图和viewcontroller之间传递数据。然后,我点击标签文本应该更改的按钮。该按钮位于容器视图中,标签文本位于viewcontroller中。我在Stack Overflow上尝试了一些解决方案,但似乎没有任何工作,或者我可能做错了。

有人可以帮忙吗?

Gamecontroller.swift

malloc

查看控制器:

@objc func buttonMove(_ sender:UIButton!) {

    print("Button tapped" + String(pointsGame))

    let buttonWidth = sender.frame.width;
    let buttonHeight = sender.frame.height;

    let viewWidth = sender.superview!.bounds.width
    let viewHeight = sender.superview!.bounds.height

    let xwidth = viewWidth - buttonWidth
    let yheight = viewHeight - buttonHeight

    let xoffset = CGFloat(arc4random_uniform(UInt32(xwidth)))
    let yoffset = CGFloat(arc4random_uniform(UInt32(yheight)))

    sender.center.x = xoffset + buttonWidth / 2
    sender.center.y = yoffset + buttonHeight / 2

    pointsGame = pointsGame + 1
}

override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
    if segue.identifier == "game" {
        let vc = segue.destination as! ViewController
        vc.gpoints = pointsGame
    }

3 个答案:

答案 0 :(得分:2)

Gamecontroller.swift

////// declare object first
    var vc = ViewController()

@objc func buttonMove(_ sender:UIButton!) {

  print("Button tapped" + String(pointsGame))

  let buttonWidth = sender.frame.width;
  let buttonHeight = sender.frame.height;

  let viewWidth = sender.superview!.bounds.width
  let viewHeight = sender.superview!.bounds.height

  let xwidth = viewWidth - buttonWidth
  let yheight = viewHeight - buttonHeight

  let xoffset = CGFloat(arc4random_uniform(UInt32(xwidth)))
  let yoffset = CGFloat(arc4random_uniform(UInt32(yheight)))

  sender.center.x = xoffset + buttonWidth / 2
  sender.center.y = yoffset + buttonHeight / 2

  pointsGame = pointsGame + 1
  vc.refreshView((points : pointsGame)

}

override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
  if segue.identifier == "game" {
      vc = segue.destination as! ViewController
      vc.gpoints = pointsGame
}

查看控制器:

class ViewController: UIViewController {
    @IBOutlet weak var points: UILabel!
    var gpoints: Int!

  override func viewDidLoad() {
      super.viewDidLoad()
      gpoints = 0

  }
  func refreshView(points : Int! )
  {
      points.text = String(points)
  }
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
}

答案 1 :(得分:1)

它不是一个全局使用viewController的好方法,最好的方法是使用委托或通知。当你在pointsGame = pointsGame + 1

之后发布通知时点击按钮,这是通知示例
// Post notification
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "buttonpressed"), object: nil , userInfo: ["points": 20])

在View控制器ViewDidLoad中添加观察者通知

let name = Notification.Name("buttonpressed")
NotificationCenter.default.addObserver(self, selector: #selector(refreshLabel(object:)), name: name, object: nil)


func refreshLabel(_ notification: Notification) {
if let myDict = notification.object as? [String: Any] {
    if let point = myDict["points"] as? Int {
        print(point)
        points.text = String(point)
    }

}

答案 2 :(得分:0)

步骤1:创建容器视图控制器的一个变量。

    var templateVC : GPTemplateVC?

步骤2:准备一个segue做到这一点。

    if segue.identifier == "embedSegueTemplateVC" {
         templateVC = segue.destination as? GPTemplateVC
    }

然后您可以访问ContainerVC中的每个元素。

您也可以使用相同的委托。

只需使用主视图控制器确认您的委托,并使用Container视图控制器中的委托对象调用委托方法。在您的情况下,请在按钮单击时调用它。