ios 中代理原理使用方法精讲
2014-09-09 22:12
375 查看
IOS中delegate的使用方法
写了一个简单的委托的试用测试:
首先创建FunctionTest类,声明委托:
FunctionTest.h
[java] view plaincopyprint?
//
// FunctionTest.h
// DelegateDemo
//
// Created by shx on 12-7-17.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol FunctionTestDelegate;
@interface FunctionTest : NSObject
{
id<FunctionTestDelegate> delegate;
}
@property (nonatomic, assign)id<FunctionTestDelegate> delegate;
- (void)func1;
- (void)func2;
@end
@protocol FunctionTestDelegate <NSObject>
- (void)func3;
- (void)func4;
@end
FunctionTest.m
[java] view plaincopyprint?
//
// FunctionTest.m
// DelegateDemo
//
// Created by shx on 12-7-17.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "FunctionTest.h"
@implementation FunctionTest
@synthesize delegate;
- (void)func1
{
NSLog(@"function 1 called
");
[delegate performSelector:@selector(func3)];
}
- (void)func2
{
NSLog(@"function 2 called
");
[delegate performSelector:@selector(func4)];
}
@end
在appdelegate中实现委托:
[java] view plaincopyprint?
//
// AppDelegate.h
// DelegateDemo
//
// Created by shx on 12-7-17.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "FunctionTest.h"
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate,FunctionTestDelegate>
{
FunctionTest *test;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@end
AppDelegate.m中实现:
[java] view plaincopyprint?
#pragma mark -FunctionTestDelegate
- (void)func3
{
NSLog(@"function 3 called
ns");
}
- (void)func4
{
NSLog(@"function 4 called
ns");
}
在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
调用添加:
[java] view plaincopyprint?
test = [[FunctionTest alloc]init];
test.delegate = self;
[test func1];
[test func2];
相关文章推荐
- ios UIScrollView代理方法追踪 及使用
- 使用代理的方法 iOS
- 二十三 ,iOS UIAlertView使用block来实现代理方法;
- iOS端JSON转Model链式编程框架SuperKVC使用方法与原理
- ios UIScrollView代理方法追踪 及使用
- iosUIScrollView以及UIPageControl的基本使用以及所有代理方法
- iOS_使用NSURLSessionDataDelegate的代理方法下载数据
- iOS用WKWebView与JS交互获取系统图片及WKWebView的Alert,Confirm,TextInput的监听代理方法使用,屏蔽WebView的可选菜单
- 【iOS开发-110】MapKit框架的主要类MKMapView以及代理方法,大头针的使用addAnnotation
- iOS_使用NSURLSessionDataDelegate的代理方法下载数据
- iOS 网络开发NSURLConnection——使用代理方法发送异步请求
- IOS自定义NSLOG和断言的使用/description方法的使用原理(对象,数组,字典)
- iOS里面消除使用代理调用方法时间警告问题
- iOS UIScrollView的代理的使用及缩放时代理监听的几个方法
- iOS中的导航条UINavigationController(UISegmentedControl)的基本使用(界面中传值的3中方法,单例,属性,代理)
- iOS中UITableView使用总结和 iOS UITableView代理方法详解
- iOS之AFNetworking实现原理和使用方法
- iOS 最全ReactiveCocoa原理及使用方法
- iOS代理方法和block的使用
- iOS开发——网络篇——NSURLSession,下载、上传代理方法,利用NSURLSession断点下载,AFN基本使用,网络检测,NSURLConnection补充