0

I've put together a text field that can take text and save it and output it into a label on the same controller page. My question is how do I use another controller page (using tab view controller) and output the same text on the second controller.

I've linked the label as the same IBOulet that the text is saved as.

Below is my code for the firstcontroller.m

#import "FirstViewController.h"

@interface FirstViewController ()
@property (nonatomic, strong) IBOutlet UILabel* label;
@end

@implementation FirstViewController
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    self.label.text = @"";
    return TRUE;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    self.label.text = textField.text;
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    textField.text = @"";
}

Thanks in advance

rmaddy
  • 314,917
  • 42
  • 532
  • 579
codertoj
  • 43
  • 1
  • 5
  • I think the information you want is here: http://stackoverflow.com/questions/17743107/passing-data-from-view-controller-to-tab-bar-controller-in-ios – chartman Apr 12 '14 at 22:45

2 Answers2

0

You can use the UITabBarDelegate to listen when the user click on another tab to pass the text.

Please check this link Class reference

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item

Or you can check this too Class reference

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

With this you could get the new view controller to assign the value in a good way.

Let me know if it helps you

Gabriel Goncalves
  • 5,132
  • 3
  • 25
  • 34
0

Really, a UITextField is not a place to store data for any significant amount of time, as Model information should not be stored in a UIView. If you'd like this text to be accessible from multiple UIViewControllers, you should create a Model object that can store the string, and persist regardless of what happens with your view controllers.

You can pass this text to the model object by having your UIViewController be a delegate for the UITextField. The UIViewController should then be made to conform to <UITextFieldDelegate> so that you can listen for :

- (void)textFieldDidEndEditing:(UITextField *)textField

When this is called, pass the text to your model. The model could be set up with a Singleton pattern, just have 1 static model object accessible globally. When your app exits, you could write this object to disk using <NSCoding>, so that you can read it back in again next time the app starts.

For non-trivial data, I'd recommend using CoreData.

tl;dr- dont pass text to other controllers directly from the textfield.

Nick
  • 2,361
  • 16
  • 27