好的简单问题,但是找不到答案。

我有一个按钮可以在我的应用程序中保存信息。
我有

  NSMutableArray *textFields = [[NSMutableArray alloc] initWithCapacity:5];
    UITextField *textField = nil;


这是我要保存的信息,我在textFields mutablearray中有5个textfields。

[save addTarget:self action:@selector(saveInfo)
 forControlEvents:UIControlEventTouchUpInside];




-(void)saveInfo {
    [[[NSUserDefaults standardUserDefaults] setValue: ????? forKey:@"Phone"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


问题是如何从saveInfo void中的textFields [1] .text之类访问和获取信息?

好吧,为了使事情更清楚一点,我添加了整个课程。它不是很大,也许有人可以看到我的实施存在问题。

@implementation Settings

- (id)init: (TableViewController*) TableControll {
  NSMutableArray *textFields = [[NSMutableArray alloc] initWithCapacity:5];
    UITextField *textField = nil;

    for (int i = 0; i < 3; i++) {
        textField = [[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f+(i*35), 120.0f, 30.0f)];
        textField.backgroundColor = [UIColor whiteColor];
        [textField setBorderStyle:(UITextBorderStyleRoundedRect)];
        [TableControll.view addSubview:textField];

        [textFields addObject:textField];
        [textField release]; textField = nil;
    }
    UITextField *textName = textFields[0];
    textName.placeholder = @"Vardas";

    UITextField *textNo = textFields[1];
    textNo.placeholder = @"Telefonas";
    textNo.keyboardType = UIKeyboardTypeNumberPad;
    UITextField *textPin = textFields[2];
    textPin.keyboardType = UIKeyboardTypeNumberPad;
    textPin.placeholder = @"Pin";

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(150, 20, 160, 30);
    [button setTitle:@"Advanced settings" forState:UIControlStateNormal];
    [TableControll.view addSubview:button];
    UIButton *save = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    save.frame = CGRectMake(150, 60, 160, 30);
    [save setTitle:@"Save settings" forState:UIControlStateNormal];
    [TableControll.view addSubview:save];
    [button addTarget:self action:@selector(goAdvanced)
     forControlEvents:UIControlEventTouchUpInside];
    [save addTarget:self action:@selector(saveInfo)
     forControlEvents:UIControlEventTouchUpInside];

    return self;
}

-(void)goAdvanced {
    AppDelegate *newControll = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [newControll ChangeController];
}

-(void)saveInfo {

    for(int i=0;i<5;i++) {
        UITextField *tempTxtField=[_textFields objectAtIndex:i];
        NSLog(@"do it %@",tempTxtField.text);
    }

}

@end

最佳答案

如果要使用接口构建器,您要做的就是为文本字段创建一堆IBOutlet,而不是将它们保留在数组中。查看以下内容:tutorial

现在看来您正在手动创建事物,因此在这种情况下,您可能只想将数组声明为@property,以便可以通过save方法对其进行访问。

07-26 09:42