50238

Incorrect data passed on first click of button

Question:

Salutations,

My problem is as follows, I have an app I am working on which contains two views. The first simply has 2 buttons labeled "Slide Show One", and "Slide Show Two".

When I click on the first button, it displays the information for the second slideshow as (due to my supreme novice-ness), I select which of the slideshows to select via a boolean as follows:

var button : Bool = false;

then:

@IBAction func slideShowOne() { button = true; } @IBAction func slideShowTwo() { print("clicked button 2"); button = false; }

Finally in the prepareForSegue:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let passedPic = segue.destination as! ViewControllerTwo; if(button == true) { print(button); passedPic.picsChosen = sLS1; } if(button == false) { print(button); passedPic.picsChosen = sLS2; } }

Only way to have it display the correct information is by clicking on say button 1 for btn1 slideshow, going back, then clicking on button 1 again. Why is this, does it have anything to do with how swift handles function calls?

Aside: Swift knowledge is now a grand total of one week.

EDIT: Got it working, much appreciated. Now a quick question, at this time my two IBActions are empty, but required since they have segues attached to them, would be the best way to either make them meaningful, or perhaps still be able to segue; I assume the prepare function is an absolute must, otherwise there is no way (of which I know), to send the required data to my second VC.

@IBAction func slideShowOne() { //self.performSegue(withIdentifier: "slideOne", sender: self); } @IBAction func slideShowTwo() { //self.performSegue(withIdentifier: "slideTwo", sender: self); } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let passedPic = segue.destination as! ViewControllerTwo; if(segue.identifier == "slideOne") { passedPic.picsChosen = sLS1; } else { passedPic.picsChosen = sLS2; } }

Answer1:

Try connecting view controller to view controller via storyboard - same ctrl dragging but whole view controller to other.

Give that segue identifier -> select identifier and on forth tab give it identfiier.

After that in one of your buttons, eg:

@IBAction func slideShowOne() { button = true; self.performSegue(withIdentifier: "toTheOtherVC", sender: self) }

And you will have desired behaviour.

Answer2:

The reason is that: override func prepare(for segue: UIStoryboardSegue, sender: Any?) always be executed firstly. So the actual workflow is as following:

-> 1) Initialize first view

-> 2) <strong>var button = false</strong>

-> 3) the user taps btn1

-> 4) func prepare is called

-> 5) second view is displayed <strong>(var button is still false at this moment, that's why you get incorrect response)</strong>

-> 6) func slideShowOne() is called (the user already is in second view)

-> 7) <strong>var button = true</strong> caused by func slideShowOne() (the user already is in second view)

-> 8) the user goes back

-> 9) the user taps btn1 again

-> 10) func prepare is called

-> 11) second view is displayed <strong>(the user gets correct response because value of var button has been changed in step7)</strong>

Let me know if you have any other questions. Have fun. : )

Recommend

  • How to access the parent view controller?
  • jQuery Boostrap Carousel Show content according to current slide
  • Using a SeekBar within a ListView
  • Linq query with table joins,case statements, count, group by clauses
  • Dismissing Popover View with Storyboard Segues
  • Preparing for segue in embedded tableView in Swift
  • Xcode : Storyboard and retaining data in each controller
  • Delegation - Change variables from different viewsControllers
  • How do I access text in UITextField from another ViewController (Swift)?
  • UISplitViewController not calling delegate methods while pushing new detailView
  • TYPO3 show content from subpages inlcuding css classes
  • How would you save(append) data from one View Controller to another View Controller when you segue f
  • unrecognized selector sent to instance error when a button is clicked
  • Passing inputs to program prompt in a batch file
  • PHP PDO Update prepared statement problem
  • Apple Watch page based interface overflowing on initial controller
  • jQuery, Calling multiple animations in a row on click
  • Inserting NULL/empty string using libpqxx library
  • segue from landscape only to portrait only
  • jquery code not working without breakpoint
  • Google Maps api v3 get start and end coordinates of a street
  • c# open webrowser in many tab
  • Should I or shouldn't I use the CachingConnectionFactory with hornetq 2.4.1
  • How to Cache Real-time Data?
  • Swift: Switch statement fallthrough behavior
  • How to test if a URL from an Eclipse bundle is a directory?
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Django rest serializer Breaks when data exists
  • Is it possible to access block's scope in method?
  • Checking free space on FTP server
  • Can I check if a recipient has an automatic reply before I send an email?
  • JFileChooser in front of fullscreen Swing application
  • Delete MySQLi record without showing the id in the URL
  • align graphs with different xlab
  • Return words with double consecutive letters
  • AT Commands to Send SMS not working in Windows 8.1
  • unknown Exception android
  • Reading document lines to the user (python)
  • failed to connect to specific WiFi in android programmatically
  • Python/Django TangoWithDjango Models and Databases