0

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.

Zeebok
  • 388
  • 1
  • 4
  • 15
  • Create on Data Manager class with getter & setter methods depending on your requirement – srinivas n Jan 19 '15 at 09:38
  • 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 Jan 19 '15 at 09:40
  • 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 *)controller – Zeebok Jan 19 '15 at 09:41
  • @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 Jan 19 '15 at 09:46
  • 1
    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:https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html to understand how view controllers in iOS works – wcd Jan 19 '15 at 09:50

1 Answers1

0

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
Community
  • 1
  • 1
Jay
  • 856
  • 7
  • 17
  • thank you very much. . . it worked perfect. . one more question plz, i am totaly new to iOS - (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]; } } i want to send array instead of nametextfield . . . – Zeebok Jan 19 '15 at 10:59
  • Please vote up. If you are satisfied with my answer :) – Jay Jan 19 '15 at 11:26
  • 1
    i don't have required reputation points to vote you up. . . i already tried that, but msg occurred that you don't have required reputation points.. . . i have asked related question, but you didn't replied. please consider above comment and reply please. – Zeebok Jan 19 '15 at 11:39
  • No problem. I have added answer to your second question in the same answer. Please check again. – Jay Jan 19 '15 at 11:51