gpt4 book ai didi

objective-c - 单例中的 UISplitViewController 委托(delegate)

转载 作者:太空狗 更新时间:2023-10-30 03:33:50 28 4
gpt4 key购买 nike

我对 UISplitView 进行了大量研究,但无法找到一种方法来控制当 Master 和 Detail 的 View 发生变化时的 Split View。

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

我的问题是我不确定这样做是否正确。我很关心可重用性内存管理。此外,我有一种感觉,让代表成为单例是违反 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 的新手(尽管我已经在没有帐户的情况下潜伏了几个月)所以热烈欢迎每一个批评。

最佳答案

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

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

进一步讨论 =>

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

示例:

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

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

我为每一个创建一个类别

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

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

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

关于objective-c - 单例中的 UISplitViewController 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9394636/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com