开发者

container viewcontrollers versus single custom viewcontrollers on iPad app

开发者 https://www.devze.com 2023-02-24 23:13 出处:网络
I am converting an existing iPhone app for the iPad app. The iPhone app was built using a container viewcontroller (UINavigationController) which presented the user first with a custom viewcontroller

I am converting an existing iPhone app for the iPad app. The iPhone app was built using a container viewcontroller (UINavigationController) which presented the user first with a custom viewcontroller (UITableViewController) that pushed a custom viewcontroller (UIViewController) based on row selection.

In the iPad app, I am presenting the user directly with the custom UIViewController开发者_JS百科 (with NO container controller) and then allow selection of different options via a UIPopoverController. In myAppDelegate.m I am simply adding the custom UIViewController to the window using:

[window addSubview:[myCustomViewController view]];

In myCustomViewController.m I am modifying the view heavily based upon device rotation by registering for orientation change notifications in viewWillAppear:

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didRotate:)                                                  name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}

I am then testing the orientation in the didRotate: method and getting very strange results. It is being called three times simply loading the view? It also seems to be reporting the orientation corresponding to the PREVIOUS drawing of the view?

- (void) didRotate:(NSNotification *)notification
{   
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
        NSLog(@"Portrait");
    } else if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        NSLog(@"Landscape");
    }
}

I was reading in the docs and it appears that adding the subview to the window (without a container class) will not cause viewWillAppear: method to be called, but in my case it seems it is being called, just unreliably.

Is there some other pattern I should be using for this app? I simply want to load a single custom view and use two popover controllers (no other navigation)?

-Derrick

btw - It works exactly as it should if I push the custom viewController onto a UINavigationController in my app delegate. I just don't need a nav controller for this app.


In my app I'm working on, I first have a property to find out if the device is an iPad:

- (BOOL)iPad {

    return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? YES : NO;
}

And then you can use the following delegate method of your view.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

if (self.iPad) {
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
        toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    //do some stuff 
    }   

}

Hope this helps.

0

精彩评论

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