最後,我設法做到了這一點 - 儘管方式不太好。
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];
}
爲了這個工作我添加了一個BOOL
到UISplitViewController
。
@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;
}
我知道這可能不是漂亮的方法,但它的工作原理。如果有人知道更好的方法來做到這一點,我很樂意聽取您的意見。