如何将 UITextField 的数据保存到 NSMutableArray 中?
我正在编写一个简单的程序,有两个 UITextFields 和一个 UIButton,当我按下 UIButton 时,会调用一个方法,该方法的编码如下
-(void) saveData{
restaurant_name_save = restaurant_name_textfield.text;
amount_name_save = amount_textfield.text;
order = [[NSMutableArray alloc] initWithObject: restaurant_name_save, amount_name_save, nil];
NSLog(@"%@", order);
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"warning" message:[NSString stringWithFormat:@"%d",[order count]] delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
在 NSLog 中,两个 UITextField 数据都正确显示,但在 UIAlertView 中,它永远显示 2 ,即使当我更改数据并再次按下按钮时...
我应该做什么,我只想将每次按下按钮时的数据保存在 NSMutableArray 中,
请帮忙...
I am writing a simple program having two UITextFields and one UIButton, when I press UIButton, a method is called, the coding of that method is as below
-(void) saveData{
restaurant_name_save = restaurant_name_textfield.text;
amount_name_save = amount_textfield.text;
order = [[NSMutableArray alloc] initWithObject: restaurant_name_save, amount_name_save, nil];
NSLog(@"%@", order);
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"warning" message:[NSString stringWithFormat:@"%d",[order count]] delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
In NSLog, both UITextField data is showing properly, but in UIAlertView, it is ever showing 2, even when I change the data and again press button...
What should I do, I simply want to save the data for each time in NSMutableArray, as I press button,,
please help ....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我可能在这里遗漏了一些东西但是
它说
[order count]
:[order count]
的输出将为2,因为数组中有两个条目。我假设看到您编写的数组的内容
stringWithFormat:@"%@", 顺序
I might be missing something here but
it says
[order count]
:The output of
[order count]
will be 2 because there are two entries in the array.I would assume to see the contents of the array you write
stringWithFormat:@"%@", order
您可能有一个错字。尝试使用
-initWithObjects:
代替:在重复运行时,还要确保
释放
成员变量order
并将其设置为nil< /code>,在执行另一个
+alloc
和-initWithObjects:
调用之前:更好的是,不要重复使用
+alloc
和-initWithObjects:
在此方法中,但在此方法之外(可能在较大对象的init
方法中)创建一个容量为2的NSMutableArray
:在处理该方法的方法中按钮操作,将项目设置在各自的索引处:
You might have a typo. Try using
-initWithObjects:
instead:On repeat runs, also make sure you
release
the member variableorder
and set it tonil
, before doing another+alloc
and-initWithObjects:
call:Better yet, don't repeatedly use
+alloc
and-initWithObjects:
in this method, but outside this method (perhaps in the larger object'sinit
method) create anNSMutableArray
of capacity 2:In the method that handles the button action, set the items at their respective indices:
使用
initWithObjects
代替initWithObject
并始终在数组中设置两个对象,并且您正在打印数组的计数,这就是为什么它总是显示 2。
您编写的所有其他代码都是正确的。
use
initWithObjects
insted ofinitWithObject
and always two objects set in the array and you are printing the count of array thats why it always show 2.
all other code written by you is correct.
如果我没听错的话,您希望有一个数组,用于在每次按下屏幕上的按钮时存储
restaurant_name_save
和amount_name_save
。 (我假设此按钮调用saveData
方法?在这种情况下,重新分配数组将首先清除其中的所有对象,然后将两个字符串添加到其中。您应该将数组声明为类变量并执行
编辑:
你分配它了吗?这是一个简单的方法,您可能需要修改它以满足您的需求 -
头文件:
源文件
If I follow you correctly, you want to have one array in which you want to store
restaurant_name_save
andamount_name_save
each time a button on your screen is pressed. (I'm assuming this button calls thesaveData
method?In that case, reallocating your array will clear any objects in it first and then adds the two strings to it. You should declare your array as a class variable and just do
EDIT:
Are you allocating it though? Here's a simple way of doing it, you might need to modify it to suit your needs -
Header file:
Source file