ealeksandrov / EAIntroView

Highly customizable drop-in solution for introduction views.
MIT License
3.76k stars 501 forks source link

How to add callback method in button inside EAIntroView #225

Closed Nikoloutsos closed 4 years ago

Nikoloutsos commented 4 years ago

I am trying to execute some code when user clicks the button. But do not know how to set an onClick method for the button. Usually I do it by binding it with the ViewController but now I do not have one.

let ingropage1 = EAIntroPage.init(customViewFromNibNamed: "test3")
let introView = EAIntroView.init(frame: self.view.bounds, andPages[ingropage1!])
introView?.delegate = self
introView?.show(in: self.view)

Where test3 is a xib file and it has the button

ealeksandrov commented 4 years ago

You can check custom nib case in example app: https://github.com/ealeksandrov/EAIntroView/blob/master/Example/Source/ViewController.m#L210-L213

Nikoloutsos commented 4 years ago

@ealeksandrov Thank you for the quick reply. I followed your example but It crashes when trying to downcast. (I've added button's tag to 2 from xib)

 let btn = ingropage1?.pageView.viewWithTag(2) as! UIButton //It crashes
 btn.addTarget(self, action: #selector(buttonAction(sender:)), for: .touchUpInside)
ealeksandrov commented 4 years ago

You are making force cast. Debug and check if any element of chain is nil or different type.

Nikoloutsos commented 4 years ago

I could not achieve what I wanted. As a workaround I used the skipButton as my custom button.

 let introView = EAIntroView.init(frame: self.view.bounds, andPages: [ingropage1!,ingropage2!])
 introView?.delegate = self
 introView?.skipButton.setTitle("  Sync now  ", for: .normal)
 introView?.skipButton.backgroundColor = UIColor.orange
 introView?.skipButton.layer.cornerRadius = 8
 introView?.skipButton.clipsToBounds = true

Thank you for your help anyway!