单例中的UISplitViewController委托

时间:2012-02-22 12:22:16

标签: objective-c singleton uisplitviewcontroller

我对UISplitView进行了大量研究,当主人和细节的视图发生变化时,他无法找到控制拆分视图的方法。

然后我找到了一种使用作为委托的单例类来管理它的方法。

我的问题是,我不确定这是否是正确的方法。我关注reusabilitymemory managment。此外,我还有一种感觉,那就是让单身人士成为代表的Apple指南。

这就是我所拥有的(它实际上正在发挥作用):

//  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

现在实施:

//  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

此代码可以免费使用和修改,以便在项目中发现它可行:)。

我是StackOverflow的新手(即使我已经潜伏了几个月没有帐户),所以每一个批评都受到热烈欢迎。

2 个答案:

答案 0 :(得分:2)

恕我直言,每一种设计模式,架构都是“好”的,如果它符合您必须解决的“问题”(并符合您个人对代码组织的偏好)

  • 你的问题是什么?
  • 你为什么需要这个对象?
  • 这个单身UISplitViewDelegate可能是你的吗? UIApplicationDelegate? (保持简单; - )

进一步讨论=&gt;

如果你UIApplicationDelegate混乱,而不是创建子对象,我最近一直用来组织我的代码的方案:使用类别和类扩展

示例:

如果我的ViewController类处理复杂的任务,其代码可以分组。
让我们说:

  • 的声音
  • 核心数据
  • 位置感知,

我为每个

创建了一个类别
  • UIViewController+soundManager
  • UIViewController+dataProvider
  • UIViewController+locationManager

(在几个@interface @implementation的同一个文件中,或在不同的文件中=&gt;我使用多个文件)

然后在每个类别中,我为这个特定类别需要的属性编写一个类扩展。

答案 1 :(得分:0)

上次我通过继承UISplitViewController并使用将其作为自己的代表来解决这个问题。