如何将 UITextField 的数据保存到 NSMutableArray 中?

发布于 2024-10-16 05:43:30 字数 736 浏览 3 评论 0原文

我正在编写一个简单的程序,有两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千纸鹤带着心事 2024-10-23 05:43:30

我可能在这里遗漏了一些东西但是
它说[order count]

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"warning" 
message:[NSString stringWithFormat:@"%d",[order count]] 

delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];

[order count]的输出将为2,因为数组中有两个条目。

我假设看到您编写的数组的内容
stringWithFormat:@"%@", 顺序

I might be missing something here but
it says [order count]:

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"warning" 
message:[NSString stringWithFormat:@"%d",[order count]] 

delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];

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

眉目亦如画i 2024-10-23 05:43:30

您可能有一个错字。尝试使用 -initWithObjects: 代替:

order = [[NSMutableArray alloc] initWithObjects: restaurant_name_save, amount_name_save, nil];

在重复运行时,还要确保释放成员变量order并将其设置为nil< /code>,在执行另一个 +alloc-initWithObjects: 调用之前:

if (order) {
    [order release], order = nil;
}
order = [[NSMutableArray alloc] initWithObjects: restaurant_name_save, amount_name_save, nil];
...

更好的是,不要重复使用 +alloc-initWithObjects:在此方法中,但在此方法之外(可能在较大对象的init方法中)创建一个容量为2的NSMutableArray

self.order = [[[NSMutableArray alloc] initWithCapacity:2] autorelease];

在处理该方法的方法中按钮操作,将项目设置在各自的索引处:

[order replaceObjectAtIndex:0 withObject:restaurant_name_textfield.text];
[order replaceObjectAtIndex:1 withObject:amount_textfield.text];

You might have a typo. Try using -initWithObjects: instead:

order = [[NSMutableArray alloc] initWithObjects: restaurant_name_save, amount_name_save, nil];

On repeat runs, also make sure you release the member variable order and set it to nil, before doing another +alloc and -initWithObjects: call:

if (order) {
    [order release], order = nil;
}
order = [[NSMutableArray alloc] initWithObjects: restaurant_name_save, amount_name_save, nil];
...

Better yet, don't repeatedly use +alloc and -initWithObjects: in this method, but outside this method (perhaps in the larger object's init method) create an NSMutableArray of capacity 2:

self.order = [[[NSMutableArray alloc] initWithCapacity:2] autorelease];

In the method that handles the button action, set the items at their respective indices:

[order replaceObjectAtIndex:0 withObject:restaurant_name_textfield.text];
[order replaceObjectAtIndex:1 withObject:amount_textfield.text];
盗琴音 2024-10-23 05:43:30

使用 initWithObjects 代替 initWithObject

并始终在数组中设置两个对象,并且您正在打印数组的计数,这就是为什么它总是显示 2。

您编写的所有其他代码都是正确的。

use initWithObjects insted of initWithObject

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.

计㈡愣 2024-10-23 05:43:30

如果我没听错的话,您希望有一个数组,用于在每次按下屏幕上的按钮时存储 restaurant_name_saveamount_name_save 。 (我假设此按钮调用 saveData 方法?

在这种情况下,重新分配数组将首先清除其中的所有对象,然后将两个字符串添加到其中。您应该将数组声明为类变量并执行

[order addObject:string1];
[order addObject:string2];

编辑:

你分配它了吗?这是一个简单的方法,您可能需要修改它以满足您的需求 -

头文件:

@interface WelcomeScreen : UIViewController {
    NSMutableArray *array;
}

@property (nonatomic, retain) NSMutableArray *array;
-(IBAction) saveData:(id) sender;

源文件

@synthesize array;

-(void) viewDidLoad {
    array = [[NSMutableArray alloc] init];
}

// Make the UIButton's tap option point to this -
-(IBAction) saveData:(id) sender
{
    [array addObject:string1];
    [array addObject:string2];

    // alert.
}

If I follow you correctly, you want to have one array in which you want to store restaurant_name_save and amount_name_save each time a button on your screen is pressed. (I'm assuming this button calls the saveData 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

[order addObject:string1];
[order addObject:string2];

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:

@interface WelcomeScreen : UIViewController {
    NSMutableArray *array;
}

@property (nonatomic, retain) NSMutableArray *array;
-(IBAction) saveData:(id) sender;

Source file

@synthesize array;

-(void) viewDidLoad {
    array = [[NSMutableArray alloc] init];
}

// Make the UIButton's tap option point to this -
-(IBAction) saveData:(id) sender
{
    [array addObject:string1];
    [array addObject:string2];

    // alert.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文