简体   繁体   English

在 IOS Swift 中的容器视图中不会触发按钮操作

[英]Button action is not fired in container View in IOS Swift

在此处输入图片说明 I'm new to IOS Programming.我是 IOS 编程的新手。 I created viewController(ViewController.swift) with container view(ContainerViewController.swift) which have 2 views (FirstViewController.swift and SecondViewViewcontroller.swift), and then i added button in FirstViewcontroler and added @IBAction for that, but I'm not able to click the button(action is not working).我用容器视图(ContainerViewController.swift)创建了viewController(ViewController.swift),它有2个视图(FirstViewController.swift和SecondViewViewcontroller.swift),然后我在FirstViewcontroler中添加了按钮并为此添加了@IBAction,但我不能单击按钮(操作不起作用)。 In container view segue added following code在容器视图中添加了以下代码

ContainerViewController.swift 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 FirstViewController.swift

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

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

this getQuotesAction button is not working please help me to fix this.这个 getQuotesAction 按钮不起作用,请帮我解决这个问题。 Advance thanks for reply.提前感谢回复。

I fixed this issue by increased the height of the containerView because my firstviewcontroller height was higher than the containerView.我通过增加 containerView 的高度解决了这个问题,因为我的 firstviewcontroller 高度高于 containerView。 Now, the sub viewcontroller actions are working fine.现在,子视图控制器操作工作正常。

In my case one of the parent view controller was ARC-ed out.在我的情况下,父视图控制器之一被 ARC 淘汰。 The button worked as UI element (got greyer when tapped), its outlet was working, I was able to update its hidden state, only the action wasn't fired.该按钮用作 UI 元素(点击时变灰),它的插座正在工作,我能够更新其隐藏状态,只是没有触发操作。

I passed the view controller's view at a point of the code, but the view controller was a local variable.我在代码的某个点传递了视图控制器的视图,但视图控制器是一个局部变量。 When I moved it to a class level (where the object instantiated of this class was kept in the memory), it resolved my problem.当我将它移到类级别(该类的实例化对象保存在内存中)时,它解决了我的问题。 It took me almost a day, I hope this helps you to reduce your efforts!我花了将近一天的时间,我希望这可以帮助您减少工作量!

The above code is telling which view controller to go to, after a potential successful button has been pressed.上面的代码告诉在按下一个潜在的成功按钮后要转到哪个视图控制器。 The button will not work with the above code.该按钮不适用于上述代码。 Either create a outlet action... Go to story board, use the split screen action (two circle top right), control click and track your button to your class and release.创建一个插座动作...转到故事板,使用分屏动作(右上角两个圆圈),控制单击并跟踪您的按钮到您的班级并释放。 Name your button and be sure to select action.为您的按钮命名并确保选择操作。 Put your respective go inside those brackets.将您各自的 go 放在这些括号内。 Should look something like this:应该是这样的:

@IBAction func btnClick(sender: UIButton) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM