iOS load new storyboard

我的梦境 提交于 2019-12-11 05:14:14

问题


Simple iOS App with a storyboard - StoryboardA

When the iPad changes orientation - I would like to load a new storyboard - StoryboardB

Where do I put that logic.

Is this a good practice when the UI in landscape (and it's navigation) is different than in portrait.


回答1:


The best practice when rotating the screen is to relayout the views. This is the whole reason behind the autoresizing (springs and struts) that has been around for a while, as well as the new autolayout functionality. You're supposed to be able to use this to reorganize your views as necessary when rotating, and you should ideally see a smooth rotation as a result. It's discouraged to destroy and recreate resources such as UI elements upon rotation, unlike in the Android world where that's actually the preferred and default method.

Generally if the UI is actually different in some way between portrait and landscape, it still has much in common, so there might be an additional section that appears in landscape, for example, which gets hidden in portrait. I would recommend against radically changing your UI upon rotation, because this is definitely not the norm and would be jarring to users. You can easily hide or show an additional section when rotating without much hassle.

If you must recreate your views upon rotation instead of rearranging them, you can at least still keep them in the same storyboard. It isn't necessary to use a whole new storyboard just for this purpose.

As for where you would put your logic for handling this, if you did decide to go with this approach, it would depend on what exactly needs to change upon rotation. If every view controller needs to be recreated, you may have the logic to handle this in your app delegate. But if it's just specific view controllers, you might put the logic to detect and handle these cases only in those specific controllers.

As I said, I highly discourage this approach, and I think it would be much better to use the mechanisms that already exist to relayout your views upon rotation.




回答2:


You can register for UIDeviceOrientationDidChangeNotification in following way

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

and on being notified of above mentioned UIDeviceOrientationDidChangeNotification, your orientationChanged: method will get fired, in which you can implement whatever layout changes you want to implement as follows:-

Create a BOOL flag in your private interface, isShowingLandscapeView, to keep track of which orientation currently device is on and act accordingly

BOOL isShowingLandscapeView = NO;

- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
        !isShowingLandscapeView)
    {
        // Changes pertaining to lanscape orientation here
        isShowingLandscapeView = YES;
    }
    else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
         isShowingLandscapeView)
    {
        // Changes pertaining to portrait orientation here
        isShowingLandscapeView = NO;
    }
}



回答3:


You can load other storyboard in following way.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"your storyboard" bundle:nil];
UIViewController *initialViewController = [storyboard instantiateInitialViewController];
[[self navigationController] pushViewController:initialViewController animated:YES];


来源:https://stackoverflow.com/questions/20912080/ios-load-new-storyboard

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!