how can I save data of UITextField into 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_na开发者_如何转开发me_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 ....
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
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];
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.
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.
}
精彩评论