Protocol for iOS development-pass value through proxy

Source: Internet
Author: User

At the beginning of iOS development, the understanding of protocol and delegate has always been dizzy, and I don't know how to transmit values between two uiviewcontrollers.

After interviewing several shoes and asking how to use delegate to pass values to two uiviewcontrollers, the answer is ambiguous. Today I have some children's shoes asking me this question, so I just need to write a blog to explain it.

1. protocol (protocol): After using this protocol, you must handle things according to the content specified in the protocol. The methods required in the protocol must be implemented (except for the @ optional method ).

Protocol is a syntax that provides a very convenient opportunity to implement the delegate mode.

Define protocol


@ Protocol ClassBDelegate <NSObject>

-(Void) methodOne;
@ Optional
-(Void) methodTwo :( NSString *) value;

@ End

Defines a ClassB protocol, which contains two methods, where methodTwo is optional.

Implement this protocol in the header file (ClassA. h) of ClassA,

@ Interface ClassA <ClassBDelegate>

@ End

Implement two ClassBDelegate methods in the implementation file (ClassA. m) of ClassA, where methodTwo can not implement


-(Void) methodOne {
// Specific implementation content

}

-(Void) methodTwo :( NSString *) value {
// Specific implementation content
}

2. As the name suggests, a proxy is to entrust others to handle affairs. When one thing happens, you will not handle it yourself and let others handle it.

Delegate and protocol are irrelevant. Delegate is a design pattern. Is to let another class (or itself) complete part of the thing that one class needs to do.

Define a proxy in the header file (ClassB. h) of ClassB

@ Interface ClassB

@ Property (nonatomic, unsafe_unretained) id <ClassBDelegate> delegate;

@ End

In this way, when we encounter problems that we want other classes (such as ClassA) to handle in the implementation file (ClassB. m) of ClassB, we can do this.

[Self. delegate methodOne];

[Self. delegate methodTwo: @ "value to be passed"]; 3. The preceding section briefly describes the protocol and proxy. Now we will explain how to transfer values between two uiviewcontrollers.

Start Xcode and select file-New Project... from the menu ...,

 

 

Select Single View Application and click Next,

 

A. Name the project DelegateByValue;

B. The name of the organization below is the name of the organization where you are located. Here, the name is my personal name;

C. The Company logo is divided into two parts: com, that is, Company. iostour writes the name of the Company where he is located. Here I write iostour of the iOS journey;

D. Class prefix. This can be casual, but I wrote W here. Because my name is Wei, I should take the first letter, so that my colleagues will know that I wrote this class;

E. Select iPhone as the device;

F. We use the xib method. Therefore, this option is not checked;

G. Select the ARC mechanism;

H. This case is not subject to unit tests and is not checked.

Click Next,

 

Select a directory for the Storage Project, and click Create to Create a project.

 

Next, we need to transmit values between two controllers. Since a WViewController is automatically generated for us during project creation, we only need to create one Controller.

Right-click DelegateByValue and click New File...

 

 

 

Choose iOS> Cocoa Touch> Objective-C Class, and then click Next,

 

Then, name it WTwoViewController, click Next, select the storage directory, and Create.

Next, create a. xib file for WTowViewController. The steps are as follows:

A. Right-click DelegateByValue and click New File...

    

B. The following window is displayed:

    

This time, we select iOS> User Interface> View, click Next, name it WTwoViewController, and Create.

After WTwoViewController is created, associate WTwoViewController. xib with WTwoViewController. h,

 

Follow the steps 1, 2, and 3, select 1, click 2, set 3 Class to WTwoViewController, and press Enter,


Click 4. Hold down the hollow circle behind the 5 view and drag it to the 6 view.

Next, drag a UITextField and a UIButton to the view, name UITextField txtValue, set an Action for UIButton, and name it pressChange.

The procedure is as follows:

1. xib and code are displayed on the split screen,

2. Select UITextField, press and hold the control key, and drag it to the Code (WTwoViewController. h) area on the right. The following window is displayed:

Set the name for it, and then click Connect;

3. Similarly, select UIButton, press and hold the control key, and drag it to the right code (WTwoViewController. h) area. The following window is displayed:

This time, because we want to set a click event for UIButton, change 1 Connection to Action, set the name, and click Connect.

 

Now the WTwoViewController window view is set.

Similarly, set the WViewController. xib window.

Drag a UILabel and a UIButton to the view, name UILabel lblValue, and set an Action for UIButton, named pressCasting.
 

 

Next, define a protocol in WTwoViewController. h, WTwoViewControllerDelegate, and define a proxy for WTwoViewController. The Code is as follows:


//
// WTwoViewController. h
// DelegateByValue
//
// Created by wzrng on 13-7-20.
// Copyright (c) July 15, 2013 wzrng. All rights reserved.
//

# Import <UIKit/UIKit. h>

@ Protocol WTwoViewControllerDelegate <NSObject>

-(Void) changeValue :( NSString *) value;

@ End

@ Interface WTwoViewController: UIViewController

@ Property (nonatomic, unsafe_unretained) id <WTwoViewControllerDelegate> delegate;

@ Property (nonatomic, strong) IBOutlet UITextField * txtValue;

-(IBAction) pressChange :( id) sender;

@ End

Next, in the-(IBAction) pressChange :( id) sender method in WTwoViewController. m, dispatch the proxy and destroy the window. The Code is as follows:

-(IBAction) pressChange :( id) sender {
[Self. delegate changeValue: self.txt Value. text];
[Self dismissViewControllerAnimated: YES completion: nil];
}

The settings in WTwoViewController have been completed. Next, you need to call WTwoViewController in WViewController and implement the WTwoViewControllerDelegate code.

First, implement the modern logic in WViewController. h. The Code is as follows:


//
// WViewController. h
// DelegateByValue
//
// Created by wzrng on 13-7-20.
// Copyright (c) July 15, 2013 wzrng. All rights reserved.
//

# Import <UIKit/UIKit. h>
# Import "WTwoViewController. h"

@ Interface WViewController: UIViewController <WTwoViewControllerDelegate>

@ Property (strong, nonatomic) IBOutlet UILabel * lblValue;

-(IBAction) pressCasting :( id) sender;

@ End

It calls WTwoViewController in the-(IBAction) pressCasting :( id) sender method of WViewController. The Code is as follows:


-(IBAction) pressCasting :( id) sender {
WTwoViewController * controller = [[WTwoViewController alloc] initWithNibName: @ "WTwoViewController" bundle: nil];
Controller. delegate = self;

[Self presentViewController: controller animated: YES completion: nil];
}

-(Void) changeValue :( NSString *) value {
// Change the value of UILabel
Self. lblValue. text = value;
}

OK.

Here, only the value transfer from WTwoViewController to WViewController is implemented. You can upload the value from WViewController to WTwoViewController by yourself.

Project source code portal DelegateByValue

 
 

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.