2

I have two tabs in a tab bar controller for a user registration activity. I need to get some UITextFields' value from the "First Tab" once the user clicks the register button in the "Second Tab".

So, is it possible to directly access to a textfield that's in another view? or what else I can do to solve?

Using delegate/notification seems another way but gone through all the articles/question related to delegate but couldn't figured out how to implement it to my code. If so, coud you please share a good article about how to implement delegates.

Thanks in advance.

snns
  • 93
  • 2
  • 7
  • Hmm, this may be not the perfect solution, but I suppose it would work using NSUserDefaults.. Just save it in first tab, and once you clicked button in second tab just access NSUserDefaults – Yanchi Jul 29 '13 at 13:17
  • Yep, that was the first solution I came up with but it turned out I couldn't implement a delegate to use didSelectViewController as here: http://stackoverflow.com/questions/10891182/how-to-implement-didselectviewcontroller – snns Jul 29 '13 at 13:41
  • Lucas's solution looks best, however what I was talking about was using NSUserDefaults. So in first controller you would save your values like [[NSUserDefaults standardUserDefaults] setObject:yourTextField.text forKey:@"yourVariable"]; then synchronize it [[NSUserDefaults standardUserDefaults] synchronize]; and in your second tab view, you would just check if value exists like if([[NSUserDefaults standardUserDefaults] objectForKey:@"yourVariable"]){ yourSecondTextField.text = [[NSUserDefaults standardUserDefaults] objectForKey@"yourVariable];} else { yourSecondTextField.text = @""} – Yanchi Jul 29 '13 at 17:47
  • 1
    But I'm pretty sure, that is not what NSUserDefaults are meant for, it's just first thing that came to my mind :) that would work – Yanchi Jul 29 '13 at 17:48
  • As Yanchi himself already said, NSUserDefault it's not really for this kind of problem. Your viewControllers are all in memory, and access an position of your memory it's really faster and better than save something in disk than retrieve this later just to get the reference, and not actually persisting it. – Lucas Eduardo Jul 29 '13 at 22:45
  • if the solution worked, please upvote and accept the answer, in order to remove your question from the list of unanswered question – Lucas Eduardo Jul 30 '13 at 00:49
  • Thanks @Yanchi your answer look like acceptable but as you said Lucas's one is good enough :) – snns Jul 30 '13 at 08:36
  • NP snns, and I'm upvoting lucas too :) – Yanchi Jul 30 '13 at 08:46

1 Answers1

4

You can access the other view controllers through the property self.tabBarController of your viewControllers inside the tab bar.

In the action method of your register button, inside secondTab's view controller, just do this:

UIViewController1 *myViewController = [self.tabBarController.childViewControllers objectAtIndex:0];
something = myViewController.textField;

Note that the index '0' it's because I am assuming that's really the first tab that you want to access. Change for another index if you desire.

Please, give some feedback if that's what you were looking for.

Lucas Eduardo
  • 11,525
  • 5
  • 44
  • 49
  • Thanks a lot, your answer definetely solved the problem. Apologizes for the delay, I thought gave you up vote but seems couldn't do it coz the rep point. – snns Jul 30 '13 at 08:34