2011-01-21 137 views
2

我正試圖在XCode中開發一個應用程序,該應用程序將在切換到新視圖時旋轉。iOS應用程序在切換旋轉視圖時崩潰

這裏是view1controller.h代碼:

#import UIKit/UIKit.h 

@interface TestViewController : UIViewController {} 

IBOutlet UIView *portrait; 
IBOutlet UIView *landscape; 

@property(nonatomic,retain) UIView *portrait; 
@property(nonatomic,retain) UIView *landscape; 

@end 

這裏是view1controller.m代碼:

#import "view1controller.h" 

@implementation TestViewController 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight))) 
    { 
     self.view = landscape; 
    } else if (((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))) { 
     self.view = portrait; 
    } 

    return YES; 
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (void)dealloc { 
    [super dealloc]; 
} 

@synthesize portrait,landscape; 

@end 

反正這樣的應用程序打開,但它崩潰時開放。

回答

1

試着這麼做:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft) 
    { 
     self.view = landscapeleft; 
    } 

    if (interfaceOrientation == UIInterfaceOrientationPortrait) 
    { 
     self.view = portrait; 
    } 

    if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     self.view = portraitupsidedown; 
    } 

    if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    { 
     self.view = landscapeleft; 
    } 


    return YES; 
} 
0

我會做到以下幾點:

if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight))) 
{ 
    [portrait removeFromSuperview]; 
    [self.view addSubview:landscape]; 
} else if (((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))) { 
    [landscape removeFromSuperview]; 
    [self.view addSubview:portrait]; 
} 

希望這有助於!

+0

沒有必要調用removeFromSuperview。 – WrightsCS 2011-01-21 23:46:19

1

您的標題中的括號錯位。

@interface TestViewController : UIViewController { 

    IBOutlet UIView *portrait; 
    IBOutlet UIView *landscape; 

} 

@property(nonatomic,retain) UIView *portrait; 
@property(nonatomic,retain) UIView *landscape; 

@end 

此外,@synthesize portrait,landscape;需要去下@implementation TestViewController

@implementation TestViewController 
@synthesize portrait,landscape; 

通知了UIView的這個圖片: alt text

+0

嗯。它仍然在啓動時崩潰,但:/ – Baccalad 2011-01-21 23:51:51

+0

您是否在Interface Builder中連接它們?你需要在IB中創建2個獨立的UIViews。 – WrightsCS 2011-01-21 23:52:19