0

Possible Duplicate:
How do I pass variables between view controllers?

I have tabbar application, tabbar application has two ViewControllers. In first view controller I have for example uitextfield and button(save button), in second view controller I have UITableView. I want, text from uitextfield sent and insert to UITableView if button was pressed.

Community
  • 1
  • 1
Invader
  • 23
  • 10
  • I found one method, with help of `NSNotificationCenter`, but this method work only on `ViewDidLoad` or `viewWillAppear:`... – Invader Sep 06 '11 at 08:38
  • I found solution, this solution very simple I us NSUserDefaults. All file.m NSArray *favorites1 = [[NSUserDefaults standardUserDefaults] arrayForKey:@"favs1"]; NSMutableArray *newFavs1 = [[NSMutableArray alloc] init]; for (NSString *string in favorites1) { [newFavs1 addObject:string]; } [newFavs1 addObject:[NSString stringWithFormat:@"%@", [textField text]] ]; [[NSUserDefaults standardUserDefaults] setObject:newFavs1 forKey:@"favs1"]; secondfile.m arrayOfWords = [[NSUserDefaults standardUserDefaults] objectForKey:@"favs1"]; – Invader Sep 10 '11 at 06:10

4 Answers4

1

Well, Save the text of the uitextfield from first view controller to your data model. Then update UITableView in second view controller with that data. You might want to check out the UITableViewSource and UITableViewDelegate documents.

Luffy
  • 11
  • 1
1

in YourFirstViewController

YourController2 = [[YourController2 alloc] initWithNibName:@"YourController2" bundle:nil];  
YourController2.yourString = @"string";

in YOurSecondViewController

.h file

NSString            *yourString;
@property (nonatomic, retain) NSString  *yourString;

.m file

 @synthesize yourString;
PJR
  • 13,052
  • 13
  • 64
  • 104
  • 1
    `View2ViewController *variable = [[View2ViewController alloc] initWithNibName:@"View2ViewController" bundle:nil]; variable.stringToTransfer = text;` string always NULL – Invader Sep 05 '11 at 10:43
  • [self presentModalViewController:variable animated:YES]; but if I use this, I insert text in text field but after button pressed I go to View2ViewController and my tabbar hides :( – Invader Sep 05 '11 at 10:52
  • you have to access it in viewcontroller 2. in viewdidload of viewcontroller 2 write NSLog(@"string is :%@",self.yourString);it will show you string. – PJR Sep 05 '11 at 10:53
  • with this [self presentModalViewController:variable animated:YES]; works perfect, but after save I go only to tableview without tabbar – Invader Sep 05 '11 at 11:07
0

If you don't want to pass references of the first controller to the second you could instead create a string variable in your app delegate and use that for communicating between the two.

You can access the App Delegate programmatically without passing references about it in your controllers.

Oscar Del Ben
  • 4,485
  • 1
  • 27
  • 41
0

The simplest way is to setup extern variables for example but I always prefer property over it.

Community
  • 1
  • 1
Prabh
  • 2,466
  • 2
  • 24
  • 27