`
luckfox
  • 浏览: 64441 次
  • 性别: Icon_minigender_1
  • 来自: 台灣
社区版块
存档分类
最新评论

protocol 和 delegate

 
阅读更多
目標:
延續範例http://luckfox.iteye.com/blog/1826126,加上protocol/delegate示範


//
//  UIStoryboardSubViewController.h
//  StoryboardDemo0
//
//  Created by Administrator on 13/3/7.
//  Copyright (c) 2013年 Administrator. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
//step 1. 告知protocol StoryboardSubDelegate的存在
@protocol StoryboardSubDelegate;					 

@interface UIStoryboardSubViewController : UIViewController


@property (strong) NSString *dataString;
@property (weak, nonatomic) IBOutlet UITextField *inputField;
//step 2.宣告一個id<StoryboardSubDelegate>的變數delegate,用來存放物件位址(也就是使用此protocol的Object位址),
//相當於callback中的回Call handle.
@property (weak) id<StoryboardSubDelegate> delegate;


- (IBAction)DoDone:(id)sender;
@end
//step 3.表明此delegate繼承自<NSObject>,如果繼承多個則<N1,N2,...>
@protocol StoryboardSubDelegate<NSObject>
//沒有加上@optional表示必須要實作,反之則可有可無
-(void)second:(UIStoryboardSubViewController *)theSecond inputString:(NSString *)inputString;
@end



//
//  UIStoryboardSubViewController.m
//  StoryboardDemo0
//
//  Created by Administrator on 13/3/7.
//  Copyright (c) 2013年 Administrator. All rights reserved.
//

#import "UIStoryboardSubViewController.h"

@interface UIStoryboardSubViewController ()

@end

@implementation UIStoryboardSubViewController
@synthesize inputField;
@synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
	inputField.text=self.dataString;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)DoDone:(id)sender
{
	//檢查protocol是否有被實作
	if([self.delegate respondsToSelector:@selector(second:inputString:)])
	{
		//呼叫此protocol的實作函數
		[self.delegate second:self inputString:inputField.text];
	}
	//關掉此ViewController
	[self dismissViewControllerAnimated:YES completion:
	 ^{
		 NSLog(@"done");
	 }];
}
@end

//
//  UIStoryboardDemoViewController.m
//  StoryboardDemo0
//
//  Created by Administrator on 13/3/7.
//  Copyright (c) 2013年 Administrator. All rights reserved.
//

#import "UIStoryboardDemoViewController.h"
#import "UIStoryboardSubViewController.h"
//step 1.protocol StoryboardSubDelegate使用
@interface UIStoryboardDemoViewController ()<StoryboardSubDelegate>

@end

@implementation UIStoryboardDemoViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//過場傳送端
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
	//將資料送到destinationViewController
	id secondCon =segue.destinationViewController;
	[secondCon setValue:self forKey:@"delegate"];
	[secondCon setValue:@"From Hello" forKey:@"dataString"];
}
//step 2.protocol 裡面的method的實作
-(void)second:(UIStoryboardSubViewController *)theSecond inputString:(NSString *)inputString
{
	NSLog(@"received %@",inputString);
}
@end
分享到:
评论

相关推荐

    协议与委托 (Protocol and Delegate) 实例解析demo

    正式协议是通过protocol指定的一系列方法的声明,然后由实现该协议的类自己去实现这些方法。而非正式协议是通过向NSObject中添加一个类别来实现,然后子类去继承NSObject。其实都差不多。 不过,非正式协议已经渐渐...

    UI界面之间的值传递-思路2-微变化

    使用kvo,protocol,delegate,NSnotifaction,SignLeton实现界面之间的传值

    UIApplicationDelegate_Protocol

    The UIApplicationDelegate protocol declares methods that are implemented by the delegate of the singleton UIApplication object. These methods provide you with information about key events in an ...

    iOS 中KVC、KVO、NSNotification、delegate 总结及区别

    iOS 中KVC、KVO、NSNotification、delegate 总结及区别  1、KVC,即是指 NSKeyValueCoding,一个非正式的Protocol,提供一种机制来间接访问对象的属性。而不是通过调用Setter、Getter方法访问。KVO 就是基于 KVC ...

    基于iOS的音乐播放器设计与实现

    数据库:coreData 客户端: 1. 三方库:AVPlayer播放,AFNetWorking下载,SDWebImage图片,Masonry...2. 基础知识:Objective-C语法,UI相关基本使用,Protocol,Delegate,KVO,Block 3. 其他操作:文件操作,内存管理

    ios代理协议的讲解

    ios中非常重要的delegate和protocol的通俗讲解,非常清晰易懂,看了之后,让你对代理和协议有不一样的认识。

    Object-C语言教程&案例,要点难点,代码示例,代码解析

    Objective-C教程要点 ...协议与代理:理解协议(Protocol)和代理(Delegate)设计模式在Objective-C中的应用。 块与闭包:学习如何使用块(Block)实现闭包(Closure)功能。 多线程与并发:了解GCD(Grand Central Dispa

    iOS代码规范_me.docx

    Delegate方法命名规范(讲述delegate方法的命名规范) Private方法命名规范(讲述私有方法的命名规范) Category命名规范(讲述分类的命名规范) Class命名规范(讲述类命名规范) Protocol命名规范(讲述协议...

    IOS开发中的设计模式汇总

    实例:tableview的 数据源delegate,通过和protocol的配合,完成委托诉求。 列表row个数delegate 自定义的delegate (二)观察者模式 应用场景:一般为model层对,controller和view进行的通知方式,不关心谁去接收,...

    破解Objective-C面试:笑到最后的技术攻略!.zip

    技术关键词:Objective-C、iOS开发、Mac OS X、编程语言、面向对象编程、内存管理、自动引用计数(ARC)、协议(protocol)、类扩展(category)、键值观察(KVO)、键值编码(KVC)、Block、Delegate模式、多态性、...

    DZAppDelegate:由App Delegate处理的常见任务抽象为更简单的子类模式

    目的:DZAppDelegate将一些常用任务抽象为Subclass-protocol样式的实现,从而使开始新项目更容易,更快捷。 配置 该协议定义了以下5种可选方法: - (NSDictionary *)appDefaults; - (void)setupAppearance; - ...

    分页效果设置

    @protocol ZSegmentedControlDelegate - (void)setSelectedIndex:(NSUInteger)selectedIndex; @end typedef NS_ENUM(NSInteger, MSegmentedControlIndicatorStyle){ MSegmentedControlIndicatorStyleDefault };...

    LCNetwork:基于AFNetworking的网络库封装

    LC网络基于AFNetworking的封装,参考了的实现方式,接口类采用@protocol约束,接口类的创建和使用更清晰。已适配AFNetworking 3.x若遇到Demo闪退问题,请删除APP重新运行,另外感谢提供免费的测试接口。功能支持...

    JQuery_1.4_API开发手册

    (Bug) Fixed an issue where host and protocol were not compared case-insensitively when determining whether anAJAXrequest was local or remote (#6908) (Bug) Fixed an issue where the “clone” variable...

    jQuery最新1.4.4精简版+1.4中文手册

    (Bug) Fixed an issue where host and protocol were not compared case-insensitively when determining whether anAJAXrequest was local or remote (#6908) (Bug) Fixed an issue where the “clone” variable ...

    【IOS一气呵成】之IAP集成:内购和内购恢复 DEMO

    @protocol RMIAPHelperDelegate //购买 -(void)requestProduct:(RMIAPHelper*)sender start:(SKProductsRequest*)request; -(void)requestProduct:(RMIAPHelper*)sender received:(SKProductsRequest*)request; -...

    能够支持无限滚动功能

    该源码是一个能够支持无限滚动功能,源码LTInfiniteScrollView,LTInfiniteScrollView能够无限滚动,可以设定视图出现的数量,总数量,...其中在delegate方法中,可以自己写不同的视图变换。 更详细的例子请参考Demo.

    http长轮询技术comet的实现

    通过comet实现了一个聊天功能。 1.需要在tomcat的server.xml里面配置 &lt;Connector port="8081" protocol="org.apache.coyote.http11.Http11NioProtocol" connectionTimeout="20000" ...&lt;Loader delegate="true"/&gt;

    自定义键盘

    @protocol BGKeyBoardDelegate @required - (void)finished; @optional - (void)calculatorInputView:(BGKeyBoard *)inputView didTapKey:(NSString *)key; - (void)calculatorInputViewDidTapBackspace:...

Global site tag (gtag.js) - Google Analytics