切换旋转视图时,iOS应用程序在打开时崩溃

时间:2011-01-21 23:31:23

标签: cocoa-touch ios ios4

我正在尝试在XCode中开发一个应用程序,它将在轮换时切换到新视图。

以下是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

无论如何应用程序打开但在打开时崩溃。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;
}

答案 2 :(得分:0)

我会做以下事情:

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

希望这有帮助!