1

I want to include some UIKeyCommands in my app. My app consists of one UISplitViewController that forces the master to be always visible on iPad full screen. On smaller screen it works like it normally would.

Now, I've implemented some UIKeyCommands in the MasterViewController and some in the DetailViewController. However, the app will only show those in DetailViewController. So I put all of them in the RootSplitViewController, but that will show all of them, even when the MasterViewController is hidden in iOS 9's splitview.

What I want though, is for it to show all when the app is fullscreen on iPad and thus the MasterViewController is forced on screen together with the DetailViewController. And when the view is small (ie 50-50) and the MasterViewController is hidden, I want it to only show those of the window that's on screen.

Any ideas on how to achieve this?

4

1 回答 1

2

最后我设法做到了这一点——尽管以一种不那么漂亮的方式。

UIKeyCommands添加到RootSplitViewController.

- (NSArray *)keyCommands {
    if (self.view.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular) {
        return @[
                    [UIKeyCommand keyCommandWithInput:@"r" modifierFlags:UIKeyModifierCommand action:@selector(changeRestaurant:) discoverabilityTitle:@"Change restaurant"],
                    [UIKeyCommand keyCommandWithInput:@"t" modifierFlags:UIKeyModifierCommand action:@selector(changeTable:) discoverabilityTitle:@"Change table"]
                ];
    } else {
        if (self.masterIsVisible == YES) {
            return @[
                         [UIKeyCommand keyCommandWithInput:@"t" modifierFlags:UIKeyModifierCommand action:@selector(changeRestaurant:) discoverabilityTitle:@"Change restaurant"]
                    ];
        } else {
            return @[
                         [UIKeyCommand keyCommandWithInput:@"t" modifierFlags:UIKeyModifierCommand action:@selector(changeTable:) discoverabilityTitle:@"Change table"]
                    ];
        }
    }
}

这些方法调用特定的实际方法UIViewController

- (void)changeRestaurant:(id)sender {
    UINavigationController *nav = (UINavigationController *)[self.viewControllers objectAtIndex:0];
    RestaurantController *master = [nav.viewControllers objectAtIndex:0];
    [master changeRestaurant];
}

- (void)changeTable:(id)sender {
    UINavigationController *nav = (UINavigationController *)[self.viewControllers objectAtIndex:1];
    TableController *detail = [nav.viewControllers objectAtIndex:0];
    [detail changeTable:sender];
}

为了使它起作用,我BOOLUISplitViewController.

@interface RootSplitViewController : UISplitViewController

@property (nonatomic) BOOL masterIsVisible;

@end

然后在MasterViewController.

- (void)viewDidDisappear:(BOOL)animated {
    RootSplitViewController *rootView = (RootSplitViewController *)self.splitViewController;
    rootView.masterIsVisible = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    RootSplitViewController *rootView = (RootSplitViewController *)self.splitViewController;
    rootView.masterIsVisible = YES;
}

我知道这可能不是漂亮的方法,但它有效。如果有人知道更好的方法,我很想听听您的反馈。

于 2016-07-24T21:06:31.857 回答