简体   繁体   中英

Inherit delegate methods from custom class and UIViewController (Objective C)

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

在此处输入图片说明

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM