[英]Passing an object to selector action
Ok simple question but can't find an answer. 好的简单问题,但是找不到答案。
I'v got a button to save information in my app. 我有一个按钮可以在我的应用程序中保存信息。 I have
我有
NSMutableArray *textFields = [[NSMutableArray alloc] initWithCapacity:5];
UITextField *textField = nil;
This is the information i want to save, i'v got 5 textfields in textFields mutablearray. 这是我要保存的信息,我在textFields mutablearray中有5个textfields。
[save addTarget:self action:@selector(saveInfo)
forControlEvents:UIControlEventTouchUpInside];
and 和
-(void)saveInfo {
[[[NSUserDefaults standardUserDefaults] setValue: ????? forKey:@"Phone"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
The question is how to access and get information from like textFields[1].text in my saveInfo void ? 问题是如何从saveInfo void中的textFields [1] .text之类访问和获取信息?
Ok To get things a little bit clearer i'v added the whole class. 好吧,为了使事情更清楚一点,我添加了整个课程。 its not very big, and maybe someone could see thats the problem with my implementation .
它不是很大,也许有人可以看到我的实施存在问题。
@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
What you'd want to do if you are using interface builder is create a bunch of IBOutlet
for your textfields instead of keeping them in an array. 如果要使用接口构建器,您想要做的是为文本字段创建一堆
IBOutlet
,而不是将它们保留在数组中。 Check out this: tutorial 看看这个: 教程
Now it looks like you're creating things by hand, so in this case, you probably just want to declare your array as @property
so it can be accessed by your save method. 现在看来您正在手动创建事物,因此在这种情况下,您可能只想将数组声明为
@property
以便可以通过save方法对其进行访问。
要从数组访问UITextField
的text
属性,应编写:
((UITextField *)[textFields objectAtIndex:1]).text
If you want to access the all textfield, 如果要访问所有文本字段,
for(int i=0;i<[textFields count];i++) {
UITextField *tempTxtField=[textFields objectAtIndex:i];
NSlog(@"%@",tempTxtField);
}
-(void)saveInfo {
NSMutableArray *tempArr = [NSMutableArray array];
for(int i = 0; i < [textFieldsArray count]; i++){
[tempArr addObject:[(UITextField*)[textFieldsArray objectAtIndex:i]text]];
}
[[[NSUserDefaults standardUserDefaults] setValue:tempArr forKey:@"Phone"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
I'm not entirely sure I understand your question, but this is what I would do in your place. 我不能完全确定我是否理解您的问题,但这就是我会在您的位置做的事情。 Instead of creating an array to hold each of the textfields, I'd just assign them a custom tag.
与其创建一个数组来保存每个文本字段,不如给它们分配一个自定义标签。 Any number will work (although avoid zero since that's the default tag on all views).
任何数字都可以使用(尽管避免为零,因为这是所有视图上的默认标记)。 So your init method would look like this:
因此,您的init方法如下所示:
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.tag = (i+1);
[textField setBorderStyle:(UITextBorderStyleRoundedRect)];
[TableControll.view addSubview:textField];
[textField release]; textField = nil;
}
Then in the code that you want to reference the text field, you'd retrieve the view by its tag, making sure to cast it as UITextField. 然后,在要引用文本字段的代码中,通过其标签检索视图,确保将其强制转换为UITextField。
-(void)saveInfo { -(void)saveInfo {
for(int i=0;i<5;i++) {
UITextField *textField = nil;
UIView *view = [self.view viewsWithTag:i];
if ([view isKindOfClass:[UITextField class]]) {
textField = (UITextField *) view;
NSLog(@"do it %@",textField.text)
}
}
} }
NOTE: I'm referencing self.view but you'll need to reference TableControll.view. 注意:我引用的是self.view,但是您需要引用TableControll.view。 You'll need to retain a reference to it somewhere in your code.
您需要在代码中的某个地方保留对它的引用。 Also, I'd recommend your start using ARC.
另外,我建议您开始使用ARC。
Hope that helps! 希望有帮助! Good luck!
祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.