如何从父类调用一个方法到它的子类? [英] How to call a method from parent class to it's child class?
问题描述
在我的主视图控制器中有一个如下所示的方法:
-(void)updateCartItem {}
我想在一个按钮动作方法下把它调用到uitableviewcell类中:
- (IBAction)Cart:(id)sender {[self updateCartItem];//这样你就可以调用包含你的VC.m文件的父方法}
请帮我提前谢谢...
您可以使用 Delegate
或代码块来执行此操作,我将同时发布两种方式并附上示例供您说明
委托方法
1 - 声明你的单元格代表
我们的示例单元格将被称为 CustomTableViewCell
@protocol CustomCellDelegate-(void)executeAction;@结尾
并将您的委托添加到您的单元格声明中,必须弱以避免保留循环
@property (weak) id细胞委托;
2 - 在 Cell Action 中执行委托操作
- (IBAction)Cart:(id)sender {[[self cellDelegate] executeAction];}
3 - 让你的 UIViewController 实现你的 CustomCell 的 CustomCellDelegate
@interface ViewController() -(void)executeAction{[自我更新CartItem];}
4 - 将您的 UIViewController
作为您的 CustomCell 的 Delegate
调整您的 cellForRowAtIndexPath
方法强>
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{静态 NSString *simpleTableIdentifier = @"CustomCell";CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];cell.cellDelegate = self;返回单元格;}
<块引用>
完整代码
CustomTableViewCell.h
#import @protocol CustomCellDelegate-(void)executeAction;@结尾@interface CustomTableViewCell : UITableViewCell@property (weak) id细胞委托;@结尾
CustomTableViewCell.m
#import "CustomTableViewCell.h"@implementation CustomTableViewCell- (IBAction)Cart:(id)sender {[[self cellDelegate] executeAction];}@结尾
ViewController.m
#import "ViewController.h"#import "CustomTableViewCell.h"@interface ViewController () @property (weak, nonatomic) IBOutlet UITableView *tableView;@结尾@实现视图控制器- (void)viewDidLoad {[超级viewDidLoad];//在加载视图后做任何额外的设置,通常是从笔尖.[_tableView setDelegate:self];[_tableView setDataSource:self];}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{返回 10;}-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{静态 NSString *simpleTableIdentifier = @"CustomCell";CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];cell.cellDelegate = self;返回单元格;}-(void)updateCartItem {//无论你需要在这里做什么}-(void)executeAction{[自我更新CartItem];}@结尾
<小时>
代码块方法
1 - 在自定义单元格中声明 actionBlock
我们的示例单元格将被称为 CustomCell
@property void(^actionBlock)(void);
2 - 在 Cell Action 中执行您的操作块
- (IBAction)Cart:(id)sender {[自我动作块];}
3 - 设置您的单元格块操作,调整您的 cellForRowAtIndexPath 方法
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{静态 NSString *simpleTableIdentifier = @"CustomCell";CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];__weak ViewController *weakSelf = self;cell.actionBlock = ^{[weakSelf updateCartItem];};返回单元格;}
<块引用>
完整代码
CustomTableViewCell.h
#import @interface CustomTableViewCell : UITableViewCell@property void(^actionBlock)(void);@结尾
CustomTableViewCell.m
#import "CustomTableViewCell.h"@implementation CustomTableViewCell- (IBAction)Cart:(id)sender {[自我动作块];}@结尾
ViewController.m
#import "ViewController.h"#import "CustomTableViewCell.h"@interface ViewController() @property (weak, nonatomic) IBOutlet UITableView *tableView;@结尾@实现视图控制器- (void)viewDidLoad {[超级viewDidLoad];//在加载视图后做任何额外的设置,通常是从笔尖.[_tableView setDelegate:self];[_tableView setDataSource:self];}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{返回 10;}-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{静态 NSString *simpleTableIdentifier = @"CustomCell";CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];__weak ViewController *weakSelf = self;cell.actionBlock = ^{[weakSelf updateCartItem];};返回单元格;}-(void)updateCartItem {//无论你需要在这里做什么}@结尾
In my main viewcontroller there is a method like as below:
-(void)updateCartItem {
}
I want to call it into uitableviewcell class under a button action method:
- (IBAction)Cart:(id)sender {
[self updateCartItem];//like this you can call parent method which contain your VC.m file
}
please help me thanks in advance...
You can use a Delegate
or a code block to do this, I will post both ways with examples for your clarification
Delegate Approach
1 - Declare your cell Delegate
Our example cell will be called CustomTableViewCell
@protocol CustomCellDelegate
-(void)executeAction;
@end
and add your delegate to your cell declaration, must be weak to avoid retain cycles
@property (weak) id<CustomCellDelegate> cellDelegate;
2 - Execute your delegate action in your Cell Action
- (IBAction)Cart:(id)sender {
[[self cellDelegate] executeAction];
}
3 - Make your UIViewController implement CustomCellDelegate of your CustomCell
@interface ViewController () <UITableViewDataSource,UITableViewDelegate,CustomCellDelegate>
-(void)executeAction
{
[self updateCartItem];
}
4 - Make your UIViewController
as Delegate
of your CustomCell adjusting your cellForRowAtIndexPath
method
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CustomCell";
CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];
cell.cellDelegate = self;
return cell;
}
Full Code
CustomTableViewCell.h
#import <UIKit/UIKit.h>
@protocol CustomCellDelegate
-(void)executeAction;
@end
@interface CustomTableViewCell : UITableViewCell
@property (weak) id<CustomCellDelegate> cellDelegate;
@end
CustomTableViewCell.m
#import "CustomTableViewCell.h"
@implementation CustomTableViewCell
- (IBAction)Cart:(id)sender {
[[self cellDelegate] executeAction];
}
@end
ViewController.m
#import "ViewController.h"
#import "CustomTableViewCell.h"
@interface ViewController () <UITableViewDataSource,UITableViewDelegate,CustomCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[_tableView setDelegate:self];
[_tableView setDataSource:self];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CustomCell";
CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];
cell.cellDelegate = self;
return cell;
}
-(void)updateCartItem {
//Whatever you need to do here
}
-(void)executeAction
{
[self updateCartItem];
}
@end
Code Block Approach
1 - Declare your actionBlock in your Customcell
Our example cell will be called CustomCell
@property void(^actionBlock)(void);
2 - Execute your action block in your Cell Action
- (IBAction)Cart:(id)sender {
[self actionBlock];
}
3 - Setup your cell block action adjusting your cellForRowAtIndexPath method
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CustomCell";
CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];
__weak ViewController *weakSelf = self;
cell.actionBlock = ^{
[weakSelf updateCartItem];
};
return cell;
}
Full Code
CustomTableViewCell.h
#import <UIKit/UIKit.h>
@interface CustomTableViewCell : UITableViewCell
@property void(^actionBlock)(void);
@end
CustomTableViewCell.m
#import "CustomTableViewCell.h"
@implementation CustomTableViewCell
- (IBAction)Cart:(id)sender {
[self actionBlock];
}
@end
ViewController.m
#import "ViewController.h"
#import "CustomTableViewCell.h"
@interface ViewController () <UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[_tableView setDelegate:self];
[_tableView setDataSource:self];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CustomCell";
CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];
__weak ViewController *weakSelf = self;
cell.actionBlock = ^{
[weakSelf updateCartItem];
};
return cell;
}
-(void)updateCartItem {
//Whatever you need to do here
}
@end
这篇关于如何从父类调用一个方法到它的子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!