简体   繁体   中英

How to Pass a Object to NextViewController in iOS?

I am Parsing the RESTKIT Values.I am Having a Entity called EY_Connections with attributes.Just am Parsing the Values using RKMappingOperation in ViewController.m and i stored the Values of mappingOperation to object of EY_Connections .

ViewController.m

@interface NewConnectionViewController()
{
DataAccessHandler *dataAccess;
RKMappingResult *savedMappingResult;
EY_Connections *connect;
}

-(void)requestConnectionData
{
NSString *requestPath = @"user_history/consumer/data.json";
[[RKObjectManager sharedManager]
 getObjectsAtPath:requestPath
 parameters:nil
 success: ^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
     NSLog(@"result arrray us: %@",mappingResult);
     savedMappingResult = mappingResult;
     connect = ((EY_Connections *)savedMappingResult.firstObject);  
 }
 failure: ^(RKObjectRequestOperation *operation, NSError *error) {
     RKLogError(@"Load failed with error: %@", error);
 }
 ];
}

If i Click a Save Button means,It should Pass All the Attribute Values of Connect Object into HomeViewController.How To pass These Values.This Is My button Action

-(IBAction)Save:(id)sender
{
//Please Update the Code To Pass the Connect Object to HomeViewController.

}

Link your view controllers in storyboard with segue and give that segue a name. Then:

-(IBAction)Save:(id)sender
{
    [self perfromSegueWithIdentifier:@"MySegue" sender:self];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"MySegue"])
    {
        // Get reference to the destination view controller
        YourViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        [vc setMyObjectHere:object];
        // or maybe
        vc.savedMappingResult = savedMappingResult;
    }
}

Maybe your view controller is somewhere else - different storyboard:

-(IBAction)Save:(id)sender
{
    UIStoryboard * story = [UIStoryboard storyboardWithName:@"WhereIsIt" bundle:nil];
    YourViewController *vc = [story instantiateInitialViewController];
    // add values
    you could use instantiateViewControllerWithIdentifier as well
}

Since it is a bit unclear is your view controller coming after or you should save values and return to previous controller here is an implementation with delegate:

Create delegate in NewViewController.h

@protocol NewViewControllerDelegate
    -(void) saveMyData:(EY_Connections*)connections;
@end

When calling NewViewController set delegate to homeviewcontroller like so:

NewViewController *nvc = // instantiate it somehow
nvc.delegate = self;

Then in HomeViewController implement this method for saving:

-(void) saveMyData:(EY_Connections*)connections{
    // save your data here into variabels in the home view controller
}

and then, in your method call:

-(IBAction)Save:(id)sender
{
    [delegate saveMyData:connect];
}

To have it covered, you can use unwind segue as well and reuse prepare for segue we had down there.

If you are using present view controller you can:

a) use delegate as shown above

b) cast presentingViewController into HomeViewController and assign it there.

Here is explanation about delegates:

https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM