IOS delegate, proxies/delegates and protocols.

Source: Internet
Author: User

Before I knew how the iOS protocol was written, I thought it was really like the Java interface, and then I found it was totally not.

First of all, the application scenario is when you want to use a program class, or logic class, to control a storyboard inside the label, found if directly with

Uistoryboard *mainstoryboard = [Uistoryboard storyboardwithname:@ "Main" Bundle:[nsbundle MainBundle]];
by storyboard, according to MyView's Storyboardid, to get the view we want to switch.
mainview= [Mainstoryboard instantiateviewcontrollerwithidentifier:@ "MyView"];

Then went to get this Mainview label property, the results found that this viewcontroller can be obtained, but the label is nil.

The results were searched and found that the logical class was unable to access the View Control class (the real problem is no one told me, after I asked so many people, and it's not a FU cking unsual issue, correct?).

Well, at this time, it is said that one of the solutions is this delegate/protocal.

First, the logical class is called logic, and the view controller class is still Viewcontroller, in Logic.h:


#import <Foundation/Foundation.h>

@protocol logicdelegate <NSObject>
-(void) dosomethingotherscant;
@end

@interface Logic:nsobject
@property (nonatomic, assign) ID <LogicDelegate> delegate;
-(void) changetext;
@end

is divided into two parts, one is the definition protocol, the protocol name is usually the class name +delegate, in this case is logicdelegate, this part with the Java interface a meaning, define the abstract method, this example is dosomthingotherscant.

The other part is defining a property, that is, who implemented the Logicdelegate interface/protocol.

One more way is to use proxy methods for logical classes.

So, LOGIC.M inside:

#import "Logic.h"
#import "ViewController.h"

@interface Logic ()
{
}

@property (strong,nonatomic) Viewcontroller *viewcontroller;
@end
@implementation Logic

-(void) changetext{
NSLog (@ "xxxx");
[Self.delegate dosomethingotherscant];
}

@end

Specifically, changetext in the logic needs, to let the agent, such as the view controller, to dosomething it can not do.

OK, to the View Controller class, first specify this class to implement the Logicdelegate protocol.

#import <UIKit/UIKit.h>
#import "Logic.h"

@interface viewcontroller:uiviewcontroller<LogicDelegate>//Here
@property (Weak, nonatomic) Iboutlet UILabel *label;
@end

Implementation class:

#import "ViewController.h"
#import "Logic.h"
@interface Viewcontroller ()
@property Logic *logic;
@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];
Self.logic=[[logic Alloc]init];
Self.logic.delegate = self;
Additional setup after loading the view, typically from a nib.
}
-(Ibaction) pressed: (ID) Sender {
[Self.logic Changetext];
}

-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}

-(void) dosomethingotherscant
{
NSLog (@ "Dododododo");
[Email protected] "DDDDDDD";
}

@end

First, the delegate attribute of the Viewcontroller is passed to the instance of logic at the time of Viewdidload.

Self.logic.delegate = self;

Then define the method that the Protocol requires to implement, dosomethingotherscant, the content is to change the value of the label.

In simple terms, the Logical class definition Protocol, the Protocol has an abstract method, Uiviewcontroller class, do the agent, specifically define the implementation of the abstract class, at the same time to Viewdidload, the logical property of the delegate to the self in.

Practice more, it's the only way.




IOS delegate, proxies/delegates 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.