-1

I am working in xcode 7.2 trying to make a project. So, I have two viewcontrolers: In the first I have 5 buttons. In the second I have a label and a segmented control.

So what I wanted to do is, to change the label and the segmented control title (title of the buttons) according to the button I press in the First VIewController.

For example If I press button A, in the secondview controller the label will say "You pressed button A" and the 3 Buttons of the segmented control should say "A","B","C". But If I press button B, in the secondview controller the label will say "You pressed button B" and the 3 Buttons of the segmented control should say "D","E","F".

Can anyone help me?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
fr0zt
  • 733
  • 4
  • 12
  • 30
  • Please check this [answer](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers). Basically you use `prepareForSegue`. – Raphael Oliveira Dec 24 '15 at 01:26
  • Can you be a little more specific please? I am trying to do that... – fr0zt Dec 24 '15 at 01:32
  • 1
    Then you should **edit** your question to show the code you are trying. – Paulw11 Dec 24 '15 at 01:37
  • I don't know how to make the button click detection... How do I detect which button I pressed in the first VC? And, I was thing to send a integer to the 2 VC. And in the second VC if I received 1 I would know that I pressed button 1, if I received 2 I know I pressed button 2, and than change the labels... I don't know If it's the best way... Neither how to do it. – fr0zt Dec 24 '15 at 01:42
  • Have you completed any basic tutorials on iOS app development? You need to implement @IBAction methods (for storyboards) or add an action handle to your button if you are creating the button programatically or you could use the `sender` argument of `prepareForSegue` if you are using direct segues from your buttons. It seems that you have a lot of things you need to solve. – Paulw11 Dec 24 '15 at 01:51
  • @hnegrao you will have to display the code you have written so we can better help you – Sana Dec 24 '15 at 01:52
  • I'll try to explain a little more what I have done: I have made some buttons in VC1 and some other in VC2 and also labels. In the VC1, when I press button 1 it sends me to VC2. This is done. I'd like it to do the same using the others buttons (button 2 -> VC2, button 3 -> VC2). But I'd like to change the labels according to button pressed in VC1. I could do it using a VC for each button but I think I shouldn't do it... – fr0zt Dec 24 '15 at 01:58

3 Answers3

0

Create a protocol on the first View Controller that is confirmed my the second view controller so whenever you press a button in first view controller you can use that delegate and inform the other view controller of the changed view state.

Sana
  • 9,895
  • 15
  • 59
  • 87
  • Can you be a little more specific please? – fr0zt Dec 24 '15 at 01:32
  • 1
    A protocol isn't necessary, a simple property that you set via `prepareForSegue` is sufficient – Paulw11 Dec 24 '15 at 01:32
  • @Paulw11 `prepareForSegue` does it once(that is passes forward) before instantiating the VC right? OP wants an interaction(passing back and forth!) between the VCs that keeps on interacting? Either KVO or Delegation seems to be a better way no? – Sana Dec 24 '15 at 01:45
  • No, `prepareForSegue` is executed after the view controller is instantiated. If it wasn't then you wouldn't be able to retrieve the `destinationViewController` property. My assumption is that they are transitioning from VC1 to VC2, so there is no need for KVO nor delegation because once VC2 is displayed, the user can't be interacting with VC1 (unless they return to VC1 in which case `prepareForSegue` will fire again as they move to VC2). If both VC1 and VC2 are active at the same time (say on different tabs of a tabbarcontroller) then a protocol would be appropriate – Paulw11 Dec 24 '15 at 01:47
0

You can pass data through a segue.

In the view controller you want to go to, setup a variable called title.

Second View Controller:<br>
    var vcTitle:String = ""

Then in your first VC add:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
  if (segue.identifier == "secondViewControllerSegue") {
    // pass data to next view
    // Create a variable called pressedButtonTitle that is set after the user presses a segment
    let destinationController = segue.destinationViewController as! SecondViewController;
    destinationController.vcTitle = pressedButtonTitle;


  }
}
Idan Magled
  • 2,186
  • 1
  • 23
  • 33
user3567080
  • 301
  • 1
  • 2
  • 10
  • Done that and got this error: class AppDelegate: UIResponder, UIApplicationDelegate { Thread1: Signal SIGABRT – fr0zt Dec 24 '15 at 02:43
  • How can I set pressedButtonTitle? With an IBAction? – fr0zt Dec 24 '15 at 13:04
  • I got this error when Implementing your code: 2015-12-24 13:54:09.357 ConversorXY[2066:60040] -[ConversorXY.SecondViewController BotaoPeso:]: unrecognized selector sent to instance 0x79ea9b60 2015-12-24 13:54:09.366 ConversorXY[2066:60040] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ConversorXY.SecondViewController BotaoPeso:]: unrecognized selector sent to instance 0x79ea9b60' *** First throw call stack: ( 0 CoreFoundation 0x002caa14 __exceptionPreprocess + 180 – fr0zt Dec 24 '15 at 13:57
  • I think I solved it. I had made UIBaction for the button, than deleted it but no completly... – fr0zt Dec 24 '15 at 14:04
0

To be more specific about how to use prepareForSegue:

One option is:
1. Give your segue an identifier on storyboard
2. In prepareForSegue, check segue.identifier to see if it matches the appropriate identifier that you just set
3. If it matches, then you can access the destination view controller, like so:

var vc = (ViewNoteViewController) segue.destinationViewController  

4. Use vc and access your labels and segmented controls from there (assuming you've made IBOutlets)

Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62