0
votes

My question is how can i send back data from 2nd view controller to 1st view controller using uinavigationcontroller. i have 2 buttons on navigation controller to send data back or cancel,as 2ndviewcontroller.h contains //------------------------------------------------------

@class DetailsViewController;
@protocol DetailsViewControllerDelegate <NSObject>
- (void)detailsViewControllerDidCancel:(DetailsViewController *)controller;
- (void)detailsViewControllerDidSave:(DetailsViewController *)controller;
@end



@interface DetailsViewController : UIViewController
{


    UITextField *nameTextField;
    UITextField *versionTextField;
    UITextField *companyTextField;
}



@property (nonatomic, weak) id <DetailsViewControllerDelegate> delegate;

- (void)cancel:(id)sender;
- (void)save:(id)sender;
//-------------------------------------------------------------

2ndviewcontroller.m contains
- (void)cancel:(id)sender
{ 
    [self.delegate detailsViewControllerDidCancel:self];   
}


- (void)save:(id)sender
{
    [self.delegate detailsViewControllerDidSave:self];
}

while my 1stviewcontroller implemented the 2ndviewcontroller's delegate as, //----------------------------------------------------

- (void)detailsViewControllerDidCancel:(DetailsViewController *)controller
{
    [detailedViewController dismissViewControllerAnimated:YES completion:nil];
}

- (void)detailsViewControllerDidSave:(DetailsViewController *)controller
{
    detailedViewController = [self.navigationController viewControllers][0];

    [self dismissViewControllerAnimated:YES completion:nil];

}

//------------------------------------------------------

any idea please? i am doing all the matters programmatically, not using any storyboard.

1
Create on Data Manager class with getter & setter methods depending on your requirementsrinivas n
this can be easily done. define a public method in first controller, and call it in second controller. or delegate, notification, key value observing, etc. It's very easy.wcd
i think thats not the proper way, delegate and protocols should do the job. i wonder the why control is not coming back to firstviewcontroller - (void)detailsViewControllerDidCancel:(DetailsViewController *)controller or - (void)detailsViewControllerDidSave:(DetailsViewController *)controllerZeebok
@wcd i am sorry, i could not get you. how can i instantiate a parent class in child.in your case i think Parent created child class. . and child will create parent.Zeebok
It seems to me that you misunderstood usage of UINavigationController. You used dismissViewControllerAnimated. This method is used to dismiss a view controller of which the view is presented modally. Maybe you need to read up this documentation:developer.apple.com/library/ios/featuredarticles/… to understand how view controllers in iOS workswcd

1 Answers

0
votes

You need to know about the data passing as parameter from detail-view-controller to parent-view-controller right. In the delegate mention the string of value, or array of data anything and then in the parent you will get it.

First, when you call a delegate from Detail-View-Controller. I have added the responds to selector, to avoid crashing when the delegate is an optional and not declared in the Parent Class.

- (void)save:(id)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(detailsViewControllerDidSave:version:company:)]) {
        [self.delegate detailsViewControllerDidSave:nameTextField.text version:versionTextfield.text company:companyTextfield.text];
    }
}


- (void)cancel:(id)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(detailsViewControllerDidCancel)]) {
        [self.delegate detailsViewControllerDidCancel];
    }
}

Second in the Parent-View-Controller, no need to pass the whole UIViewController itself. Pass only the necessary values, in your case I assume name, version and company.

- (void)detailsViewControllerDidCancel
{
    // No data required/// and just dissmisses the screen
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)detailsViewControllerDidSave:(NSString *)name version:(NSString*)version company:(NSString*)company
{

    // you can use the values as parameter.. name, version, company
    // ......

    [self dismissViewControllerAnimated:YES completion:nil];

}

Example code:

  1. How do I set up a simple delegate to communicate between two view controllers?

If you want to send as an Array, do like below,

Parent-ViewController - (void)detailsViewControllerDidSave:(NSMutableArray*)listOfValues {

    // you can use the list of values 
    // ......

    [self dismissViewControllerAnimated:YES completion:nil];

}

Detail-View-Controller

- (void)save:(id)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(detailsViewControllerDidSave:)]) {
        [self.delegate detailsViewControllerDidSave:[NSMutableArray arrayWithObjects:nameTextfield.text, versionTextfield.text, companyTextfield.text, nil]];
    }
}

Change the declaration of the protocol,

@protocol DetailsViewControllerDelegate <NSObject>
- (void)detailsViewControllerDidCancel;
- (void)detailsViewControllerDidSave:(NSMutableArray *)listOfValues;
@end