[Transfer of]ios data

Source: Internet
Author: User
Tags vars uikit

Scenario 1:a-->b need to pass data to B.

Code:

    • Jump--Execute login2contacts this segue
    • [self performseguewithidentifier:@ "login2contacts" sender: nil];
    • When the Performseguewithidentifier jump is performed, the Prepareforsegue method is called, and Viewcontroller is taken in Prepareforsegue to pass the data
    • /**
    • * After executing segue, this method is called before jumping
    • * Typically here to pass data to the next controller
    • */
    • -(void) Prepareforsegue: (uistoryboardsegue *) Segue sender: (ID) sender
    • {
    • //1. Get the target controller (contact list Controller)
    • Uiviewcontroller *CONTACTVC = Segue. Destinationviewcontroller;
    • //2. Set the title
    • CONTACTVC. title = [NSString stringwithformat:"%@ contact list", self . Accountfield. Text];
    • //Contactvc.title equivalent to ContactVc.navigationItem.title
    • ContactVc.navigationItem.title = [NSString stringwithformat:@ "%@ contact list", Self.accountField.text];
    • }

Scenario 2 B->a When entering B, the B operation is completed to return to a, need to bring back the data to a
Idea, to let B pass to a first create a proxy object, that is, a new protocol, a to implement this Protocol, that a is equivalent to a proxy, and then a proxy into B, B to invoke the protocol in a method
Code:

    • #import <UIKit/UIKit.h>
    • @class Mjaddviewcontroller, Mjcontact;
    • @protocol mjaddviewcontrollerdelegate <NSObject>
    • @optional
    • -(void) Addviewcontroller: (Mjaddviewcontroller *) ADDVC didaddcontactwithname: (NSString *) name Phone: (NSString *) Phone
    • -(void) Addviewcontroller: (mjaddviewcontroller *) ADDVC didaddcontact: (mjcontact *) Contact;
    • @end
    • @interface Mjaddviewcontroller:uiviewcontroller
    • @property (nonatomic, weak) id<mjaddviewcontrollerdelegate> delegate;
    • @end
    • /**
    • * Add
    • */
    • -(ibaction) Add {
    • //1. Close the current controller
    • [self. Navigationcontroller popviewcontrolleranimated:YES];
    • //2. Pass data to previous controller (Mjcontactsviewcontroller)
    • //2. Notification Agent
    • if ([self. Delegate Respondstoselector:@selector (addviewcontroller:didaddcontact:)]) {
    • mjcontact *contact = [[Mjcontact alloc] init];
    • Contact. Name = self. NameField. Text;
    • Contact. Phone = self. Phonefield. Text;
    • [Self-delegate Addviewcontroller:self didaddcontact:contact];
    • }
    • }
    • A file
    • @interface Mjcontactsviewcontroller () <MJAddViewControllerDelegate>
    • /**
    • * Called before the jump is executed
    • * In this method, the view of the target controller has not been created
    • */
    • -(void) Prepareforsegue: (uistoryboardsegue *) Segue sender: (ID) sender
    • {
    • ID VC = Segue. Destinationviewcontroller;
    • //Set up a proxy for the next controller (add a contact's director)
    • Mjaddviewcontroller *ADDVC = VC;
    • ADDVC. Delegate = self ;
    • }
    • Proxy methods for #pragma mark-mjaddviewcontroller
    • -(void) Addviewcontroller: (mjaddviewcontroller *) ADDVC didaddcontact: (mjcontact *) Contact
    • {
    • //1. Add Model Data
    • [self. Contacts Addobject:contact];
    • //2. Refresh the table
    • [self. TableView Reloaddata];
    • }

[Transfer of]ios data

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.