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

先说说我遇到的问题吧,我自定义了一个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];
}

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
回答: 在iOS开发中,可以使用Block和delegate实现一对多的回调。使用delegate时,首先需要定义一个delegate协议,其中包含回调方法。然后在需要进行回调的地方,设置delegate并调用回调方法。具体实现可以参考以下代码示例:\[2\] ``` // 定义delegate协议 protocol FirstCellDelegate: class { func firstCellBtnTap(_ cell: firstTableViewCell) } // 在需要进行回调的地方设置delegate并调用回调方法 weak var delegate: FirstCellDelegate? // 调用回调方法 if let delegate = self.delegate { delegate.firstCellBtnTap(self) } // 实现delegate回调方法 func firstCellBtnTap(_ cell: firstTableViewCell) { let cellIndexPath = tableView.indexPath(for: cell) print("delegate回调 section:\(cellIndexPath?.section ?? 0), row:\(cellIndexPath?.row ?? 0)") } ``` 另外,使用Block也可以实现一对多的回调。Block可以作为函数表达式传递给API,可以选择性地存储,并且可以被多个线程使用。Block不仅包含了在回调时需要执行的代码,还包含了执行期间所需的数据。具体实现可以参考以下代码示例:\[3\] ``` // 定义Block回调 typealias CallbackBlock = () -> Void // 在需要进行回调的地方使用Block进行回调 var callback: CallbackBlock? // 调用回调 callback?() ``` 通过使用Block和delegate,可以实现一对多的回调,满足不同场景下的需求。 #### 引用[.reference_title] - *1* [Block实现iOS回调](https://blog.csdn.net/feelinghappy/article/details/119870367)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [iOS Block与Delegate的用法,各自优缺点及使用场景](https://blog.csdn.net/huangshanfeng/article/details/82106580)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值