xcode init controller with data

2019-09-15 23:54发布

问题:

I would like to pass two strings from current view to userViewController when this one is initialized. Will be possible to init userViewController with, in my case, two strings? Sorry if is a basic objective-c issue, I never did it, always use alternative methods as singleton or setter. Thanks for help.

if (self.controladorUser == nil)
    {
        userViewController *aController = [[userViewController alloc] initWithNibName:@"userViewController" bundle:];
        self.controladorUser = aController;
        [aController release];
    }

    [UIView setAnimationTransition: UIViewAnimationOptionCurveEaseIn forView:self.view cache:YES];   
    [self.controladorPass viewWillDisappear:YES];
    [self.controladorUser viewWillAppear:YES];

    [self.controladorPass.view removeFromSuperview];
    [self.view insertSubview:controladorUser.view   atIndex:0];

    [self.controladorPass viewDidDisappear:YES];
    [self.controladorUser viewDidAppear:YES];

    [UIView commitAnimations];

in userViewController something like: (id)initWithData:

回答1:

It would be a good idea to create your own init method for the view controller you want to create them with. For example:

-(id)initWithNibName:(NSString *)nibName bundle(NSBundle *)nibBundle stringOne:(NSString *)aStringOne stringTwo(NSString*)aStringTwo
{
    self = [super initWithNameName:nibName bundle:nibBundle];
    if (self)
    {
        [self setStringOne:aStringOne];
        [self setStringTwo:aStringTwo];
    }
    return self;
}

Hope this helps :)



回答2:

Or make the two strings properties (nonatomic, copy) and set them immediately after you do the alloc/init.



回答3:

Just make a custom initializer with your 2 strings.