how do i swap the view as the iphone rotates

Discussion in 'iOS Programming' started by slking, Aug 15, 2008.

  1. slking macrumors newbie

    Joined:
    Aug 15, 2008
    #1
    I am very-much new to the iphone programming. I want to swap the view of a UIViewController as the iphone rotates. I have these methods implementd in my view controller. i get syntax error in the line "self.view = landscapeView".

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;// for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }

    -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
    self.view = landscapeView;
    }

    if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
    {
    self.view = portraitView;
    }
    }
     
  2. mccannmarc macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #2
    try:-

    Code:
    self.view = [landscapeView self];
    
    and

    Code:
    self.view = [portaitView self];
    
    repectively

    OR it that fails try:-

    Code:
    [self setView:[landscapeView self]];
    
    and

    Code:
    [self setView:[portraitView self]];
    
    respectively
     
  3. slking thread starter macrumors newbie

    Joined:
    Aug 15, 2008
    #3
    Thanks for the reply.
    I tried both methods mentioned above. But
    Code:
    [self setView:[landscapeView self]];
    
    and
    Code:
    self.view = [landscapeView self];
    
    Gave
    "Error:landscapeView undeclared (first use in this function)"

    I have two UIView objects declared in the
    Code:
    @interface MyCalViewController : UIViewController {
    	UIView	*p;
    	UIView	*l;
    }
    
    @end
    
    and in the MYCalViewController.m
    Code:
    - (void)loadView {
    	self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    	p = [self portraitView];
    	l = [self landscapeView];
    	
    	UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
    	if (orientation == UIInterfaceOrientationPortrait) {
    		[self.view addSubview:p];
    	}
    	else {
    		[self.view addSubview:l];
    	}}
    
    So is there anything that i am doing wrongly or should i do something else to get this done?
     
  4. godrifle macrumors regular

    godrifle

    Joined:
    Oct 20, 2003
    Location:
    Fort Thomas, KY
    #4
    According to Master Nilson, the correct approach is to replace this line:

    Code:
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    with this line:

    Code:
    return YES;
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    ...assuming you want to support all four orientations. Otherwise, only return YES for orientations you want to support.
     

Share This Page