Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly accessing a segue's destination view controller to assign protocol delegates

I'm encountering some problems in integrating segue and protocols while implementing a selection list.

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 to this selection list view by performing a segue from another table view:

@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 navigate to the selection list using:

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

the alert is displayed. If I instead use:

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

no alert is displayed, because, I think, I don't pass to the destination selection list the selectController. Any ideas to solve this issue please?

like image 237
yassassin Avatar asked Dec 16 '11 11:12

yassassin


People also ask

Which of the following methods allows you to pass data from source view controller to the destination view controller?

The prepareForSegue:sender: method of the source view controller lets you pass data from the source view controller to the destination view controller.

How do I segue to another view controller?

Well, in addition to that, Ctrl+dragging also helps set up segues. So, Ctrl+Drag from the “Go to Other View Controller” button, to somewhere in the second View Controller. It can be anywhere in the main box of the second view controller. When you release, it will show you a box like the one below.

How do I connect to segue on iOS?

The segue needs to connect from the view controller itself so nothing else triggers it. To create a segue from the controller Control-drag from the View Controller icon to the Exit icon. Give this new segue the identifier unwind to reference it from the code. Time to resume the task of saving a new player.


1 Answers

When using Segue to pass data to the 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 the Apple Docs

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

like image 162
agilityvision Avatar answered Nov 24 '22 04:11

agilityvision