iOS开发——使用代理(Delegate)实现跨界面执行跳转请求

本文介绍了如何通过定义代理协议(viewDelegate)来解决自定义UITableViewCell内的UIButton点击事件与UITableViewController间视图跳转的需求。通过实现协议方法,实现了UIButton点击后,触发视图控制器的跳转操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先说说我遇到的问题吧,我自定义了一个UITableViewCell,上面有一个UIButton按钮,我想通过点击这个按钮实现视图跳转,UIButton的点击触发的事件是写在UITableViewCell中的,但视图跳转必须是在UITableViewController中才能实现的。这时候我就想到了通过代理(Delegate)实现这一需求。

先创建一个协议继承NSObject,命名为viewDelegate。
viewDelegate.h中

#import <Foundation/Foundation.h>

@protocol viewDelegate <NSObject>
/*
 *@required标注的方法为必须实现的方法;
 *@optional标注的方法为可以选择实现;
 */
@optional

-(void)setViewControl;//定义setViewControl方法

@end

TableViewCell.h中

#import <UIKit/UIKit.h>
#import "viewDelegate.h"
@interface TableViewCell : UITableViewCell

//委托代理人
@property(nonatomic,weak)id<viewDelegate> Delegate;

@end

TableViewCell.m中

//自定义cell重写init方法
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        //添加所有子控件,这里也就是button
        [self setUpAllChildView];

    }
    return self;
}

-(void)setUpAllChildView
{
    //创建一个UIButton
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button1.frame = CGRectMake(0, 0, 100, 50);
    [button1 setTitle:@"Green" forState:UIControlStateNormal];
    [button1 setTitle:@"BB" forState:UIControlStateSelected];
    [button1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button1];   
}

-(void)buttonPressed:(UIButton *)button
{
    //实现按钮不同状态的切换
    button.selected=!button.selected;

    //通知执行协议方法
    [self.Delegate setViewControl];

}

TableViewController.m中

@interface TableViewController ()<viewDelegate>//这个一定要写,而且要记住导入头文件


@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 20;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * ID = @"cell";
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    if(!cell)
    {
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

        //调用委托方法
        cell.Delegate = self;
    }

    return cell;
}

//实现协议方法
-(void)setViewControl
{
    ViewController *vc = [[ViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}

运行代码,效果如下,成功解决了问题。
这里写图片描述这里写图片描述

 

转载于:https://www.cnblogs.com/ahtchxw/p/5466606.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值