I'm trying to pass a string from my first ViewController to my second ViewController but it returns NULL
- by Dashony
In my first view controller I have 3 input fields each of them take the user input into and saves it into a string such as: address, username and password as NSUserDefaults. This part works fine.
In my second view controller I'm trying to take the 3 strings from first controller (address, username and password) create a html link based on the 3 strings. I've tried many ways to access the 3 strings with no luck, the result I get is NULL.
Here is my code:
//.h file - first view controller with the 3 input fields CamSetup.h
#import <UIKit/UIKit.h>
@interface CamSetup : UIViewController <UITextFieldDelegate>
{
NSString * address;
NSString * username;
NSString * password;
IBOutlet UITextField * addressField;
IBOutlet UITextField * usernameField;
IBOutlet UITextField * passwordField;
}
-(IBAction) saveAddress: (id) sender;
-(IBAction) saveUsername: (id) sender;
-(IBAction) savePassword: (id) sender;
@property(nonatomic, retain) UITextField *addressField;
@property(nonatomic, retain) UITextField *usernameField;
@property(nonatomic, retain) UITextField *passwordField;
@property(nonatomic, retain) NSString *address;
@property(nonatomic, retain) NSString *username;
@property(nonatomic, retain) NSString *password;
@end
//.m file - first view controller CamSetup.m
#import "CamSetup.h"
@interface CamSetup ()
@end
@implementation CamSetup
@synthesize addressField, usernameField, passwordField, address, username, password;
-(IBAction) saveAddress: (id) sender
{
address = [[NSString alloc] initWithFormat:addressField.text];
[addressField setText:address];
NSUserDefaults *stringDefaultAddress = [NSUserDefaults standardUserDefaults];
[stringDefaultAddress setObject:address forKey:@"stringKey1"];
NSLog(@"String [%@]", address);
}
-(IBAction) saveUsername: (id) sender
{
username = [[NSString alloc] initWithFormat:usernameField.text];
[usernameField setText:username];
NSUserDefaults *stringDefaultUsername = [NSUserDefaults standardUserDefaults];
[stringDefaultUsername setObject:username forKey:@"stringKey2"];
NSLog(@"String [%@]", username);
}
-(IBAction) savePassword: (id) sender
{
password = [[NSString alloc] initWithFormat:passwordField.text];
[passwordField setText:password];
NSUserDefaults *stringDefaultPassword = [NSUserDefaults standardUserDefaults];
[stringDefaultPassword setObject:password forKey:@"stringKey3"];
NSLog(@"String [%@]", password);
}
- (void)viewDidLoad
{
[addressField setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"stringKey1"]];
[usernameField setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"stringKey2"]];
[passwordField setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"stringKey3"]];
[super viewDidLoad];
}
@end
//.h second view controller LiveView.h
#import <UIKit/UIKit.h>
#import "CamSetup.h"
@interface LiveView : UIViewController
{
NSString *theAddress;
NSString *theUsername;
NSString *thePassword;
CamSetup *camsetup; //here is an instance of the first class
}
@property (nonatomic, retain) NSString *theAddress;
@property (nonatomic, retain) NSString *theUsername;
@property (nonatomic, retain) NSString *thePassword;
@end
//.m second view LiveView.m file
#import "LiveView.h"
@interface LiveView ()
@end
@implementation LiveView
@synthesize theAddress, theUsername, thePassword;
- (void)viewDidLoad
{
[super viewDidLoad];
theUsername = camsetup.username; //this is probably not right?
NSLog(@"String [%@]", theUsername); //resut here is NULL
NSLog(@"String [%@]", camsetup.username); //and here NULL as well
}
@end