问题
Currently I have a custom view table cell and a text field just above it. I want to get the text from the UItextfield
and put that into an NSMutableArray
.
Pseudocode:
String text = _textfield.text;
[array addObject:text];
NSLog{array}
In my header file I have created the textfield and the array.
I currently receive the error : 'CustomTableView:[340:11303] array: (null)'
when I NSLog.
I am not to sure why the text from the textfield is not getting added to the array. If any one is able to help it will be greatly appreciated.
Note - My textfield is above the custom cell not in it. I have even tried just adding a string to the array directly and logging it and I get the same error. So I would assume that this is something to do with the array.
回答1:
did you initialize your Array.take a MutableArray and initialize it.
NSMutableArray *array=[NSMutableArray alloc]init];
回答2:
You mentioned that you have declared the textfield and the array in your header file...
Have you initialised the variable array
?
e.g.
array = [NSMutableArray new];
回答3:
It looks like you are not actually creating the array. In Objective C, you do not create things in header file, you declare them. The implementation files(.m files) do all the work.
Try this:
NSString *text = _textfield.text;
array = @[text]
NSLog( @"%@", array );
回答4:
This is how you should print your array,
NSLog(@"%@", array);
回答5:
It looks as if your a newbie to ios.Go through the objective-c and Read the apple documentation carefully.
NSString * text = self.textfield.text;
NSMutableArray *array = [NSMutableArray alloc] init];
[array addObject:text];
NSLog(@"%@",array);
回答6:
For me this is what worked...
I have taken one textfield inside tableviewcell. I am creating textfields based on dynamic data. My requirement is , I need to get textfields text which are created dynamically.
For getting text in another method
NSIndexPath *indexPath = [tableViewObj indexPathForCell:customCell];
if (indexPath.row==0)
{
[arrayPhoneNumbers addObject:customCell.textFieldObj.text];
NSLog(@"array is :%@",arrayPhoneNumbers);
}
else if(indexPath.row==1)
{
[arrayPhoneNumbers addObject:customCell.textFieldObj.text];
NSLog(@"array is :%@",arrayPhoneNumbers);
}
else if(indexPath.row==2)
{
[arrayPhoneNumbers addObject:customCell.textFieldObj.text];
NSLog(@"array is :%@",arrayPhoneNumbers);
}
Like this I have added textfield text to array. Let me know if you have any doubts.
来源:https://stackoverflow.com/questions/14958613/unable-to-add-text-from-uitextfield-to-an-array