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