2

Possible Duplicate:
Passing Data between View Controllers

iOS dev noob here..

I have a bunch of buttons on my main ViewController, what I want to happen is when the user clicks on one of those buttons, I want it to take them to a separate view that has a label and I want the text in that label to change according to which button they press.

So basically I want to be able to pass data from a button press on the main ViewController to a label in a second view.

That might be a bit confusing and I apologize, but any help would be greatly appreciated.

Community
  • 1
  • 1
Ben T.
  • 39
  • 1
  • 5
  • Could you elaborate as to your current project setup... Do you have a navigation controller for example ? – Daniel Aug 13 '12 at 18:25
  • This question is asked over and over again... http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Joe Aug 13 '12 at 18:31
  • I just noticed the segue tag, which you do not mention storyboard anywhere in your question. See here http://stackoverflow.com/questions/7937035/how-pass-data-in-seque-ios5-storyboard-uitableview-to-detail-view – Joe Aug 13 '12 at 18:35
  • Daniel - I do not have a navigation controller. It's just a single view application. It's a very basic app. I have buttons on the main view, and what I want to happen is when a user clicks on a button, it sends certain text to a label that is sitting in the second view. – Ben T. Aug 13 '12 at 18:38

2 Answers2

1

I would suggest you use a storyboard if possible. You then can ctrl drag from each button to a new viewcontroller that you drag out onto the storyboard. This will setup a segue and you can use the method prepareforseguie to pass the data you need to use in the new view controller.

DirectX
  • 922
  • 10
  • 14
  • If you look at the tags, which I just noticed since it is nowhere in the question, it looks like OP is using segues. – Joe Aug 13 '12 at 18:36
  • Look at my post in http://stackoverflow.com/questions/11885673/how-to-pass-data-back-from-one-view-to-other-view-in-ios/11886609#11886609 to see how to do that. – Cthutu Aug 13 '12 at 18:37
0

Assuming your view controller with the buttons is in a navigation controller - presumably at the root...

What you would do is, add a target to each of your buttons, in code this can be done with the method addTarget:action:forControlEvents: of your UIButton

For example:

[myBtn addTarget:self action:@selector(tappedButton:) forControlEvents:UIControlEventTouchUpInside];

The method tappedButton will be messaged with the button that was tapped:

- (void)tappedButton:(UIButton*)sender{
    // exploit the button
}

Inside this method you can get the title of the button - myBtn.titleLabel.text

You can then create a new view controller (let's keep things simple and say you have your own UIViewController subclass called MySimpleViewController.

In this class you have a cameFrom property which you can set the button's title on, and in viewDidLoad of MySimpleViewController, you would get the property value of cameFrom, this could be the method implementation.

- (void)tappedButton:(UIButton*)sender{
    MySimpleViewController *detail = [[MySimpleViewController alloc] init];
    detail.cameFrom = sender.titleLabel.text;

    [self.navigationController pushViewController:detail 
                                         animated:YES];
    [detail release];
}

So over in your MySimpleViewController's viewDidLoad, you create a UILabel, give the text property the value of self.cameFrom and add it to the view with addSubview:

Daniel
  • 23,129
  • 12
  • 109
  • 154