Ram Chandran
Ram Chandran

Reputation: 83

Button action is not fired in container View in IOS Swift

enter image description hereI'm new to IOS Programming. 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). In container view segue added following code

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")
}

this getQuotesAction button is not working please help me to fix this. Advance thanks for reply.

Upvotes: 4

Views: 2279

Answers (3)

Martin
Martin

Reputation: 4765

In my case one of the parent view controller was ARC-ed out. 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.

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!

Upvotes: 1

Ram Chandran
Ram Chandran

Reputation: 83

I fixed this issue by increased the height of the containerView because my firstviewcontroller height was higher than the containerView. Now, the sub viewcontroller actions are working fine.

Upvotes: 1

Wayne
Wayne

Reputation: 1

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. Should look something like this:

@IBAction func btnClick(sender: UIButton) {}

Upvotes: -1

Related Questions