IOS之学习笔记十五(协议和委托的使用)

简介: IOS之学习笔记十五(协议和委托的使用)

1、协议和委托的使用

1)、协议可以看下我的这篇博客


IOS之学习笔记十四(协议的定义和实现) https://blog.csdn.net/u011068702/article/details/80963731


2)、委托可以叫代理,实现协议的类的对象可以叫委托对象或者代理对象


3)、关键就是我们在控制器里类(获取数据类)里面的成员变量需要是一个委托对象或者代理对象


4)、然后调用控制器里类(获取数据类)里面的方法的时候会调用委托对象里面定义的方法


2、测试app启动弹框提示

1)、control.h

#ifndef Control_h

#define Control_h

#import <Foundation/Foundation.h>

//协议定义

@protocol UpdateAlertDelegate

-(void)updateAlert;

@end

@interface Control : NSObject

//遵循协议的一个代理变量定义

@property (nonatomic, weak) id<UpdateAlertDelegate> delegate;

- (void) willShowAlert;

@end

#endif /* Control_h */


2)、control.m

#import <Foundation/Foundation.h>

#import "Control.h"

@implementation Control

- (void) willShowAlert

{

   [self.delegate updateAlert];

}

@end


3) 、我们在ViewController.h文件里面实现在control.h文件里面定义的协议

#import <UIKit/UIKit.h>

#import "Control.h"

@interface ViewController : UIViewController<UpdateAlertDelegate>

@end


4)、在ViewController.m文件里面实现协议的定义的方法,而且实例化对象设置自己为委托对象

#import "ViewController.h"

#import "Control.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

   [super viewDidLoad];

   Control *control = [Control new];

   control.delegate = self;

   [control willShowAlert];

}

- (IBAction)ui:(id)sender {

   NSLog(@"hello word");

}

- (void)didReceiveMemoryWarning {

   [super didReceiveMemoryWarning];

   // Dispose of any resources that can be recreated.

}

- (void)updateAlert

{

   UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"协议和代理" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];

 

   alert.alertViewStyle=UIAlertViewStyleDefault;

   [alert show];

}

@end


3、效果

20170724223902569.png


目录
打赏
0
0
0
0
2
分享
相关文章
调用Live555接收RTSP直播流,转换为Http Live Streaming(iOS直播)协议
调用Live555接收RTSP直播流,转换为Http Live Streaming(iOS直播)协议
578 1
IM通讯协议专题学习(九):手把手教你如何在iOS上从零使用Protobuf
接上篇《金蝶随手记团队的Protobuf应用实践(原理篇)》,本文将以iOS端的Objective-C代码为例,图文并茂地向您菔救绾卧趇OS工程中快速使用Protobuf,希望对你有帮助。
205 0
IM通讯协议专题学习(九):手把手教你如何在iOS上从零使用Protobuf

热门文章

最新文章

  • 1
    苹果app上架-ios上架苹果商店app store 之苹果支付In - App Purchase内购配置-优雅草卓伊凡
    40
  • 2
    苹果app上架app store 之苹果开发者账户在mac电脑上如何使用钥匙串访问-发行-APP发布证书ios_distribution.cer-优雅草卓伊凡
    41
  • 3
    uniapp云打包ios应用证书的获取方法,生成指南
    39
  • 4
    iOS|解决 setBrightness 调节屏幕亮度不生效的问题
    124
  • 5
    iOS|记一名 iOS 开发新手的前两次 App 审核经历
    29
  • 6
    iOS各个证书生成细节
    39
  • 7
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
    203
  • 8
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
    62
  • 9
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    82
  • 10
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    64