Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISplitViewController delegate in a singleton

I did a lot of research on UISplitView and was not able to find a way to control a Split View when the Master and the Detail has a view that changes.

Then I found a way to manage it with a singleton class that is the delegate.

My problem is that i'm not sure if it's the right way to go. I'm concerned about reusability and memory managment. Also I have a feeling that it's aginst Apple guidelines to make delegates in singletons.

This is what I have (and it's actually working):

//  SharedSplitViewDelegate.h

/* In the detail view controllers:

 // in the initial detail view controller
 - (void)awakeFromNib
 {
 [super awakeFromNib];
 // needs to be here, otherwise if it's booted in portrait the button is not set
 self.splitViewController.delegate = [SharedSplitViewDelegate initSharedSplitViewDelegate];
 }

 // shared between all detail view controllers
 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
 {
 SharedSplitViewDelegate *rotationHandler = [SharedSplitViewDelegate initSharedSplitViewDelegate];
 [self.toolbar setItems:[rotationHandler processButtonArray:self.toolbar.items] animated:YES];
 }

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
 return YES;
 }

 */

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface SharedSplitViewDelegate : NSObject <UISplitViewControllerDelegate>

+ (id)initSharedSplitViewDelegate; // returns the singleton class instance

- (NSArray *)processButtonArray:(NSArray *)array; // Adds and removes the button from the toolbar array. Returns the modified array.

@end

Now the implementation:

//  SharedSplitViewDelegate.m

#import "SharedSplitViewDelegate.h"

@interface SharedSplitViewDelegate()
@property (nonatomic, strong) UIBarButtonItem *button;
@property (nonatomic, strong) UIBarButtonItem *cachedButton;
@end

@implementation SharedSplitViewDelegate

@synthesize button = _button;
@synthesize cachedButton = _cachedButton;

#pragma mark - Singleton class definition

static id sharedSplitViewDelegate = nil;

+ (void)initialize
{    
    if (self == [SharedSplitViewDelegate class]) {
        sharedSplitViewDelegate = [[self alloc] init];
    }
}

+ (id)initSharedSplitViewDelegate {
    return sharedSplitViewDelegate;
}

#pragma mark - Split view delegate methods

- (BOOL)splitViewController:(UISplitViewController *)svc
   shouldHideViewController:(UIViewController *)vc
              inOrientation:(UIInterfaceOrientation)orientation
{
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
        return NO;
    } else {
        return YES;
    }
}

- (void)splitViewController:(UISplitViewController *)svc
     willHideViewController:(UIViewController *)aViewController
          withBarButtonItem:(UIBarButtonItem *)barButtonItem
       forPopoverController:(UIPopoverController *)pc
{
    barButtonItem.title = @"Browse";
    self.button = barButtonItem;
}

- (void)splitViewController:(UISplitViewController *)svc
     willShowViewController:(UIViewController *)aViewController
  invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    self.button = nil;
}

#pragma mark - Utility methods

- (void)setButton:(UIBarButtonItem *)button
{
    if (button != _button) {
        _button = button;
    }

    if (button != nil) {
        self.cachedButton = button;
    }
}

- (NSArray *)processButtonArray:(NSArray *)array
{
    NSMutableArray *processedArray = [array mutableCopy];
    if (self.button != nil && ![processedArray containsObject:self.button]) {
        [processedArray insertObject:self.button atIndex:0];
    } else if (self.button == nil && [processedArray containsObject:self.cachedButton]) {
        [processedArray removeObjectAtIndex:0];
    }

    return [processedArray copy];
}

@end

This code is free to use and modify for everyone that would find it viable in their project :).

I'm new to StackOverflow (even though I've lurked for a couple months without an account) so every critique is warmly welcomed.

like image 865
zamber Avatar asked Feb 22 '12 12:02

zamber


1 Answers

IMHO, every design pattern, architecture, is 'good' if it fits the 'problem' you have to solve (and fits your personal preferences for code organisation)

  • What's your problem ?
  • Why do you need this object ?
  • Could this singleton UISplitViewDelegate be your UIApplicationDelegate ? (Keep it Simple ;-)

further discussion =>

If you UIApplicationDelegate is a mess, rather than creating sub-object, a scheme I've been using recently to organize my code : use categories and class extensions

Example :

If my ViewController class handles complex tasks whose code can be separated in groups.
let's say :

  • sound
  • core data
  • location-aware,

I create a category for each of these

  • UIViewController+soundManager
  • UIViewController+dataProvider
  • UIViewController+locationManager.

(in same file with several @interface @implementation, or in different files => i use several files)

Then along with each category I write a class-extension for properties this particular category needs.

like image 161
Vinzzz Avatar answered Sep 17 '22 13:09

Vinzzz