Abhimanyu
Abhimanyu

Reputation: 61

Issue in passing data back to viewcontroller

a small issue when trying to pass data back from tableviewcontroller to viewcontroller. i have 3 buttons and 3 textfields in viewcontroller when clicked on each button will navigate to same tableview with static data like (Headache, Fever, Cough, Cold). the issue comes here when i select a row in the tableview the data is assigned to all the textfileds in viewcontroller.But i want when button1 is clicked when i select Fever it should assign to only textfiled1 and button2 is clicked when i select Cough it should assign to only 2st textfiled. Below is my code.

In **AppDelegate.h**

@property (strong, nonatomic) NSString *selectedText;


In **ViewController.m**

-(void)viewWillAppear:(BOOL)animated
{
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    txtField1.text = appDelegate.selectedText;
    txtField2.text = appDelegate.selectedText;
    txtField3.text = appDelegate.selectedText;

}


**FamilyDetailsViewController.m**

pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

        NSString *selected= [arr objectAtIndex:indexPath.row];

        AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

        appDelegate.selectedText = selected;

       [self.navigationController popViewControllerAnimated:YES];
}

Upvotes: 0

Views: 48

Answers (1)

Davinder Singh
Davinder Singh

Reputation: 645

Have a int variable in first view controller which should be accessible in whole view controller. once when you press button1 then set the value of int variable as 1. change it to 2 & 3 for other buttons. now, on the time of assigning make condition as per that int variable.

if(variable == 1){
txtField1.text = appDelegate.selectedText;
}
else if(variable == 2){
txtField2.text = appDelegate.selectedText;
}
else if(variable == 3){
txtField3.text = appDelegate.selectedText;
}

Upvotes: 2

Related Questions