Creating a shared toolbar across views that act as delegates
Using Xcode 9.1 & Storyboards, I'm trying to create an application whereby the root view controller contains a Container View for subsequent content, plus a few buttons that I want to share across all of the content being loaded. I'm looking for some advice from anyone who has tried to do something similar with regards to my approach.
The first view's Container View loads a Navigation Controller, to handle the view stack and segues.
Here's the basic layout:
I'm considering using the Container View like this as it is a bit like having the Navigation Controller show its Toolbar on every view, but without having to recreate every button for every view.
My intention is to have each child view - when visible - become the delegate of the parent's button bar, so that whenever the Exit or Prev/Next (< / >) buttons are selected they're notified and can act accordingly. The root view controller is not going to be responsible for loading each view in to the navigation controller, and the behaviour of the buttons - particularly the Next button - will depend upon the view current loaded.
To this end I've created a protocol in the root view controller .h, using answers found elsewhere on SO:
@protocol RootViewControllerToolbarDelegate;
and defined the interface:
@protocol RootViewControllerToolbarDelegate <NSObject>
@optional
- (void)rootViewController:(RootViewController*)viewController
didChooseButton:(ToolbarButtonType)toolbarButtonType;
ToolbarButtonType
being a simple Enum. In the corresponding .m file of the root (parent) I've set up an IBAction for the buttons, and within this I'm notifying any registered delegate:
id<RootViewControllerToolbarDelegate> strongDelegate = self.delegate;
if ([strongDelegate respondsToSelector:@selector(rootViewController:didChooseButton:)]) {
[strongDelegate rootViewController:self didChooseButton:([sender tag] == 1 ? ToolbarButtonPrevious : ToolbarButtonNext)];
}
In a subsequent view - loaded in the the Navigation Controller (which is within the root view controller's Container View) - I've set up a test such that the .h implements the delegate I created in the root, assigning self to the delegate in viewDidLoad:
RootViewController *rvc = (RootViewController *)[UIApplication sharedApplication].keyWindow.rootViewController;
rvc.delegate = self;
and then defining the delegate method to handle the action.
- (void)rootViewController:(RootViewController*)viewController
didChooseButton:(ToolbarButtonType)toolbarButtonType{
switch (toolbarButtonType){
case ToolbarButtonNext: { //test
MyNextViewToLoad *emv = [self.storyboard instantiateViewControllerWithIdentifier:@"sbIdMyNextView"];
[self.navigationController pushViewController:emv animated:YES];
break;
}
default: break;
}
}
This is all working so far as this simple test is concerned at least, but I have questions:
Many thanks for your advice.
链接地址: http://www.djcxy.com/p/52412.html上一篇: UITableView,我怎么知道cellForRowAtIndexPath中的部分?
下一篇: 在充当代表的视图之间创建共享工具栏