[IOS delegate and Protocols]

Source: Internet
Author: User

Reprint please indicate the source

http://blog.csdn.net/pony_maggie/article/details/25655443

Author: Pony

The syntax of the agent and the agreement do not repeat here, check the information yourself.

The idea of this demo is this, there is a class A, this class is not a view-based class, it inherits from NSObject, this class will start a timer, when the timer triggers, it will trigger a B view pops up an alert reminder. Because Class A cannot manipulate the B view directly, it operates with a delegate mechanism, a "delegate" B view.

Create a new View project named Delegatedemo, which is our B view, which is generated by default. Then create a new TimeControl class as our Class A.

A Class A header file first defines a protocol, the protocol that our agent will follow, and then there should be a public method to start the timer, the code is as follows:

#import <Foundation/Foundation.h>// Protocol definition @protocol updatealertdelegate <nsobject>-(void) Updatealert; @end @interface Timercontrol:nsobject // a proxy variable definition that follows a protocol ID Delegate ; -(void) startthetimer;    @end

Then we look at the Class A implementation file, very simple, start the timer, timer trigger to update the view through the proxy object:

@implementation Timercontrol-(void) startthetimer{    [nstimer scheduledtimerwithtimeinterval:  5.0f  target:self selector: @selector (TIMERPROC) Userinfo:nil repeats:no];} -(void) timerproc{    [self. delegate Updatealert]; // Agent Update UI }@end

Take a look at the view class, which first follows the protocol defined above in order to "Help" Class A to handle things, as follows:

#import <UIKit/UIKit.h>#import"TimerControl.h"@interface delegatedemoviewcontroller:uiviewcontroller<updatealertdelegate>@end

Obviously, the agreement here is like the role of an intermediary, without this intermediary, you cannot "accept the agent." Note that proxies and protocols are not always implemented together, but in most cases we use protocols to aid the implementation of proxies. The implementation file for the B view is also simple:

- (void) viewdidload{[Super Viewdidload]; //additional setup after loading the view, typically from a nib.Timercontrol *timer =[[Timercontrol alloc] init]; Timer.Delegate= self;//setting up proxy instances[Timer Startthetimer];//Start Timer, timer 5 trigger}- (void) didreceivememorywarning{[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}//The method by which the proxy object implements the protocol declaration, called by the proxy object- (void) updatealert{Uialertview*alert=[[uialertview Alloc] Initwithtitle:@"Tips"Message@"Time to" Delegate: Self Cancelbuttontitle:nil otherbuttontitles:@"Determine", nil]; Alert.alertviewstyle=Uialertviewstyledefault; [Alert show];}

Source:

Https://github.com/pony-maggie/DelegateDemo

[IOS delegate and Protocols]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.