0

I have four textfields like textfield1, textfield2, textfield3, textfield4 and one button. When I click on the button I am adding the textfield text to an NSMutableArray and populating in a tableview.

My code is:

- (void)buttonClick
{
    NSMutableArray  *array =[[NSMutableArray alloc]init];
    [array addObject: textfield1.text];
    [array addObject: textfield2.text];
    [array addObject: textfield3.text];
    [array addObject: textfield4.text];
 }

Populating in table view using delegate methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text =[array objectAtIndex:indexPath.row];
    return cell;
}

The above code is working fine. But when I click on button without entering any data in the textfields, the app is crashing because the array is empty. How to solve this problem?

5 Answers 5

3

You can't add nil to an array.

[array addObject:textfield1.text ?: @""];
[array addObject:textfield2.text ?: @""];
[array addObject:textfield3.text ?: @""];
[array addObject:textfield4.text ?: @""];

This makes sure there are four items in the array (possibly empty strings).

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

Comments

1

Why crashes?

You can't add nil in array. It should have something inside of it.

-(void)buttonClick
{
    NSMutableArray  *array =[[NSMutableArray alloc]init];

    if ([textfield1.text length]>0) {
        [array addObject: textfield1.text];

    }
    if ([textfield2.text length]>0) {
        [array addObject: textfield2.text];

    }
    if ([textfield3.text length]>0) {
        [array addObject: textfield3.text];
    }



}

Comments

0

Try this

-(void)buttonClick
{
    NSMutableArray  *array =[[NSMutableArray alloc]init];
     if(textfield1.text.length > 0)
        [array addObject: textfield1.text];

     if(textfield2.text.length > 0)
        [array addObject: textfield2.text];

     if(textfield3.text.length > 0)
        [array addObject: textfield3.text];

    if(textfield4.text.length > 0)
        [array addObject: textfield4.text];

 }

Populating in table view using delegate methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text =[array objectAtIndex:indexPath.row];
    return cell;
}

if you have multiple text fields then: //give tag value of each textfield from 100 to 150 or any any tag (i am considering 50 text fields)

for (int i=100; i<=150; i++) {
// self.view or use ur view on which u r addding textfield
    id txtF = [self.view viewWithTag:i]; 
    if([txtF isKindOfClass:[UITextField class]]) {
        UITextField *txtField = (UITextField*)txtF;
        if(txtField.text.length > 0) {
            //Add ur object
        }
    }
}

3 Comments

Thank you for your quick response. I have one dought suppose if i have more textfield suppose 20 textfield. I have to check 20 conditions like if (textfiled.text.length>0). Is there any alternative solution for this. Please tell me
k I will implement and let you know
also make sure that there is no other subview on view whose tag value is lying in between the range of ur textfields tag. its better to take range of tags for textfields more tha 1000 or 3000 etc
0

you can check the textfields before putting an array wheather it has text or not if yes then put in the array only.

Comments

0

This line in the buttonClick method defined a local array variable:

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

It should be

array =[[NSMutableArray alloc]init];

Which is your instance variable array.

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.