ipatch
ipatch

Reputation: 4033

Passing data between view controllers - iOS Xcode - what's a good way?

Alright, I'll try and make this as simple as possible (I was seeming to get the run around on chat.stackoveflow.com when I tried asking this question). I want to pass the text from a textfield in one viewcontroller to another viewcontroller.

Should I use a Model class and store the the textfield.text in a Model.[h/m] files and then have the second view controller access the data stored in the model?

Basically this is what I have,

ViewControllerWelcome.h

@interface ViewControllerWelcome : UIViewController { }

@property (weak, nonatomic) IBOutlet UITextField *textFieldUsername;

ViewControllerWelcome.m

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
                ViewControllerHome *home = (ViewControllerHome *)[storyboard instantiateViewControllerWithIdentifier:@"Home"];
                NSString *username = _textFieldUsername.text;
                home.lblUSERNAME.text=username;
                [self presentModalViewController:home animated:YES];

ViewControllerHome.h

@interface ViewControllerHome : UIViewController {
NSString *username;
UILabel *lblUSERNAME;
}

@property (weak, nonatomic) IBOutlet UILabel *lblUSERNAME;

ViewControllerHome.m

- (void)changeUSERNAME {

 // get username from welcome tf
ViewControllerWelcome *welcome = [[ViewControllerWelcome alloc] init];
 welcome.username = [self lblUSERNAME.text];
 // _lblUSERNAME.text = welcome._textFieldUsername.text;
 //welcome.textFieldUsername.text = _username;
 // username = welcome.textFieldUsername.text;
 NSLog(@"username = %@",username);
// welcome.textFieldUsername.text = _lblUSERNAME.text;
// NSLog(@"username = %@",welcome.textFieldUsername.text);
}    

As you can see I tried several different things, but couldn't come up with a working solution :-l

Upvotes: 1

Views: 17350

Answers (4)

ipatch
ipatch

Reputation: 4033

I refined this question with another question on SO that can be found here Basically, I created a singleton to pass the data between the view controllers. I haven't had any problems with it so far (knocks on wood).

Upvotes: 0

carl_h
carl_h

Reputation: 2193

Since you're using Storyboards, it makes more sense to be using Segues to perform the transition between your ViewControllers. If you're not familiar with segues you can have a look here:

http://developer.apple.com/library/IOs/#featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomSegues/CreatingCustomSegues.html

It's very simple to create a segue in IB. Once you have that set up you implement
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender and configure your destination view controller. So you'll have something like the following.

-(void)prepareForSegue(UIStoryboardSegue *)segue sender:(id)sender{
    // Assume you have a viewHomeSegue defined that has the name of the segue you want to perform
    NSString * segueIdentifier = [segue identifier];
    if([segueIdentifier isEqualToString:viewHomeSegue]){
        ViewControllerHome * homeController = (ViewControllerHome *)[segue destinationViewController];
        homeController.lblUSERNAME.text = _textFieldUsername.text;
    }
}

Upvotes: 6

tomidelucca
tomidelucca

Reputation: 2543

if you have a .storyboard file you can layout your views in there like with interface builder.

This documents really helped me out the first time:

Part 1: http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

Part 2: http://www.raywenderlich.com/5191/beginning-storyboards-in-ios-5-part-2

If you only want to check out the segues part, I'd recommend you to read part 2 only.

Upvotes: 0

Norbert
Norbert

Reputation: 4291

You shouldn't keep strong references between those two ViewControllers in both directions. What you can do, for example, is to declare a protocol in your ViewControllerWelcome with a delegate in it. You could let the ViewControllerHome instanciate the other controller and then be its delegate which gets informed as soon as the user finished entering the username.

@protocol ViewControllerWelcomeDelegate <NSObject>    
- (void) userNameEntered:(NSString *)userName;    
@end

@interface ViewControllerWelcome : UIViewController
@property (nonatomic, weak) id <ViewControllerWelcomeDelegate> delegate;
...
@end

In your @implementation:

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
   [theTextField resignFirstResponder];
   [delegate userNameEntered:theTextField.text];

   return YES;
}

Upvotes: 0

Related Questions