Question

Prior to storyboards, and working with .xib files, I used this piece of code to do screen adjustments during init.

- (id)initForNewItem:(BOOL)isNew {
    self = [super initWithNibName:@"NAME" bundle:nil];
    if (self) {
        if (isNew) {
            // Place some buttons only when isNew is true
        }
    }

    return self;
}

Then I also implemented this to generate an exception when initWithNibName is directly called because I wanted to avoid that:

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    @throw [NSException exceptionWithName:@"Wrong initializer" reason:@"Use initForNewItem:" userInfo:nil];
    return nil;
}

Then another viewcontroller could call the custom init and the screen would be set:

MyViewController *myViewController = [[MyViewController alloc] initForNewItem:YES]; // Or NO ofcourse depending on the situation.

Now I'm using storyboard and initWithNibName is never called. Instead only initWithCoder is called but this method can only be called by the storyboard right? So how would I do something similar while using storyboard?

Was it helpful?

Solution

You wouldn't do this with storyboards.

The way to do it is to use properties to give the correct values once the view controller has been initialised.

This is done in - (void)prepareForSegue:(UIStoryBoardSegue *)segue sender:(id)sender.

You can access segue.destinationViewController and then use this to put values in to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top