-4

I'm stuck with the following:

There are two view controllers, viewController1 and viewController2.

In viewController1, a UIButton, UITextField are created programmatically in viewDidLoad, and the button views viewController2 when pressed.

Is there a way for viewController2 to fetch data from the UITextField in viewController1 when it shows up.

Because of I'm new to programming, I'd appropriate an answer with detailed explanations.

Thank you..

karthik
  • 621
  • 4
  • 14
  • Declare a property in the.h file for ViewController2: NSString *data, then when you are ready to push view controller 2, create an instance of it, then call viewController2.data = viewController1.textField.text. Now view controller 2 has the data from viewController1. This question has been asked a lot of times, so I don't want to put this as an aswer. Here's a reference to a similar question: http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Zhang Oct 23 '14 at 06:08
  • try to search before you ask question : http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Huy Nghia Oct 23 '14 at 06:10
  • Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – karthik Jun 08 '18 at 12:02

2 Answers2

-1

Make a property in you VC2 like this:

@property(nonatomic,strong) NSString* yourData;

In the button press event in you VC1, do this:

ViewController2 *viewController2 = [[ViewController2 alloc] initWithNib:@"ViewController2" bundle:nil];
viewControllerB.yourData = myTextfield.text;
[self pushViewController: viewController2 animated:YES];

Hope this helps... :)

Rashad
  • 11,057
  • 4
  • 45
  • 73
-1

Actually, there are many solutions to your question.

  1. Pass data using segues.

  2. Pass data using properties declared in the app delegate file.

Now, both these things have different uses depending upon your need. You should learn these things in details. Explaining them here is not a good idea.

http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/ http://www.cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html

These links should help you understand.

Solution to your problem:

In AppDelegate.h (application Delegate file) file, declare a property.

@property NSString *name;

Synthesize it in AppDelegate.m (application Delegate file).

@synthesize name;

Now, in ViewController1.h declare AppDelegate object.

AppDelegate *delegate;

In viewDidLoad of ViewController1.m type this:

delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];

Now, in the button press event in ViewController, add this.

//get data from textField and assign it to delegate.name
//delegate.name = textField.text;

In viewController2 create a delegate object and access the value.

//Declare a delegate object in viewController2 and create the delegate object in viewDidLoad of viewController2

//Use delegate.name
//NSLog(@"%@",delegate.name);

I hope it helps.

Saumya
  • 180
  • 3
  • 8