0

I have a SubSelectVC that handles sub-selection choice that is presented modally from a SearchVC. The SubSelectVC has a -(void)didSelectRowAtIndexPath that performs these options, roughly:

if ([[[UIDevice currentDevice] systemVersion] intValue] < 5) {
    ((SearchVC *)self.parentViewController.filters.filterValue = @"Some value";
}
else {
    ((SearchVC *)self.presentingViewController.filters.filterValue = @"Some value";
}

This seems like it screams of bad design but, I mean, the option to do it this way is there and it's so easy! What's wrong with this, and how would I make it right? (Should I use delegation?)

tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126

1 Answers1

2

Yes, I think a better encapsulated version of this would be to define a delegate protocol in the header file for SubSelectVC, and a delegate property on SubSelectVC.

That way your view controller is reusable for any task that requires modal selection from a list.

EDIT: added example header:

SubSelectVC.h:

@protocol SubSelectVCDelegate

- (void)itemSelected:(NSString *)itemName;

@end

@interface SubSelectVC : UIViewController

@property (assign) id <SubSelectVCDelegate> delegate;

// etc...

@end
joerick
  • 16,078
  • 4
  • 53
  • 57
  • No, the example is fine, but I could use a little more explanation on why the other way is not preferred, as in some situation where it would break or require modification to work – tacos_tacos_tacos Jan 13 '12 at 20:21
  • The principle here is that you design a class to perform a very well-defined task, then you can write the code, knowing exactly what it should do, and what its inputs will be. It's far easier to write bug-free code that works first time if you think in this way. It also makes it clear when it's possible to reuse the class. If you require modifications, you are free to subclass the SubSelectVC to do something more specific. – joerick Jan 13 '12 at 20:42