1

I have designed a screen in a storyboard that is subclassed by several controllers. The design is the same for all of them.

Now, I need to load the screen. According to How can I load storyboard programmatically from class?, I do:

UIStoryboard *b = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
SearchViewController *c = [b instantiateViewControllerWithIdentifier:@"SearchViewController"];

return c;

However, this load the base class, not the subclass. (Baseclass is SearchViewController, subclass is SearchCustomerViewController). What I can do?

P.D: This is how is the code of the base class and subclass:

@interface SearchViewController : UIViewController
    <SqliteDataSource>

- (IBAction)showMenu:(id)sender;
- (IBAction)addNew:(id)sender;

- (id)initWithRouterParams:(NSDictionary *)params;

@property (strong, nonatomic) NSDictionary *params;

@property (weak, nonatomic) IBOutlet SearchBar *editSearch;
@property (weak, nonatomic) IBOutlet UITableView *searchResults;
@property (weak, nonatomic) IBOutlet UIToolbar *barActions;

+ (id)load:(Class)name;

@end

@implementation SearchViewController

+ (id)allocWithRouterParams:(NSDictionary *)params {
    UIStoryboard *b = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    SearchViewController *c = [b instantiateViewControllerWithIdentifier:@"SearchViewController"];

    return c;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.editSearch.hideTable = NO;
    [self.editSearch becomeFirstResponder];
}

+ (id)load:(Class)name {
    return [name allocWithRouterParams:nil];
}


@interface CustomerListViewController : SearchViewController

@property (weak, nonatomic) CustomerModel *customerData;

@end
-(void) viewDidLoad {
    [super viewDidLoad];

    self.customerData = [CustomerModel setupController:self searchResult:self.searchResults searchCustomer:self.editSearch hideTable:NO onSelectModel:^(NSMutableDictionary *data) {
        NSInteger theId = [data[@"id"] integerValue];

        [self setCustomer:[[Db currentDb] loadById:[Customer class] theId:theId]];
    }];

    self.customerData.filter = CUSTOMER_FILTER_NONE;
}
Community
  • 1
  • 1
mamcx
  • 15,916
  • 26
  • 101
  • 189
  • Give your subclass an identifier and then try to instantiate the view controller the same way you did before. Also did you try the code you gave above? It might do what you want it to do. – Andrew Jul 09 '13 at 17:45
  • To have a different identifier is necessary to duplicate the screen, something I wish to avoid. – mamcx Jul 09 '13 at 18:50
  • so you don't want a different identifier or don't want to duplicate the screen...I can't tell – Andrew Jul 09 '13 at 18:53
  • I don't want to duplicate the screen. Same screen for several inherited uiviewcontrollers – mamcx Jul 09 '13 at 18:57
  • It won't duplicate the screen. Just instead of `[b instantiateViewControllerWithIdentifier:@"SearchViewController"];` do `[b instantiateViewControllerWithIdentifier:@"SearchCustomerViewController"];` after you give that subclass in the storyboard an identifier. – Andrew Jul 09 '13 at 19:07
  • Although that may not work...I can't see what your storyboard looks like so its a little hard to help – Andrew Jul 09 '13 at 19:08
  • Think is with a single screen in the storyboard, that represent the search UI. 5 subclases use the same UI, but different code. – mamcx Jul 09 '13 at 21:08

1 Answers1

1

Storyboard view controllers will always instantiate with class you choose in the storyboard. You can't just import the "view" and have a custom controller.

You either need to duplicate your design or refactor how you are building your view controllers. One option would be embedded view controllers.

Patrick Tescher
  • 3,387
  • 1
  • 18
  • 31