About iOS development-Delegation

Source: Internet
Author: User

About iOS development-Delegation
In fact, delegation is not available in OC, but also in C #. The methods of understanding each other are different. In OC, delegation is a type of protocol and requires @ protocol declaration, in iOS development, delegation generally uses a large number of values in the page. Delegation is one of the simplest and most flexible modes in Cocoa. Delegation literally means entrusting what is needed to others, for business scenarios, refer to the page relationship between the main view and the sub-view, or the interaction between the view layer and the data layer. A simple delegate can be declared by @ protocol to define a method and reference the delegate object. The methods must be implemented. The methods are @ required by default, and can be set as optional @ optional, first, define a delegate: otocol BookDelegate <NSObject> @ required-(void) getBookCount; @ optional-(void) optionMethod; @ end defines the Book class and Customer class at this time: @ interface Book: NSObject <BookDelegate> @ end @ interface Customer: NSObject <BookDelegate> @ property (assign, nonatomic) id <BookDelegate> didBookDelegate; @ end implements the getBookCount method: @ imple Mentation Book-(void) getBookCount {NSLog (@ "implementation of getBookCount in Book");} @ end @ implementation Customer-(void) getBookCount {NSLog (@ "Implementation of getBookCount in Customer");} @ end simple call: Book * book = [[Book alloc] init]; customer * customer = [[Customer alloc] init]; [customer getBookCount]; [book getBookCount, next, let's look at the following code. At this time, we found the didBookDelegate: customer. didBookDelegate = book; [customer. di DBookDelegate getBookCount]; The above describes how to implement BookDelegate for the Book instance. In this case, the Book instance can be assigned a value to the variable in the customer, and its instantiated object can be delegated to didBookDelegate. The above is a basic scenario for delegation. As an instantiated object book, you can execute its method on your own or transfer the execution process through delegation. Page value transfer is simple: page A data can be passed to page B, page B can be passed to page A, simple two pages to pass the value, the page reference is as follows: all are text boxes and buttons. When the jump mode is selected, Modal: the definition of ViewController on the first page: # import <UIKit/UIKit. h> # import "SecondViewController. h "@ interface ViewController: UIViewController <StudySubjectDelegate> @ property (strong, nonatomic) IBOutlet NSString * firstData; @ property (weak, nonatomic) IBOutlet UITextField * subjectName; @ end the second page defines the SecondViewController header file and declares a delegate ://// SecondViewController. h // Sample /// Created by keso on 15/2/3. // Copyright (c) 2015 keso. all rights reserved. // # import <UIKit/UIKit. h> @ class SecondViewController; @ protocol StudySubjectDelegate <NSObject>-(void) shouldChangeValue :( SecondViewController *) controller; @ end @ interface SecondViewController: UIViewController @ property (assign, nonatomic) id <StudySubjectDelegate> firstViewDelegate ;@ Property (weak, nonatomic) IBOutlet NSString * showData; @ property (weak, nonatomic) IBOutlet UITextField * studySubject; @ end ViewController. click Event in m:-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {if ([segue. identifier ispolictostring: @ "firstEdit"]) {SecondViewController * controller = segue. destinationViewController; NSLog (@ "% @", self. subjectName. text); // pass your own instance to the second view contr Oller. firstViewDelegate = self; controller. showData = self. subjectName. text ;}} can be assigned in this way. In fact, the key is the attribute defined in the second view: if ([controller respondsToSelector: @ selector (setShowData :)]) {[controller setValue: self. subjectName. text forKey: @ "showData"];} click events on the second page. The Code is as follows: 1 [self. firstViewDelegate shouldChangeValue: self]; in the above section, we can see that the delegate in oc is to hand over your instance to the member variables of other objects, and then the member variables are used to execute instance work, today, I don't know why it's a headache. It's just that the last second page jumps to a page. You cannot assign values to UITextField because you have not figured out how to assign values to UITextField. Every time you enter the field, it becomes null. If you have any idea, please kindly advise. Thank you ~

Related Article

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.