zoukankan      html  css  js  c++  java
  • IOS开发——Protocol使用协议

    protocol ['prəutəkɔl] (样例:http://blog.sina.com.cn/s/blog_6aafe9c90100yozz.html )
    一、说明
     
    两个类进行通讯,用协议就比較方便。 

    1.协议声明了能够被不论什么类实现的方法 
    2.协议不是类,它是定义了一个其它对象能够实现的接口 
    3.假设在某个类中实现了协议中的某个方法,也就是这个类实现了那个协议。 
    4.协议经经常使用来实现托付对象。一个托付对象是一种用来协同或者代表其它对象的特殊对象。 
    5:托付,就是调用自定义方法,别的类来实现。 
    6.新特性说明 
    @optional预编译指令:表示能够选择实现的方法 
    @required预编译指令:表示必须强制实现的方法 


    二、定义 
    .h 
    @protocol ContactCtrlDelegate 
    -(void)DismissContactsCtrl; 
    @end 
    
    
    @interface ContactsCtrl : UIViewController { 
        id <ContactCtrlDelegate> delegate; 
    } 
    @property (nonatomic, assign) id <ContactCtrlDelegate> delegate; 
    .m 
    @synthesize delegate; 

    三、样例 

    比如:UITextView 

    @protocol UITextViewDelegate <NSObject> 
    @optional 
    - (BOOL)textViewShouldBeginEditing:(UITextView *)textView; 
    - (BOOL)textViewShouldEndEditing:(UITextView *)textView; 
    - (void)textViewDidBeginEditing:(UITextView *)textView; 
    - (void)textViewDidEndEditing:(UITextView *)textView; 
    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text; 
    - (void)textViewDidChange:(UITextView *)textView; 
    - (void)textViewDidChangeSelection:(UITextView *)textView; 
    @end
    假设要调用以上这些方法。就必须设置UITextView的托付:TextView.delegate = self; 


    四、Demo 
    1、ContactsCtrl.h 
    #import <UIKit/UIKit.h> 
    
    //定义协议 
    @protocol ContactCtrlDelegate 
    -(void)DismissContactsCtrl; 
    @end 
    
    @interface ContactsCtrl : UIViewController { 
        IBOutlet UINavigationBar *ContactNavBar; 
        id <ContactCtrlDelegate> delegate; 
    } 
    @property (nonatomic, assign) id <ContactCtrlDelegate> delegate; 
    -(IBAction)canCelBtn:(id)sender; 
    @end 
    2、ContactsCtrl.m 
    @implementation ContactsCtrl 
    @synthesize delegate; 
    
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
    - (void)viewDidLoad { 
        [super viewDidLoad]; 
        ContactNavBar.topItem.prompt = @"选取联系人发送短信"; 
    } 
    
    //调用协议中的方法 
    -(IBAction)canCelBtn:(id)sender{ 
        [delegate DismissContactsCtrl]; 
    } 

    3、ProtocolDemoCtrl.h 

    #import <UIKit/UIKit.h> 
    #import "ContactsCtrl.h" 
    @interface ProtocolDemoCtrl : UIViewController <ContactCtrlDelegate>{//加入托付 
        ContactsCtrl *contactsView; 
    } 

    4、ProtocolDemoCtrl.m 

    #import "ProtocolDemoCtrl.h" 
    #define BARBUTTONADD(SELECTOR) [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:SELECTOR] autorelease]; 
    
    @implementation ProtocolDemoCtrl 
    @synthesize contactsView; 
    
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
    - (void)viewDidLoad { 
        [super viewDidLoad]; 
        self.navigationItem.rightBarButtonItem = BARBUTTONADD(@selector(addContactAction:)); 
    } 
    
    - (void)addContactAction:(id)sender{ 
        ContactsCtrl *contactView = [[ContactsCtrl alloc] initWithNibName:@"ContactsCtrl" bundle:nil]; 
        self.contactsView = contactView; 
        contactsView.delegate = self;//设置托付 
        [self presentModalViewController:contactsView animated:YES]; 
        [contactView release];     
    } 
    
    //实现ContactCtrlDelegate协议中的方法 
    -(void)DismissContactsCtrl{ 
        [contactsView dismissModalViewControllerAnimated:YES]; 
    } 

    综上,假设A类.h文件定义了一个协议,在A类里面我们还须要定义一个托付delegate,协议里面有个必须实现的methodA方法(@required,这种方法在实现了协议的类里面实现),在A类的.m文件里须要调用这个methodA方法,我们使用托付调用这个methodA方法

    [delegate DismissContactsCtrl]; 
    类B实现了这个协议,在.h文件里定义类A的对象a,在.m文件里须要实现methodA方法。而且须要设置托付

    a.delegate = self;//设置托付

    假设MethodA返回的是B类的对象,那么在A类中就能够调用B类属性和方法国,B这个类可以调用A的属性和方法。


    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    [LeetCode]题解(python):119-Pascal's Triangle II
    [LeetCode]题解(python):118-Pascal's Triangle
    [LeetCode]题解(python):117-Populating Next Right Pointers in Each Node II
    寒假自学进度8
    寒假自学进度7
    寒假自学进度6
    寒假自学5
    寒假自学学习4
    寒假自学进度3
    寒假自学进度2
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4657138.html
Copyright © 2011-2022 走看看