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

    Joined:
    Feb 22, 2011
    #1
    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:

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

    but follow:

    in .h file

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

    Code:
    @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

    jnoxx

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

    seepel

    Joined:
    Dec 22, 2009
    #3
    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

    Code:
    v.view
    
    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.

    Code:
    [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.

    Code:
    [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...

    Code:
    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.

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

Share This Page