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) after using this protocol, you must handle things according to the content specified in the protocol, except for the @ optional method required in the protocol ).
Protocol is a syntax that provides a very convenient opportunity to implement the delegate mode.
Define protocol as follows:
- @protocol ClassBDelegate<NSObject>
- - (void)methodOne;
- @optional
- - (void)methodTwo:(NSString *)value;
- @end
Defines a ClassB protocol, which contains two methods, where methodTwo is optional.
The following code implements this protocol in the header file ClassA. h) of ClassA:
- @interface ClassA<ClassBDelegate>
- @end
In ClassA implementation file ClassA. m), implement two ClassBDelegate methods, where methodTwo can not be implemented, as shown below:
- -(Void) methodOne {
- // Specific implementation content
- }
-
- -(Void) methodTwo :( NSString *) value {
- // Specific implementation content
- }
2. Proxy delegate), as its name implies, is to entrust others to handle affairs. When one thing happens, they will not handle it themselves and let others handle it.
Delegate and protocol are irrelevant. Delegate is a design pattern. Is part of what a class needs to do, so that another class can be itself.
Define a proxy in the ClassB header file ClassB. h) as follows:
- @interface ClassB
- @property (nonatomic, unsafe_unretained) id<ClassBDelegate> delegate;
- @end
In this way, when we encounter a problem that we want other classes such as ClassA to handle in the implementation file ClassB. m) of ClassB, we can
- [Self. delegate methodOne];
- [Self. delegate methodTwo: @ "value to be passed"];
3. I briefly introduced the protocol and proxy. Now I will explain how to transfer values between two uiviewcontrollers.
Start Xcode and select file-New Project... from the menu. The following window is displayed:
Select Single View Application and click Next. The following window is displayed:
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 to bring up the following window:
Select a directory for the Storage Project and click Create to Create a project. The directory structure after the project is created is as follows:
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... as follows:
The following window is displayed:
Select iOS> Cocoa Touch> Objective-C Class and click Next. The following window is displayed:
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, for example:
Follow the steps 1, 2, and 3, select 1, click 2, set 3 Class to WTwoViewController, and press enter, for example:
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 and hold down the control key and drag it to the WTwoViewController. h code on the right. The following window is displayed:
Set the name for it, and then click Connect;
3. Similarly, select UIButton and hold down the control key and drag it to the WTwoViewController. h code on the right. 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.
Now, all settings in the window are complete.
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.txtValue.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