简体   繁体   中英

Add action to button in .xib file Using Swift(4.0)

I implemented horizontal(Paging) slide show of views, created separate xib and integrated with the view controller. Slide show is working as expected but I want add action to the every views so that from there I can move directly to the respective main content pages. Find the below code for implementation of slide show.

func createSlides() -> [Slide] {

        let slide1:Slide = Bundle.main.loadNibNamed("Slide", owner: self, options: nil)?.first as! Slide

        slide1.labelTitle.text = "Page1"

let slide2:Slide = Bundle.main.loadNibNamed("Slide", owner: self, options: nil)?.first as! Slide

        slide2.labelTitle.text = "Page2"

return [slide1, slide2]

Slide Function

func setupSlideScrollView(slides : [Slide]) {
        scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
        scrollView.contentSize = CGSize(width: view.frame.width * CGFloat(slides.count), height: view.frame.height)
        scrollView.isPagingEnabled = true

 for i in 0 ..< slides.count {
        slides[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
        scrollView.addSubview(slides[i])
    }
}

Xib file

class Slide: UIView {

 @IBOutlet weak var labelTitle: UILabel!


    var onClickCallback: (() -> Void)?

    override init (frame : CGRect) {
        super.init(frame : frame)


    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
    }

If I think what you are asking is right then do this. (BTW I am a bit rusty at this so please don't crucify me if I'm wrong). Also I don't know where you are designing this so I am going to assume it is in a different place to main.viewcontroller. If it isn't just add a button and create a segue to the content page.

  1. Make a public var in createSlides Func and set it to the slide you are on. Eg: Slide 1. When slide two is opened it should update the var two slide two. For the sake of this we will call the var test
  2. Add a button into your design file
  3. Add a @IBAction into the viewcontroller. This @IBAction should be linked with your button.
  4. inside this @IBAction create a segue to the content using the variable we created before. self.performSegue(withIdentifier: test, sender: self)
  5. In the main view controller add two segues from the view with the slidshow to the view controller with the content (Assuming you only have two slides). One segue should be called slide 1 (The same name as the let you created in createSlides func.) The second should be called slide 2 (for obvious reasons.)
  6. Now when your button in slide 1 is pressed it should perform the segue slide 1 and show your main content

Hope I helped, Toby.

Also if this doesn't work, is not what you want to achieve or is badly worded please comment what it is and I will try to fix it.

call this function and pass pageIndex where you want to jump:

func jumpToView(_ index : Int) {
    let offset = CGPoint(x: view.frame.width * CGFloat(index), y: 0)
    scrollView.setContentOffset(offset, animated: true)
}

Hope this what you are looking.

First line in function let offset = CGPoint(x: view.frame.width * CGFloat(index), y: 0) , for calculate position where you want to move in scrollview. Eg : for first xib position will be CGPoint(x:0,y:0) and for 3rd xib, position will be CGPoint(x:200,y:0), consider each xib width is 100

setContentOffset use to scroll at specific location/point.

For more check Link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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