"iOS Dev 26" uses protocol proxies to implement forward and reverse values between navigation controllers Uinavigationcontroller views

Source: Internet
Author: User

Experiment Description

(1) Positive value: For example, a class value to be passed to Class B, we first declare a Class B object (of course, B class header file to import), and then the class A value to a value in class B (so you need to prepare a variable in class B to accept, Just use @property and @synthesize the entire variable).


(2) Reverse value: For example, you need to pass the value in class B to class A. We first write a protocol in class B, the protocol has a method that can take parameters, this parameter is the value we want to pass (this protocol can also be written separately, not necessarily written in Class B), and then Class B follows this protocol, using this Protocol to create a delegate variable, and then assign the delegate variable to Class A, Then implement the method inside the protocol in Class A.


(3) Of course, the highlight is:

    • Class B, although adhering to the Protocol but does not implement the method in the protocol, just to pass a value to the method, and then let class A to implement this method, of course, at the same time a class will accept the class B to this method passed the value, and this value is what we need to pass the value, so it is worth passing.
    • Use the delegate variable skillfully, first in class B with this delegate variable to the method passed a value, and then in Class A to give this delegate variable to class A, so a class in the implementation of the Protocol method is actually obtained before the class B using a delegate variable passed the value.

References

(1) about why there are sec1.delegate1=self in viewcontroller.m, if not understand the reference xxxx.delagate=self;


(2) about the delegate variable, if you do not understand can refer to the OBJECTIVE-C protocol Introduction + The implementation of the delegate and the Protocol and delegation in iOS


(3) for the entire value of the principal-agent mode, if you do not understand, you can refer to the iOS Development protocol: Proxy Transfer value


Experimental purpose

(1) To achieve the reverse value: from Viewcontroller click into Secondviewcontroller (there are three buttons), then click any button, return to Viewcontroller and the title of the button to change the title on the navigation bar. And then


(2) To achieve the positive value: we then click into the Secondviewcontroller and the navigation bar title of the same button's text color to red (that is, the previous three buttons were clicked).


So what we need is a navigation controller, and inside there are two view controllers Viewcontroller and Secondviewcontroller,viewcontroller is the root view controller. Of course we need to initialize the navigation controller in the APPDELEGATE.M and set the Viewcontroller as the root view controller and set the Self.window.rootViewController as the navigation controller so that the navigation controller can be displayed. This part of the code is omitted here. We mainly look at the code inside the two view controller.


Experiment Code

In the SecondViewController.h:

#import <uikit/uikit.h>//Create a protocol that passes the value of the Protocol @protocol Sendvalue <nsobject>-(void) Sendbtntitle: (NSString *) Btntitle, @end//Let this secondviewcontroller class comply with this protocol @interface secondviewcontroller:uiviewcontroller<sendvalue>/ /Use this protocol to create a delegate variable that the class object can use to invoke the method in the protocol, similar to an attribute, as in this example SECONDVIEWCONTROLLER.M [self.delegate1 Sendbtntitle:text1] ,//The delegate variable is set to which class, it is equivalent to which class becomes the proxy, because people have entrusted to you, then you can not proxy it? As in this example VIEWCONTROLLER.M Sec1.delegate1=self@property (nonatomic,assign) ID <sendValue> delegate1; @property ( nonatomic,assign) NSString *titletext; @end

In the SECONDVIEWCONTROLLER.M:

#import "SecondViewController.h" @interface Secondviewcontroller () @end @implementation Secondviewcontroller@synthesize delegate1; @synthesize titletext;-(void) Viewdidload {//use for to create three buttons and get a title separately, Add a click event, and in this case to determine if the Viewcontroller page passed the TitleText value and which button's title value consistent, then the title of the button will become red for (int i=1; i<=3; i++)        {UIButton *btn=[[uibutton alloc]init];        Btn.frame=cgrectmake (60+40*i, 300, 30);        Btn.backgroundcolor=[uicolor Purplecolor];        [BTN settitle:[nsstring stringwithformat:@ "button%i", I] forstate:uicontrolstatenormal];        if ([TitleText IsEqualToString:btn.currentTitle]) {btn.selected=yes;        } [btn Settitlecolor:[uicolor Redcolor] forstate:uicontrolstateselected];        [Btn addtarget:self Action: @selector (Backto:) forcontrolevents:uicontroleventtouchupinside];    [Self.view ADDSUBVIEW:BTN];    } [Super Viewdidload]; Do any additional setup after loading the view.} Because we need to get the title of the current button, we need to pass the button object, adding a parameter-(void) Backto: (UIButton *) BTN {//Use the Currenttitle property to get the title NSString *text1=btn.currenttitle of the current button; Using the delegate variable to send a message to the method inside the protocol, let it execute, but this execution we are in viewcontroller.m to implement, here is actually equivalent to notice method to start the feeling [self.delegate1 sendbtntitle:    Text1]; [Self.navigationcontroller Popviewcontrolleranimated:no];} @end

In the ViewController.h:

#import <UIKit/UIKit.h> #import "SecondViewController.h"//Follow <sendValue> agreement @interface Viewcontroller: Uiviewcontroller<sendvalue> @end

In the VIEWCONTROLLER.M:

#import "ViewController.h" #import "SecondViewController.h" @interface Viewcontroller () @end @implementation viewcontroller-(void) Viewdidload {//Add a button that clicks into the Secondviewcontroller view to UIButton *btn1=[uibutton Buttonwithtype:    Uibuttontyperoundedrect];    Btn1.frame=cgrectmake (38, 80, 300, 30);    Btn1.backgroundcolor=[uicolor Whitecolor];    [Self.view ADDSUBVIEW:BTN1];    [Btn1 settitle:@ "Jump to Secondviewcontroller" forstate:uicontrolstatenormal];    [Btn1 addtarget:self Action: @selector (Jumpto:) forcontrolevents:uicontroleventtouchupinside];    [Super Viewdidload]; Do any additional setup after loading the view, typically from a nib.} -(void) Jumpto: (NSString *) bartitle{//Initialize an object first, because the following is going to give the delegate variable in this object to our current class Secondviewcontroller *sec1=[[    Secondviewcontroller Alloc]init]; This is the key, you need to give the previous delegate variable to the current class (ie, Viewcontroller view Controller), since the acceptance of the delegate becomes the agent, then the protocol is to implement the method, because this method is not implemented in the Secondviewcontroller,    It found Viewcontroller do agent help it to achieve, in fact, is to use this method to pass value, of course, this method needs to have parameters, otherwise pass a fart value sec1.delegate1=self; The title of the current navigation barA variable passed to Secondviewcontroller is a positive value sec1.titletext=self.navigationitem.title; [Self.navigationcontroller pushviewcontroller:sec1 animated:no];} Reverse value: Assigns the value passed by the protocol method to our current navigation bar title-(void) Sendbtntitle: (NSString *) btntitle{Self.navigationitem.title=btntitle;} @end


A screenshot of the experimental results


"iOS Dev 26" uses protocol proxies to implement forward and reverse values between navigation controllers Uinavigationcontroller views

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.