I have a program that I am making, which has two arrays, one for usernames and one for passwords. I am trying to add a string that a user inputs into a textbox to an array. Instead, the program is only adding the string once. How do I fix this?
I am using an NSMutableArray. My code is:
NSString *usrname = [NSString stringWithFormat:@"%d", usrString];
[usrs addObject:usrname];
This code adds the string once, but when I change the string and press the button again, it d开发者_运维技巧oesn't add the second string.
Check in your code that by mistake you did not set array with fixed capacity by ([[NSMutableArray alloc] initWithCapacity: arrayCapacity]
);
You just need to init without capacity ( NSMutableArray *array = [[NSMutableArray alloc] init];
) so that it will take it upto available memory.
Hi Based on the info which you provided you have to write the below code in .h and .m files In .h file
IBOutlet UITextFiled *userNameTextField,*PasswordTextField;
IBOutlet UIButton *loginButton;
NSMutableArray *usrs,*passwords;
-(IBAction)loginButtonClick;
In .m file
-(void)viewDidLoad
{
usrs=[[NSMutableArray alloc]init];
passwords=[[NSmutableArray alloc]init];
}
-(IBAction)loginButtonClick
{
NSString *userstr=[[NSString alloc]initWithFormat:@"%@",userNameTextField.text];
NSString *passwordstr=[[NSString alloc]initWithFormat:@"%@",PasswordTextField.text];
[usrs addObject:userstr];
[passwords addobject:passwordstr];
}
I hope this will helpful to you. Now the data will add into the array but if you close your app or if you move from login page to another page the array will get clear.In order to store the data in permanent manner you have to use plist or sqlite.
精彩评论