How i pass my view to nib at run time(Dynamically) in viewController class?

Posted by Rajendra Bhole on Stack Overflow See other posts from Stack Overflow or by Rajendra Bhole
Published on 2010-06-09T11:06:06Z Indexed on 2010/06/09 13:32 UTC
Read the original article Hit count: 134

Filed under:

Hi, I want to create viewController class programmatically in which the view of nib file of viewController is not fixed. I have a UntitledViewController class that contains 4 UIButtons:

-(IBAction)buttonPressed:(id)sender{
    if((UIButton *)sender == button1){
        tagNumber = button1.tag;
        NewViewController *newVC = [[NewViewController alloc] init];
        //newVC.tagN
        [self.navigationController pushViewController:newVC animated:YES];
        NSLog(@"Button Tag Number:- %i", tagNumber);

    }

    if((UIButton *) sender == button2){
        tagNumber = button2.tag;
        NewViewController *newVC = [[NewViewController alloc] init];
        [self.navigationController pushViewController:newVC animated:YES];


    }

    if((UIButton *) sender == button3){
        tagNumber = button3.tag;
        NewViewController *newVC = [[NewViewController alloc] init];
        [self.navigationController pushViewController:newVC animated:YES];

    }

    if((UIButton *) sender == button4){
        int x = tagNumber;
        tagNumber = button4.tag;
        NewViewController *newVC = [[NewViewController alloc] init];
        newVC.untitledVC = self;
        [self.navigationController pushViewController:newVC animated:YES];
    }
}

When I will click any one of the UIButtons, I want to create a new NewViewController class with button tag property but without fixed nib file. In NewViewController's loadView event I want to fix the view of the nib of NewViewController class using UIButton's tag property (In NewViewController.xib I'm creating 4 UIViews named view1, view2, view3 and view4).

-(void)loadView{
    [super loadView];
    CGRect frame = CGRectMake(0, 0, 320, 480);
    UIView *newView = [[UIView alloc] initWithFrame:frame];
    newView.tag = untitledVC.tagNumber;
    self.view1 = newView;
}

Now I want that when I click button1 the NewViewController will be generated with view1 and when I click button2 the NewViewController will be generated with view2 and so on with different implementation.

Thanks.

© Stack Overflow or respective owner

Related posts about iphone