【问题标题】:If I define a protocol in a base UIViewController will its children be able to conform to it in objective-c?如果我在基本 UIViewController 中定义一个协议,它的孩子是否能够在 Objective-c 中遵守它?
【发布时间】:2014-05-05 22:55:01
【问题描述】:

我有:

UITableViewControllerA(列出优化数据库产品结果的不同方法)

UIViewControllerB(与上面列表中的行之一关联的控制器)

BaseController(UIViewController 的子类,包含与 UITableViewControllerA 中的行关联的控制器的通用代码,是它们的超类)

我可以使用 perpareForSegue 方法轻松地将数据从UITableViewControllerA 传递给UIViewControllerB。我现在正尝试将数据从UIViewControllerB 传递到UITableViewControllerA,当在我的UIButton 自定义方法中调用popViewControllerAnimated 时,用户在选择了用于优化数据库产品结果的内容后点击该方法。

我决定使用委托。 UIViewControllerB 是 BaseController 的子类,在上面提到的 UITableViewControllerA 中,我有一个改进产品的方法列表:

性别 - 尺寸 - 颜色 - 价格 - 类型

我决定在 BaseController 中定义它,而不是在他们的每个控制器中定义我的协议:

#import <UIKit/UIKit.h>

@protocol BaseViewControllerDelegate <NSObject>

- (void)didTapDoneButtonWithSelection:(NSString *)selection; // use string for now to test

@end

@interface BaseViewController : UIViewController
@property (nonatomic, retain) id <BaseViewControllerDelegate> delegate;

- (UIButton *)clearButton;
- (UIButton *)doneButton;


@end

然后我转到 UIViewControllerB 并访问委托属性并传入要在点击完成按钮时发送到 UITableViewControllerA 的字符串:

- (void)doneButtonTapped:(id)sender {
    NSLog(@"DONE BUTTON TAPPED");
    [[self delegate] didTapDoneButtonWithSelection:@"THIS IS A TEST"];
    [[self navigationController] popViewControllerAnimated:YES];
}

在 UITableViewControllerA 我符合委托协议:

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

@interface UITableViewControllerA : UITableViewController <BaseViewControllerDelegate>

@end

在 UITableViewControllerA 的实现文件中,我声明了一个实例 var 来保存从 UIViewControllerB 传递过来的数据,并在 viewWIllAppear 的日志消息中显示。 didTapDoneButtonWithSelection 方法从参数中获取数据并将其存储在 _test 变量中:

@interface UITableViewControllerA ()

@end

@implementation UITableViewControllerA
{
    NSString *_test;
}

- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"%@", _test);
}

- (void)didTapDoneButtonWithSelection:(NSString *)selection
{
    _test = selection;
}

问题:

我的日志消息是给我的(null)。

我正在做的事情可能吗?还是我走错了路。如果是这样,请用代码示例纠正我。

感谢您的宝贵时间

问候

【问题讨论】:

  • 登录didTapDoneButtonWithSelection
  • 什么也没发生。根本没有日志。
  • 你设置了委托实例吗?
  • @MatthewClark 我所做的就是如上所示。我想这就是我错过的。让我试一试。

标签: ios objective-c cocoa-touch uiviewcontroller uitableview


【解决方案1】:

我必须获取 UIViewControllerB 的实例并将 UITableViewControllerA 设置为它在 prepareForSegue 方法中的委托:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    UITableViewControllerA *tvca = [segue destinationViewController];
    [tvca setDelegate:self];
}

但这感觉很奇怪,好像我应该用另一种方式来做这件事。

【讨论】:

    【解决方案2】:

    编辑:我认为最初误解了这个问题。

    如果您使用 segues,则正确答案是另一个。如果你不是,那么它是:

    在 UITableViewControllerA 中,当您要推送 BaseViewController 时,它应该如下所示:

    BaseViewController* base = [[BaseViewController alloc] init];
    base.delegate = self;
    [self.navigationController pushViewController:base animated:YES];
    

    【讨论】:

    • 这对我不起作用。没想到将数据传回之前的控制器会这么困难。
    猜你喜欢
    • 2010-11-25
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    相关资源
    最近更新 更多