2011-01-21 141 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