开发者

iOS app crashes upon opening when switching rotation views

I am trying to develop an app in XCode that will switch to a new view upon rotation.

Here is the code for 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

and here is the code for 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开发者_如何学Go;
    }

    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

Anyway so the app opens but it crashes upon opening.


Try something like:

- (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;
}


Your brackets in your header are misplaced.

@interface TestViewController : UIViewController {

    IBOutlet UIView *portrait;
    IBOutlet UIView *landscape;

}

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

@end

Also, @synthesize portrait,landscape; needs to go under @implementation TestViewController

@implementation TestViewController
@synthesize portrait,landscape;

Notice the UIView's in this image:

iOS app crashes upon opening when switching rotation views


I would do the following:

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

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜