在我的第一个视图控制器中,我有 3 个输入字段,每个字段都将用户输入输入并将其保存到一个字符串中,例如:地址、用户名和密码为NSUserDefaults
. 这部分工作正常。在我的第二个视图控制器中,我试图从第一个控制器(地址、用户名和密码)中获取 3 个字符串,基于 3 个字符串创建一个 html 链接。我尝试了很多方法来访问 3 个字符串,但没有成功,我得到的结果是NULL
. 这是我的代码:
//.h 文件 - 具有 3 个输入字段的第一个视图控制器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 文件 - 第一个视图控制器 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 第二个视图控制器 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秒查看LiveView.m文件
#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