【问题标题】:Inherit delegate methods from custom class and UIViewController (Objective C)从自定义类和 UIViewController 继承委托方法(目标 C)
【发布时间】:2016-12-16 12:40:49
【问题描述】:

我有一个实现自定义类方法的主控制器。现在在主控制器中,我想在需要实现 UITableView 委托方法的警报视图中添加一个表。我想实现类似下面的东西

单击Click Here 按钮后,将显示一个警报视图,其中包含一个显示所有项目的表格。

CustomController 定义如下

@interface CustomController : UIViewController

现在

在 .h 文件中

@interface mainController : CustomController<CustomDelegateMethod>

在 .m 文件中

@interface mainController()

必填

在 .h 文件中

@interface mainController : CustomController<CustomDelegateMethod>

// 我想在上面的行中添加UIViewController<UITableViewDelegate, UITableViewDataSource>

MainController 中的 AlertView 代码:

UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"List of Items" message:nil delegate:self cancelButtonTitle:@"Back" otherButtonTitles:@"Add New Item", nil];

    tableView = [([UITableView alloc])initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor clearColor];
    [av addSubview:tableView];
    [av show];

如何实现来自不同控制器的委托方法? 因为当我添加 @interface mainController : CustomController<CustomDelegateMethod>,UIViewController<UITableViewDelegate, UITableViewDataSource> 它抛出错误Expected identifier or '(' 如果我像这样添加 tableview 委托方法 @interface mainController : CustomController<CustomDelegateMethod,UITableViewDelegate, UITableViewDataSource> ,运行应用程序时不会调用委托方法。

【问题讨论】:

  • 那么你的问题是什么?

标签: ios objective-c uitableview ipad uiviewcontroller


【解决方案1】:

试试:

在.h文件中

@interface mainController : CustomController

在 .m 文件中

@interface mainController() <CustomDelegateMethod,UITableViewDelegate, UITableViewDataSource>

@property(nonatomic,strong)UITableView * tableView;

@end

UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"List of Items" message:nil delegate:self cancelButtonTitle:@"Back" otherButtonTitles:@"Add New Item", nil];

UITableView *tableView = [([UITableView alloc])initWithFrame:CGRectZero style:UITableViewStyleGrouped];
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = [UIColor clearColor];
self.tableView = tableView;
[av addSubview:tableView];
[av show];

【讨论】:

  • 到目前为止没有抛出任何错误,但是当我运行代码时,没有调用 .m 文件中的委托方法。 (我尝试过类似的方法,但我在 .h 文件中添加了委托方法)
  • 无结果 :( 不知道我的委托方法调用是否正确
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-02
  • 1970-01-01
  • 2020-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多