在iPhone中单击按钮时如何将文本字段/标签中的值添加到数组 [英] How add values from text field / label to an array when a button click in iphone
问题描述
我有一个文本字段,用户可以输入1-10之间的值
I have a text field In that user can enter values from 1-10
在文本字段底部,我有一个按钮.
at the bottom of the text field i have button.
当用户单击完成"按钮时,我需要将文本中的值添加到数组中
i need to add the values in text filed to array when user clicks done button
为此,我编写了这样的代码.
for this i wrote code like this.
- (void)viewDidLoad
{
[super viewDidLoad];
PainlevelsArray = [[NSMutableArray alloc] init]; //PainlevelsArray declared in .h
}
-(IBAction)doneClick:(id)sender
{
[PainlevelsArray addObject:txtField.text ];
// [PainlevelsArray insertObject:rateLabel.text atIndex:0 ];
NSLog(@"PainlevelsArray *** %@",PainlevelsArray);
[self.navigationController popToRootViewControllerAnimated:YES];
}
但是,每个时间数组仅在单击按钮时才在日志中仅显示最近添加的一个值,像这样
But every time array displays only the recently added one value only in log when button clicks, like this
控制台上的输出
Output on Console
======================
========================
2012-05-07 10:11:15.689 ESPT[796:10d03] PainlevelsArray *** (
2
)
2012-05-07 10:11:27.984 ESPT[796:10d03] PainlevelsArray *** (
4
)
My array gets empty every time view loads, and adde a fresh entry when button clicks
但是我需要这样
2012-05-07 10:11:27.984 ESPT[796:10d03] PainlevelsArray *** (
4,
5,
2,
6,
8,
)
所有进入的路径都添加/插入到现有阵列中.
All the velues enters are adde/inserted to the existing array.
预先感谢
在ExerciseStartViewController中
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ExerciseResult *result = [[ExerciseResult alloc] init];
[self.navigationController pushViewController:result animated:YES];
[result release];
}
当前/ExerciseResult视图控制器中的
- (void)viewDidLoad
{
[super viewDidLoad];
PainlevelsArray = [[NSMutableArray alloc] init]; //PainlevelsArray declared in .h
}
-(IBAction)doneClick:(id)sender
{
[PainlevelsArray addObject:txtField.text ];
// [PainlevelsArray insertObject:rateLabel.text atIndex:0 ];
NSLog(@"PainlevelsArray *** %@",PainlevelsArray);
[self.navigationController popToRootViewControllerAnimated:YES];
}
推荐答案
您需要使用全局数组.
最好的解决方案是在AppDelegate.h文件中声明一个数组,然后在applicationdidfinishlaunch方法中对该数组进行分配初始化.
Best possible solution is declare an array in AppDelegate.h file and alloc-init this array in applicationdidfinishlaunch method.
AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[app.arrMutable addObject:textField.text ];
使用UserDefaults :用于将您的应用程序数据保存为键值格式.除非您尚未将其从沙箱中删除,否则存储为用户默认值的值/对象始终保留在应用程序中.您可以使用以下代码将数组存储在userdefaults中.
Use UserDefaults: These are used to save your application data in key-value format. The values/objects stored as userdefaults always remain in application, until you have not removed it from sandbox. You can use following code to store an array in userdefaults.
[[NSUserDefaults standardUserDefaults] setObject:app.arrMutable forKey:@"Global_Array_Key"];
在AppDelegae applicationDidFinishLuanch方法中,您可以使用以下命令从userdefaults中检索此数组:
In AppDelegae applicationDidFinishLuanch method you can retrieve this array from userdefaults using:
self.arrMutable = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:@"Global_Array_Key"]];
这篇关于在iPhone中单击按钮时如何将文本字段/标签中的值添加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!