4

i am creating custom cell.

In that i added 3 textfields. so i want to store that textfield values into nsmutablearray.

when i am trying this code

UITextField *valueField = [[UITextField alloc] initWithFrame:CGRectMake(165,6, 135, 30)];
valueField.borderStyle =  UITextBorderStyleRoundedRect;
[cell addSubview:valueField];
[array1 addObject:valueField.text];
[valueField release];

i am getting error like this

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0'

so please tell me the reason thanks in advance.

3
  • What is array1 and how is it declared? Commented Aug 10, 2011 at 6:15
  • array1 is object of nsmutablearray. and declared as like NSMutableArray *array1; Commented Aug 10, 2011 at 6:18
  • 1
    Why do you want to add the text of a newly created textfield when you know that there is nothing inside it... Commented Aug 10, 2011 at 6:37

5 Answers 5

2
UITextField *valueField = [[UITextField alloc] initWithFrame:CGRectMake(165,6, 135, 30)];
valueField.borderStyle =  UITextBorderStyleRoundedRect;
valueField.text = @"";
[cell addSubview:valueField];
[array1 addObject:valueField.text];
[valueField release];

the above code will work fine for you.

Sign up to request clarification or add additional context in comments.

5 Comments

hi rajesh this code is working fine. can please tell me how i can chat with you
i could not understand what r saying
can u please tell me. textfield values cannot store in array what is the problem
@Ram before you store values to array , you need to set the textField text ,eg: valueField.text = @"hai"; then try add the value to array, you can do it.
@Ram you can accept this answer helpful by clicking the tick button to green
2

The text attribute of UITextField is nil by default. Set it to an empty string before adding it to your array, though I think this is not what you are trying to achieve.

Comments

2

Just add this condition to check if the textfield is empty (i.e. textfield.text = nil):

if (valueField.text != nil) {
     [array1 addObject:valueField.text];
}
else {
     [array1 addObject:@""];
}

This will check if textfield is empty it will add an empty string. If you dont want that just skip the else part.

5 Comments

can u please tell me. textfield values cannot store in array what is the problem
you should store these values on this delegate callback textFieldDidEndEditing:
when i am insert your code into delegate method it is showing only last field value
Please study about the delegate callbacks in the documentation and post a new question explaining what you want
1

I noticed you said you declared your array like so:

" declared as like NSMutableArray *array1;"

Have you alloc your array before adding objects to it?

In other words have you done this ?

NSMutableArray *array1 = [[NSMutableArray alloc] init];

If you only did

NSMutableArray *array1;

That will only declare a pointer to a NSMutableArray object. It doesn't point to an instance of a NSMutableArray object.

Comments

0

When you put [array1 addObject:valueField.text];, you are adding a nil object to your array, just as your error says. How can valueField.text be equal to something when you just initialized and allocated valueField?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.