我正在为iphone制作一个客户端服务器程序,我想使用我的serverIP,这是我在第一个视图服务器中的第二个视图的一部分.IP是一个uitextfield。我用来在第二个视图中输入ServerIP的值,但我想使用serverIP IN Firstview的值。
“”secondview.h“”接口文件
#import <UIKit/UIKit.h>
@interface secondview : UIViewController {
IBOutlet UIView *view;
IBOutlet UITextField *serverIP;
IBOutlet UITextField *noc;
IBOutlet UIButton *save;
IBOutlet UIButton *back;
IBOutlet UIButton *load;
IBOutlet UILabel *display1;
}
-(IBAction) back;
-(IBAction) save;
-(IBAction) load;
@property (nonatomic,retain) IBOutlet UITextField *serverIP;
@property (nonatomic,retain) IBOutlet UITextField *noc;
@property (nonatomic , retain) IBOutlet UILabel *display1;
@end
“”secondview.m“”实施文件
#import "secondview.h"
@implementation secondview
@synthesize serverIP,noc,display1;
-(IBAction) save{
[[NSUserDefaults standardUserDefaults] setInteger:serverIP forKey:@"save"];
NSUserDefaults *myname = [NSUserDefaults standardUserDefaults];
[serverIP resignFirstResponder];
}
-(IBAction) load {
serverIP = [[NSUserDefaults standardUserDefaults] integerForKey:@"load"];
NSUserDefaults *myname = [NSUserDefaults standardUserDefaults];
}
-(IBAction) back {
[self.parentViewController dismissModalViewControllerAnimated: YES];
}
- (void)dealloc {
[super dealloc];
}
@end
答案 0 :(得分:1)
serverIP是textField。您可以将其作为serverIp.text获取 因此,您可以将其保存为用户默认值
[[NSUserDefaults standardUserDefaults] setObject:serverIp.text forKey:@"save"];
您试图将文本字段本身保存为用户默认值中的整数,这是不正确的。
要检索它,请使用
NSString *serverIpString = [[NSUserDefaults standardUserDefaults] objectForKey:@"save"];
答案 1 :(得分:0)
这样做是为了保存:
[[NSUserDefaults standardUserDefaults] setObject:serverIP.text forKey:@"save"];
并检索:
NSString *serverIPSTR = [[NSUserDefaults standardUserDefaults] objectForKey:@"load"];
答案 2 :(得分:0)
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"load"] != nil) {
serverIP = [[NSUserDefaults standardUserDefaults] objectForKey:@"load"];
} else {
serverIP = @"Your default IP address";
[[NSUserDefaults standardUserDefaults] setObject:serverIP ForKey:@"load"];
}
虽然我会使用不太可能与默认值冲突的东西,而不是“加载”,例如
<强> myappname_serverip 强>