在iOS iOS 4.3中支持纵向和横向界面方向的最佳实践

时间:2012-01-26 08:17:40

标签: ipad ios4 uiviewcontroller orientation nib

在文件中,

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/RespondingtoDeviceOrientationChanges/RespondingtoDeviceOrientationChanges.html#//apple_ref/doc/uid/TP40007457-CH7-SW6

他们使用了 performSegueWithIdentifier:sender: dismissViewControllerAnimated:completion:,它们在iOS 5.0及更高版本中可用。

在iOS 4.3中,是否有任何智能方法可以将多个笔尖用于不同的ipad界面方向。

1 个答案:

答案 0 :(得分:0)

假设您有一个PortraitViewController和一个LandscapeViewController。

您需要以下内容:

@interface PortraitViewController : UIViewController {
    BOOL isShowingLandscapeView;
    LandscapeViewController *landscapeViewController;
}

@implementation PortraitViewController

- (void)viewDidLoad 
{
    [super viewDidLoad];

    landscapeMainViewController = [[LandscapeMainViewController alloc] initWithNibName:@"LandscapeMainViewController" bundle:nil];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[UIDevice currentDevice]  endGeneratingDeviceOrientationNotifications];
    [landscapeMainViewController release]; landscapeMainViewController = nil;
    [super dealloc];
}

- (void)orientationChanged:(NSNotification *)notification
{
    [self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}

- (void)updateLandscapeView
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
    {
        [self presentModalViewController:landscapeMainViewController animated:YES];
        isShowingLandscapeView = YES;
    }
    else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
    {
        [self dismissModalViewControllerAnimated:YES];
        isShowingLandscapeView = NO;
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end


@interface LandscapeViewController : UIViewController
@end

@implementation LandscapeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil  bundle:nibBundleOrNil])
    {
        self.wantsFullScreenLayout = YES;
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    }
    return self;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

@end

致谢:http://www.edumobile.org/iphone/iphone-programming-tutorials/alternativeviews-in-iphone/