【问题标题】:Button action is not fired in container View in IOS Swift在 IOS Swift 的容器视图中未触发按钮操作
【发布时间】:2016-10-11 02:31:00
【问题描述】:

我是 IOS 编程新手。我用容器视图(ContainerViewController.swift)创建了viewController(ViewController.swift),它有2个视图(FirstViewController.swift和SecondViewViewcontroller.swift),然后我在FirstViewcontroler中添加了按钮并为此添加了@IBAction,但我不能单击按钮(操作不起作用)。在容器视图中添加以下代码

ContainerViewController.swift

func segueIdentifierReceivedFromParent(button: String){
    buttonClick = button
    if button == "first"
    {
        self.segueIdentifier = "firstSegue"
        self.performSegueWithIdentifier(self.segueIdentifier, sender: nil)
    }
    else if button == "second"
    {
        self.segueIdentifier = "secondSegue"
        self.performSegueWithIdentifier(self.segueIdentifier, sender: nil)
    }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == segueIdentifier{

        //Avoids creation of a stack of view controllers
        if lastViewController != nil{
            lastViewController.view.removeFromSuperview()
        }

        vc = segue.destinationViewController //as! UIViewController

       self.addChildViewController(vc)
       vc.view.frame = CGRect(x: 0,y: 0, width:self.view.frame.width,height:self.view.frame.height)

      self.view.addSubview(vc.view)
      vc.didMoveToParentViewController(self)
}

FirstViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func getQuotesAction(sender: UIButton){
    print("Button is Clicked")
}

此 getQuotesAction 按钮不起作用 请帮我解决这个问题。提前感谢您的回复。

【问题讨论】:

  • 如果我理解正确,我会在您的 containerViewController 上设置一个委托协议,并使用一个更改它的方法 childVC。然后FirstViewController中的按钮就可以调用那个方法了。
  • 你能展示你的故事板吗?

标签: ios swift uibutton uicontainerview


【解决方案1】:

我通过增加 containerView 的高度解决了这个问题,因为我的 firstviewcontroller 高度高于 containerView。现在,子视图控制器操作正常。

【讨论】:

    【解决方案2】:

    在我的情况下,父视图控制器之一被 ARC 淘汰。该按钮用作 UI 元素(点击时变灰),它的插座正在工作,我能够更新它的隐藏状态,只是没有触发操作。

    我在代码的某个点传递了视图控制器的视图,但视图控制器是一个局部变量。当我将它移到类级别(该类的实例化对象保存在内存中)时,它解决了我的问题。花了我差不多一天的时间,希望能帮助你减少工作量!

    【讨论】:

      【解决方案3】:

      上面的代码告诉你在按下一个可能成功的按钮后要转到哪个视图控制器。该按钮不适用于上述代码。要么创建一个出口动作......转到故事板,使用分屏动作(右上角两个圆圈),控制点击并跟踪你的按钮到你的班级并释放。为您的按钮命名并确保选择操作。把你各自的去那些括号里面。应该看起来像这样:

      @IBAction func btnClick(sender: UIButton) {}
      

      【讨论】:

      • 我已经在 FirstViewController.swift 中添加了这段代码 @IBAction func btnClick(sender: UIButton) {},但它不起作用
      • 你有没有 self.performSegueWithIdentifier("segueName", sender: nil) 是你的按钮操作
      猜你喜欢
      • 2016-07-30
      • 1970-01-01
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      • 1970-01-01
      相关资源
      最近更新 更多