iphone how to one view point other view

Discussion in 'iOS Programming' started by kimti, Feb 25, 2011.

  1. kimti, Feb 25, 2011
    Last edited by a moderator: Feb 25, 2011

    kimti macrumors newbie

    Feb 22, 2011
    i create two nib files, and when i show the first nib, it show me the first view,and i click a button, i want to the first view change to the second view, but the nib no need change:

    UIViewController *c = [[UIViewcontroller alloc] init];
    [c.view setFrame:f];
    [self.view addSubview:c.view]
    this is ok.

    but follow:

    in .h file

    UIView *v
    @property(nonatomic,retail) UIView *v;
    in .m file

    @synthesize v;
    UIViewController *c = [[UIViewcontroller alloc] init];
    [c.view setFrame:f];
    [self setV:v.view];
    [self.view addSubview:v];
    this is no show the view;
    what happen?
    thank you.
  2. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    you are confusing yourself with ur code.
    Should be more like this
    DetailViewController *a = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    [self.view addSubview:a];
  3. seepel macrumors 6502


    Dec 22, 2009
    First, the more descriptive your variables the better off you'll be. using just c and v is just asking to confuse yourself. Here c is an instance of UIViewController and v is an instance of UIView controller. When you call

    You are trying to access the view method of a UIView which doesn't exist and I would expect your program to crash with an exception about UIView not responding to message view.

    So lets say you changed your setV to this to get rid of the error.

    [self setV:v];
    Now you are setting your member variable "v" to itself, so you don't get anything new. Probably what you want to do, is set your variable v to the view of your viewcontroller c.

    [self setV:c.view];
    I'm not so sure this is even what you want. Using a generic UIViewController doesn't seem to get you anything vs simply making a new UIView. I would do this instead...

    UIView *newView = [[UIView alloc initWithFrame:f];
    [self setV:newView];
    [self.view addSubview:self.v];
    [newView release];
    Unless you really need the view controller in which case it would make more sense to hang onto a pointer to it, instead of it's view.

    UIViewController *controller;
    @property (nonatomic, retain) UIViewController *controller;
    UIViewController *newController = [[UIViewController alloc] init];
    [self setController:newController];
    [newController release];
    self.controller.view.frame = f;
    [self.view addSubview:self.controller.view];

Share This Page