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:

Xcode Storyboard中的基本布局

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:

  • Even though this works I've been coding long enough to feel like I'm going in the wrong direction and I'm wondering if there is there a better approach to my task?
  • I did not go with Notifications because I did't want multiple views responding to the same message and having to determine which view should respond (plus Event systems can become difficult to manage), but all the same would this have been a better approach? For example, perhaps simply unregistering the observer of each view before segueing to the next view was all I needed to do?
  • As stated earlier, I didnt use the Navigation Controller toolbar because AFAIK that would mean adding the toolbar buttons in to every view. It also seems like there are some layout restrictions with toolbars, such as defining a height or removing any trace of a border. With that said, would this have been the correct approach to take (and make each child view a delegate of the viewController toolbar)?
  • Are there any watch-outs from the code above, particularly with memory (I'm using ARC)?
  • Many thanks for your advice.

    链接地址: http://www.djcxy.com/p/52412.html

    上一篇: UITableView,我怎么知道cellForRowAtIndexPath中的部分?

    下一篇: 在充当代表的视图之间创建共享工具栏