单例中的 UISplitViewController 委托

发布于 2025-01-08 14:15:08 字数 3772 浏览 3 评论 0原文

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

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

我的问题是我不确定这是否是正确的方法。我担心可重用性内存管理。另外,我有一种感觉,让代表成为单例是违反苹果指南的。

这就是我所拥有的(并且它实际上正在工作):

//  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 的新手(尽管我已经潜伏了几个月而没有账户),所以每一个批评都受到热烈欢迎。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以为你会在 2025-01-15 14:15:08

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

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

进一步讨论=>

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

示例:

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

假设:

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

我为每个

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

(在具有多个@interface @implementation的同一文件中,或在不同的文件中=>我使用多个文件)

然后,我为每个类别编写一个类扩展,用于该特定类别所需的属性。

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.

哭了丶谁疼 2025-01-15 14:15:08

上次我通过子类化 UISplitViewController 并使用它作为自己的委托解决了这个问题。

Last time I solved this by subclassing the UISplitViewController and used it as his own delegate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文