ios - 重新定义目标操作方法以便 `UIButtons` 将消息中继到我的 `UIViewController` 中的方法的最安全方法是什么?

标签 ios objective-c uiview uiviewcontroller

我正在通过创建自定义 UIView 来重组旧代码分隔属于 View 的 UI 元素来自那些属于 ViewController. 的人以前包含在多个 ViewController 中的几个按钮现在位于一个自定义中 UIView我需要他们向当前 viewController 中的方法发送消息这样用户就可以导航到其他 viewControllers.

This answer在 SO 上建议使用委托(delegate),而 this answer对同一个问题建议不要使用委托(delegate)。 This answer换一个问题也是有道理的。但我不知道哪一个最适合我的需要。我不愿意做任何可能破坏当前 appDelegate 的事情它管理一个 MultiviewViewController (依次在 ViewControllers).

之间切换

所以我的问题是:

重新定义目标- Action 方法的最安全方法是什么 UIButtons将消息中继到我的 UIViewController 中的方法?

下面我的代码的“精简”版本显示了我正在尝试做的事情

安全 - 澄清

By safe, I simply meant reliable i.e. not likely to introduce unintended consequences at runtime. The underlying motivation for the question is to keep UI elements where they belong - i.e. in a UIView or in a UIViewController - without breaking an existing app.

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

ViewController.m

#import "ViewController.h"
#import "CustomView.h"

@interface ViewController ()

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    CGRect rect                 = [UIScreen mainScreen].bounds;
    float  statusBarHeight      = [[UIApplication sharedApplication] statusBarFrame].size.height;
    CGRect screenFrame          = CGRectMake(0, statusBarHeight, rect.size.width, rect.size.height - statusBarHeight);
    self.view                   = [[UIView alloc] initWithFrame: screenFrame];

    self.view.backgroundColor   = [UIColor lightGrayColor];   

    CustomView *cv              = [[CustomView alloc]initWithFrame:screenFrame];    //create an instance of custom view
    [self.view addSubview:cv];                                                      // add to your main view
}


- (void)goTo1                                             {
        NSLog(@"switch to Family 1");

    //  * commented out from the original app
    //  MultiviewAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    //    [parent setSelectedZone:1];
    //  [appDelegate displayView:1];
}

- (void)goTo2                                             {
        NSLog(@"switch to Family 2");

    //  * commented out from the original app
    //  MultiviewAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    //    [parent setSelectedZone:2];
    //  [appDelegate displayView:1];
}

- (void)goTo3                                             {
        NSLog(@"switch to Family 3");

    //  * commented out from the original app
    //  MultiviewAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    //    [parent setSelectedZone:3];
    //  [appDelegate displayView:1];
}

@end

自定义 View .h

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

@interface CustomView : UIView {
}

@end

自定义 View .m

#import <Foundation/Foundation.h>
#import "CustomView.h"

@interface CustomView ()

- (UIViewController *)viewController;

@end


@implementation CustomView : UIView

- (UIViewController *)viewController {
    if ([self.nextResponder isKindOfClass:UIViewController.class])
        return (UIViewController *)self.nextResponder;
    else
        return nil;
}


- (id)initWithFrame:(CGRect)frame
{
    self                                  = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {

        [self threeButtons];
    }
    return self;
}


- (void)buttonPicked:(UIButton*)button                          {
    NSLog(@"Button %ld : - send message to UIViewController instead.”, (long int)[button tag]);

    switch (button.tag) {
        case 1:
            //            [self goTo1];
            break;
        case 2:
            //            [self goTo2];
            break;
        case 3:
            //            [self goTo3];
            break;
        default:
            break;
    }
}



- (void)threeButtons {

    int     count                         = 3;
    int     space                         = 5;
    float   size                          = 60;

    for (int i = 1; i <= count; i++) {

        CGFloat x                         = (i * (size + space)) + 40;
        CGFloat y                         = 100;
        CGFloat wide                      = size;
        CGFloat high                      = size;
        UIButton *buttonInView            = [[UIButton alloc] initWithFrame:CGRectMake(x, y, wide, high)];

        [buttonInView setTag:i];
        [buttonInView addTarget:self action:@selector(buttonPicked:) forControlEvents:UIControlEventTouchUpInside];

        buttonInView.layer.borderWidth    = 0.25f;
        buttonInView.layer.cornerRadius   = size/2;
        [buttonInView setTitle:[NSString stringWithFormat:@"%i", i] forState:UIControlStateNormal];
        [buttonInView setTitleColor: [UIColor blackColor] forState:UIControlStateNormal];
        buttonInView.layer.borderColor    = [UIColor blackColor].CGColor;
        buttonInView.backgroundColor      = UIColor.whiteColor;

        [self addSubview:buttonInView];
    }
}

@end

最佳答案

3rd answer you linked 的开始状态:

I thinks it is not a good idea to know inside the view about parent structures. It is breaks encapsulation and leads to hard maintenance, bugs and extra relations.

我认为这只对了一半。我要声明的是,不仅自定义 View 不应该知道任何可能使用它的 Controller ,使用自定义 View 的 Controller 也不应该知道自定义 View 的任何细节。

UITableView为例。 TableView 对使用 TableView 的类一无所知。这是通过使用其委托(delegate)和数据源协议(protocol)实现的。同时,使用 TableView 的 Controller 没有任何直接知识或 Hook 到 TableView 的 View 结构(除了与 TableView 单元格关联的特定 API 之外)。

您的自定义 View 的任何用户都不应该知道哪些 UI 组件构成了该 View 。它应该只公开指示某些高级事件发生的事件,而不是特定的 UIButton(或其他)被点击的事件。

考虑到这些想法,您链接的第一个答案是最好的。使用适当的界面设计您的自定义 View ,而不是隐藏其内部细节。这可以使用委托(delegate)协议(protocol)、通知(如 NSNotificationCenter 中)或事件 block 属性来完成。

通过采用这种方法,您的自定义 View 的实现可以完全改变,而无需更改其事件接口(interface)。您现在可以用其他一些自定义 View 替换 UIButton (例如)。您只需调整代码以调用相同的委托(delegate)方法(或发布一些适当的通知)。不用担心,自定义 View 的每个客户端现在都需要从调用 addTarget... 更改为其他适当的代码。所有客户端都只是继续实现自定义 View 的相同旧委托(delegate)方法。

在您的具体情况下,不要认为您的自定义 View 具有某些 Controller 需要处理的三个按钮。将您的自定义 View 视为可能发生 3 个不同的事件。自定义 View 的客户端只需要知道发生了其中一个事件以及发生了哪一个事件。但是发送到自定义 View 客户端的所有信息都不应该包含有关 UIButton 的任何信息。在更抽象的级别上对这 3 个事件进行分类,具体取决于这些事件所代表的内容,而不是它们的实现方式。

关于ios - 重新定义目标操作方法以便 `UIButtons` 将消息中继到我的 `UIViewController` 中的方法的最安全方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42965659/

相关文章:

ios - 我想在不停止另一个应用程序后台播放音乐的情况下制作音效

iphone - 奇怪的架构,没有 appDelegate 类的应用程序。 (AVCam 源链接)

objective-c - 通过对两个参数进行加权对 NSArray 进行排序

ios - 将大小为 10x10 的 UIImageView 设置为 UIView 的背景

ios - SDK更新后,UserCannotBeAlteredWithoutSessionError

ios - UICollectionView 垂直居中

iphone - 从 UITableView 创建 UIImage

ios - 以编程方式将文本插入 UITextView 而不移动插入符号位置。

ios - UIView 类中的 Swift Collection View 委托(delegate)

ios - Swift:UIView.transitionWithView 完成 block 问题