Pass data to parentViewController [iOS]

I have this storyboard: enter image description here

When I click the "Insegnante" button in the first view controller (called newCourseViewController ), it shows me a table view with the teacher list. When I click on the teacher (and the tableView:canEditRowAtIndexPath: method is tableView:canEditRowAtIndexPath: ), I want the UITableViewController โ€œpassโ€ the object clicked on the first view controller.

This is my code for the first view controller newCourseViewController.h

 #import <UIKit/UIKit.h> #import "Teacher.h" @interface newCourseViewController : UIViewController @property (nonatomic , strong) Teacher *teacher; @end 

And this is my code for the first view controller newCourseViewController.m (only important code)

 #import "newCourseViewController.h" #import "Courses.h" #import "Teacher.h" #import "addTeacherToCourseViewController.h" @interface newCourseViewController () @property (weak, nonatomic) IBOutlet UITextField *textField; @end @implementation newCourseViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)setTeacher:(Teacher *)teacher { self.teacher = teacher; NSLog(@"Maestro settato!"); } -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"addTeacherToCourse"]) { [segue.destinationViewController setPreviousViewController:self]; } } 

Now the code for the second view controller addTeacherToCourseViewController-h

 @interface addTeacherToCourseViewController : UITableViewController @property (nonatomic , weak) id previousViewController; @end 

and addTeacherToCourseViewController.m (important method only)

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { Teacher *teacher = [self.teachers getTeacherInPosition:indexPath.row]; [self.previousViewController setTeacher:teacher]; [self.navigationController popViewControllerAnimated:YES]; } 

In the first view of the controller in the prepareForSegue method prepareForSegue I set myself to the previousViewController in the second view. Then I โ€œskipโ€ the selected teacher and reject the second view controller. When the application crashes [self.navigationController popViewControllerAnimated:YES]; Xcode and simulator crash. I canโ€™t understand what the problem is. Can you help me?

+4
source share
1 answer

To send values โ€‹โ€‹to the parent controller, you must use protocols. I will provide the appropriate steps you must take to work with your desired features.

1. Create a protocol for your AddTeacherToCourseController. In AddTeacherToCourseController.h, add the following imports below:

 @protocol AddTeacherToCourseControllerProtocol <NSObject> - (void)yourDelegateMethod:(Teacher *)insegnante; @end 

And below add the interface tag:

 @property (strong, nonatomic) id <AddTeacherToCourseControllerProtocol> delegate; 

2. In AddTeacherToCourseController.m:

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // I would use the existing array you are using to display the teachers in order to select the correct one you want to send back like this: // Teacher *teacher = [self.teachers getTeacherInPosition:indexPath.row]; [self.delegate yourDelegateMethod:[yourTeacherArray objectAtIndex:indexPath.row]]; } 

[this method will call your delegation method through the protocol and pass your selected professor to the parent controller]

3. In your parent controller, your newCourseViewController.h immediately after the interface line adds:

 <AddTeacherToCourseControllerProtocol> 

4. If you do not have an Insegnante button action, create it in the interface builder [drag and naming]. Then add the following to this action:

 // assuming your storyboard is named MainStoryboard. here you create your segue programmatically: UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; addTeacherToCourseViewController *addTeacherController = (addTeacherToCourseViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"addTeacherToCourseViewController"]; addTeacherController.delegate = self; [self.navigationController pushViewController:addTeacherController animated:YES]; 

5. In Interface Builder:

  • Remove your segment from the Insegnante button.
  • Edit the storyboard identifier 'addTeacherToCourseViewController' in 'addTeacherToCourseViewController'

6. In newCourseViewController.h write your delegation method:

 - (void)yourDelegateMethod:(Teacher *)insegnante{ // Do whatever you want with your Insegnante // and be sure to pop the second controller from the view stack: [self.navigationController popViewControllerAnimated:YES]; } 

Let me know if you have questions, and if my answer helped someone.

+5
source

All Articles