Inherit delegate methods from custom class and UIV

2019-09-11 07:03发布

I have a main controller which implements methods from a custom class. Now in the main controller I want to add a table within an alert view for which I need to implement UITableView delegate methods. I want to implement something like below

enter image description here

On Clicking the Click Here Button, an alert view is displayed which contains a table displaying all the items.

CustomController is defined like below

@interface CustomController : UIViewController

PRESENT

in .h file

@interface mainController : CustomController<CustomDelegateMethod>

in .m file

@interface mainController()

REQUIRED

in .h file

@interface mainController : CustomController<CustomDelegateMethod>

// I want to add UIViewController<UITableViewDelegate, UITableViewDataSource> to the above line.

AlertView code in MainController:

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];

How can I implement delegate methods from different controllers? Because When I add @interface mainController : CustomController<CustomDelegateMethod>,UIViewController<UITableViewDelegate, UITableViewDataSource> it is throwing an error Expected identifier or '(' and if I add the tableview delegate methods like this @interface mainController : CustomController<CustomDelegateMethod,UITableViewDelegate, UITableViewDataSource> , delegate methods are not called when I run the application.

1条回答
闹够了就滚
2楼-- · 2019-09-11 07:34

Try:

in .h file

@interface mainController : CustomController

in .m file

@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];
查看更多
登录 后发表回答