当前位置: 首页 > news >正文

网站备案中查询常州被约谈企业主公司发讣告

网站备案中查询,常州被约谈企业主公司发讣告,四个常见的网络营销方式,公众号开发算软件开发吗文章目录 一、 Delegate 模式的概念二、Delegate 的实现步骤步骤 1: 定义一个协议#xff08;Protocol#xff09;步骤 2: 在主类中添加一个 delegate 属性步骤 3: 实现协议的类遵守协议并实现方法步骤 4: 设置 delegate 三、Delegate 模式的特点四、Delegate 模式的常见场景… 文章目录 一、 Delegate 模式的概念二、Delegate 的实现步骤步骤 1: 定义一个协议Protocol步骤 2: 在主类中添加一个 delegate 属性步骤 3: 实现协议的类遵守协议并实现方法步骤 4: 设置 delegate 三、Delegate 模式的特点四、Delegate 模式的常见场景1. 视图控制器与视图组件之间的通信2. 处理用户输入事件3. 自定义控件的回调4. 事件的传递与回调5. 解耦设计6. 常见框架与委托 一、 Delegate 模式的概念 核心思想一个对象定义协议Protocol让其他对象遵守协议并实现相关方法最终将任务交给这些对象完成。使用场景实现对象之间的事件回调、数据传递 和 解耦。 二、Delegate 的实现步骤 步骤 1: 定义一个协议Protocol 协议中声明了需要实现的方法。 protocol MyDelegate NSObject - (void)taskDidComplete; // 定义协议方法 end步骤 2: 在主类中添加一个 delegate 属性 主类通过 weak 引用委托对象避免循环引用。 interface MyClass : NSObject property (nonatomic, weak) idMyDelegate delegate; // 弱引用 - (void)startTask; endimplementation MyClass - (void)startTask {NSLog(Task is starting...);// 模拟任务完成[self.delegate taskDidComplete]; // 调用委托对象的方法 } end步骤 3: 实现协议的类遵守协议并实现方法 其他类通过遵守 MyDelegate 协议来实现任务。 interface AnotherClass : NSObject MyDelegate endimplementation AnotherClass - (void)taskDidComplete {NSLog(Task completed! Delegate notified.); } end步骤 4: 设置 delegate 主类通过 delegate 属性与实现类建立连接。 int main(int argc, const char * argv[]) {autoreleasepool {MyClass *myClass [[MyClass alloc] init];AnotherClass *anotherClass [[AnotherClass alloc] init];myClass.delegate anotherClass; // 设置委托对象[myClass startTask]; // 触发任务}return 0; }输出 Task is starting... Task completed! Delegate notified.三、Delegate 模式的特点 解耦实现者委托对象和调用者主类分离降低耦合度。灵活性不同的对象可以遵守协议实现不同的逻辑。单向通信委托对象实现协议方法主类通过调用这些方法实现通信。 四、Delegate 模式的常见场景 1. 视图控制器与视图组件之间的通信 在 iOS 中视图组件如 UITableView、UITextField通过 Delegate 与控制器通信传递事件和数据。 示例UITableView 的 Delegate 和 DataSource UITableView 的事件例如单元格选中、滚动通过 UITableViewDelegate 和 UITableViewDataSource 协议回调到控制器中。 interface ViewController () UITableViewDelegate, UITableViewDataSource property (nonatomic, strong) UITableView *tableView; endimplementation ViewController - (void)viewDidLoad {[super viewDidLoad];self.tableView [[UITableView alloc] initWithFrame:self.view.bounds];self.tableView.delegate self;self.tableView.dataSource self;[self.view addSubview:self.tableView]; }// UITableViewDataSource 方法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return 10; // 返回行数 }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell *cell [tableView dequeueReusableCellWithIdentifier:cell];if (!cell) {cell [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell];}cell.textLabel.text [NSString stringWithFormat:Row %ld, (long)indexPath.row];return cell; }// UITableViewDelegate 方法 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {NSLog(Selected row: %ld, (long)indexPath.row); } end2. 处理用户输入事件 委托模式在输入控件如 UITextField 和 UITextView中广泛使用用于处理用户输入。 interface ViewController () UITextFieldDelegate property (nonatomic, strong) UITextField *textField; endimplementation ViewController - (void)viewDidLoad {[super viewDidLoad];self.textField [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 200, 40)];self.textField.borderStyle UITextBorderStyleRoundedRect;self.textField.delegate self;[self.view addSubview:self.textField]; }// UITextFieldDelegate 方法 - (BOOL)textFieldShouldReturn:(UITextField *)textField {[textField resignFirstResponder]; // 隐藏键盘return YES; } end3. 自定义控件的回调 在开发自定义控件时可以定义一个 delegate让外部类通常是视图控制器实现回调方法处理控件的行为。 示例自定义控件 protocol CustomViewDelegate NSObject - (void)customViewButtonWasTapped; endinterface CustomView : UIView property (nonatomic, weak) idCustomViewDelegate delegate; endimplementation CustomView - (instancetype)initWithFrame:(CGRect)frame {self [super initWithFrame:frame];if (self) {UIButton *button [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];[button setTitle:Tap Me forState:UIControlStateNormal];[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];[button addTarget:self action:selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];[self addSubview:button];}return self; }- (void)buttonTapped {[self.delegate customViewButtonWasTapped]; } end使用自定义控件 interface ViewController () CustomViewDelegate endimplementation ViewController - (void)viewDidLoad {[super viewDidLoad];CustomView *customView [[CustomView alloc] initWithFrame:self.view.bounds];customView.delegate self;[self.view addSubview:customView]; }// CustomViewDelegate 方法 - (void)customViewButtonWasTapped {NSLog(Button was tapped in custom view.); } end4. 事件的传递与回调 在对象之间需要传递事件或执行回调时Delegate 是一种常用方案。 例如网络请求完成后将结果返回给调用者。例如任务完成通知某个对象。 5. 解耦设计 Delegate 可以用于降低对象之间的耦合度。例如 UITableView 和其数据源分离控制器实现协议方法而 UITableView 并不知道具体实现。MVC 架构中Controller 与 View 通过 Delegate 通信解耦逻辑与界面代码。 6. 常见框架与委托 UITableViewDelegate / UITableViewDataSource表格视图事件与数据源回调。UICollectionViewDelegate / UICollectionViewDataSource集合视图的数据源与事件处理。UITextFieldDelegate文本输入控件的事件回调。NSURLSessionDelegate网络请求结果的回调。CLLocationManagerDelegate位置服务的事件回调。 最佳实践对于单一事件回调推荐使用 Block而对于复杂逻辑、多方法回调使用 Delegate 模式更合适。
http://www.hkea.cn/news/14573285/

相关文章:

  • 建设网站最简单的软件是wordpress 积分集成
  • 易语言做网站视频网站开发进度计划是什么
  • 如何在百度开个网站品牌网站建是啥
  • 网站功能模块设计怎么写驾考学时在哪个网站做
  • 淘宝客网站开发直播app源码
  • 成都旅游网站建设地址建设银行的网站是多少
  • 做一婚恋网站多少钱东莞动点网络科技有限公司
  • 数据库怎么做两个网站东莞新闻最新消息
  • 个人博客网站备案使用网站效果图
  • python 直播网站开发唐山做网站企业
  • 网站建设的七个步骤中英文网站是咋做的
  • 网站开发工程师岗位职责说明书如何下载字体到wordpress
  • 福州短视频seo网站一个静态网站多少钱
  • 两学一做专栏网站株洲网站建设优化
  • 做商城网站需要备案吗深圳信息职业技术学院
  • 龙岗南联网站建设十大黄金软件app免费
  • 网站举报12321网站程序 seo
  • 专门做顶账房的网站旅游网站建设可行性分析
  • 申报课题所需的网站怎么做小说推广赚钱
  • 网站手机源码深圳石岩网站建设
  • 旅游手机网站建设企业营销型网站建设价格
  • 门户网站流程图在线转格式网站怎么做
  • 郑州做网站托管装修十大风格
  • 谷歌网站地图生成三网合一网站建设计划
  • 外贸自建站收款通道使用net域名的大网站
  • 宜昌网站建设哪家好网站多国语言
  • 地方门户类网站怎么检查网站有没有被挂马
  • 怎么自建网站网站建设于朦胧
  • 19年做网站夜晚直播
  • 万网企业网站建设网页模板下载后怎么用