0

我有UITextField名为textField ,用户可以在其中使用NSUserDefaults保存电话号码。然后我让IBAction拨打该号码。如何将用户已保存到UITextField的数字放入该操作?

-(IBAction)callPhone:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:123456"]];
}

更新

- (IBAction) saveBtnPresssed : (id) sender
{
    myString1 = [[NSString alloc] initWithFormat:textField.text];
    [textField setText:myString1];
    NSUserDefaults *stringDefault = [NSUserDefaults standardUserDefaults];
    [stringDefault setObject:myString1 forKey:@"stringKey"];
}


- (void)viewDidLoad
{
    [textField setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"stringKey"]];
    [super viewDidLoad];
}
4

2 回答 2

0

您要查找的字符串由以下方式返回:

[textField text];

如果您将其保存为用户默认值,那么您可以:

-(IBAction)callPhone:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *phoneNumberString = [defaults objectForKey:@"key_you_saved_the_number_with"];

//... do what you need with that number here

}

编辑:

    [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@"], [defaults objectForKey:@"stringKey"]];

所以结果字符串应该是“tel:”+文本字段中的实际数字而不是“tel:123456”

于 2012-05-02T12:24:35.960 回答
0

在您的代码设置中,不需要使用相同字符串的文本文件。只需使用:

 (IBAction) saveBtnPresssed : (id) sender
{
    myString1 = [[NSString alloc] initWithFormat:textField.text];
    NSUserDefaults *stringDefault = [[NSUserDefaults standardUserDefaults] setObject:myString1 forKey:@"YOUR_KEY"];
}

这会将字符串设置为您的默认值,您可以将数据获取为

NSString *pString = [defaults objectForKey:@"YOUR_KEY"];
于 2012-05-02T12:49:45.567 回答