2

I have an array of images that you scroll through by clicking a button. I migrated to Swift 4 today and now instead of remaining on the same page and scrolling through the images, it switches back to the home tab when the button is clicked. It was working fine in Swift 3. Here is the code, can't find anything on SO regarding how to fix it in Swift 4.

@IBAction func noBtn_clicked(_ sender: UIButton) {
    self.index = (self.index >= self.circleArray.count-1) ? 0 : self.index+1 
    self.swipeImageView.image = circleArray[index] //imageView
}

When the button is clicked it goes to another page instead of remaining on the page and going to the next image.

pacification
  • 5,838
  • 4
  • 29
  • 51
techgirl
  • 293
  • 1
  • 3
  • 18

1 Answers1

1

See IBAction connection in interface builder(in storyboard) of your view controller. You must have connected this action with multiple action or segue.

Reason of this problem is, you have copied UIButton element in your interface builder from same or some other sources.

Note: Copying of UIElement disconnects its connection with IBOutlet only. IBAction connection can't automatically disconnected. Manually you need to disconnect it.

Krunal
  • 77,632
  • 48
  • 245
  • 261
  • Thanks Krunal. You are right. I had a connection going to the page from the homepage but there's not a connection coming from it the opposite way. Once I deleted the connection it worked. But it still doesn't explain why connecting to that page would result in the code not working. I verified there's no connection going back the other way. – techgirl Sep 13 '17 at 13:30
  • 1. Remove all Action connections from your storyboard (view controller) interface. 2. Connect `noBtn_clicked` only to IBAction of your button. Your code will work. – Krunal Sep 13 '17 at 13:40
  • Thanks Krunal. By removing the connections except for noBtn_clicked the code did work. Unfortunately I have a homepage that has links to these pages, I've had to remove the connection from the homepage in order for the code to work. I need to have the connection from the homepage without it going back to the homepage when a button is clicked. Is there a way to do that programmatically. – techgirl Sep 13 '17 at 17:37
  • 1
    If you've navigation controller in root of homepage then try this on back button click (code block) `self.navigationController?.popViewController(animated: true)` – Krunal Sep 13 '17 at 17:39
  • I thought it worked but noticed that the addition of that code removes my navigation and tool bar. I do need those to remain in order for the users to navigate the app. – techgirl Sep 13 '17 at 20:27
  • This code does not have relation to navigation. It is different problem than this.. Please 'Ask New Question' with this navigation and tab problem. And share your code related to navigation, along with storyboard snapshot with new question. I'll answer and solve your problem related to navigation, in new question. Current answer is enough to solve your current issue, you've mentioned here in this question. – Krunal Sep 14 '17 at 04:02
  • I was able to fix the navigation issue. I realized I had to seque pointing to the navigation controller page rather than the page View Controller. However the code still isn't working based on the original question asked with the snippet provided. – techgirl Sep 14 '17 at 16:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154485/discussion-between-techgirl08-and-krunal). – techgirl Sep 14 '17 at 21:12