Proper access to segue session view controller to assign protocol delegates

I am facing some problems in integrating segue and protocols when implementing a picklist.

In my selection list .h I have:

#import <UIKit/UIKit.h> @protocol SelectionListViewControllerDelegate <NSObject> @required - (void)rowChosen:(NSInteger)row; @end @interface SelectColor : UITableViewController <NSFetchedResultsControllerDelegate> -(IBAction)saveSelectedColor; @property (nonatomic, strong) id <SelectionListViewControllerDelegate> delegate; @end 

In my selection list .m I have:

 @implementation SelectColori @synthesize delegate; //this method is called from a button on ui -(IBAction)saveSelectedColore { [self.delegate rowChosen:[lastIndexPath row]]; [self.navigationController popViewControllerAnimated:YES]; } 

I would like to access this select list list by running segue from another kind of table:

 @implementation TableList ... - (void)selectNewColor { SelectColor *selectController = [[SelectColor alloc] init]; selectController.delegate = (id)self; [self.navigationController pushViewController:selectController animated:YES]; //execute segue programmatically //[self performSegueWithIdentifier: @"SelectColorSegue" sender: self]; } - (void)rowChosen:(NSInteger)row { UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error Title" message:@"Error Text" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [errorAlert show]; } 

If I go to the selection list using:

[self.navigationController pushViewController: selectController animated: YES];

a warning appears. If I use instead:

[self performSegueWithIdentifier: @ "SelectColorSegue" sender: self];

warning is not displayed because, I think, I am not going to selectController destination select list. Any ideas to solve this problem?

+7
source share
1 answer

When using Segue to transfer data to destinationViewController you need to use the method

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"SelectColorSegue"]) { SelectColor *vc = segue.destinationViewController; vc.delegate = self; } } 

from Apple Docs

By default, the implementation of this method does nothing. Subclasses can override it and use it to pass any relevant data to the view that will be displayed. The segue object contains pointers to viewing controllers among other information.

+14
source

All Articles