Teo
Teo

Reputation: 3442

Pass data to root controller

I am developing a contacts app. I want to add a contact, by opening a new view like this:

RootViewController.m... it has a NSMutableArray called contacts

- (IBAction)addContact:(id)sender {

    AddContViewController *cont = [[AddContViewController alloc]init];
    [self.navigationController presentViewController:cont animated:YES completion:nil];

}

And then come back and add the contact to the array of the root view controller:

AddContViewController.m

- (IBAction)acceptAction:(id)sender {


    if ([[firstName text] length] < 1 && [[lastName text] length] < 1)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Oh no!" message:@"Invalid contact information!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
    }
    else{

// create the contact and put it in the root view controller's array

   Contact *cont = [[Contact alloc]initWithFirstName:[firstName text] lastName:[lastName text] andDOB:[dobPicker date]];

// and now I don't know what to do....

    [self dismissViewControllerAnimated:YES completion:^{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Success!" message:@"Contact added successfully!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
        }];
    }
}

Upvotes: 0

Views: 1071

Answers (3)

adig
adig

Reputation: 4057

You should use a delegate to communicate the new Contact object back to your RootViewController.

Define the protocol

@protocol AddContDelegate
   -(void)didAddnewContact:(Contact *)contact;
@end

On Your AddContViewController have a delegate property :

@property (nonatomic, assign) id<AddContDelegate> delegate;

In your addContact: method assign the delegate :

- (IBAction)addContact:(id)sender {

    AddContViewController *cont = [[AddContViewController alloc]init];
    cont.delegate = self;
    [self.navigationController presentViewController:cont animated:YES completion:nil];

}

Implement the delegate method in RootViewController :

-(void)didAddnewContact:(Contact *)contact {
   [contacts addObject:contact];
}

Call the delegate from AddContViewController :

- (IBAction)acceptAction:(id)sender {


    if ([[firstName text] length] < 1 && [[lastName text] length] < 1)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Oh no!" message:@"Invalid contact information!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
    }
    else{

   Contact *cont = [[Contact alloc]initWithFirstName:[firstName text] lastName:[lastName text] andDOB:[dobPicker date]];

    if([self.delegate respondsToSelector:@selector(didAddnewContact:)]) {
        [self.delegate didAddnewContact:cont];
    }

    [self dismissViewControllerAnimated:YES completion:^{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Success!" message:@"Contact added successfully!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
        }];
    }
}

Upvotes: 3

MPG
MPG

Reputation: 251

You may want to use delegate for this action so that you can communicate to the rootviewcontroller to have contact object.

The same can be achieved using the stack of navigationViewController using [navigationViewController viewControllers] and of the last object is of type of your class you can perform certain selector of your root and dismiss your AddContViewController with a success message.

Hope this will help!!

Upvotes: 0

Darren
Darren

Reputation: 10398

There are several ways to pass the data back. I'd suggest setting up a delegate method. Add this to the top of your AddContViewController.h after any imports:

@class addContViewController
@protocol addContViewControllerDelegate <NSObject>
-(void)addContViewController:(addContViewController *)controller didAddContact:(Contact *)contact;
@end

And after the interface section add

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

Then in your RootViewController.h add the protocol to the interface line <addContViewControllerDelegate>
Now in your RootViewController.m method addContact just before you push the new view, add:

cont.delegate = self;

Now in your AddContViewController.m instead of dismissing the view, call:

[self.delegate addContViewController:self didAddContact:cont];

This will call a new method in your RootViewController which it'll pass the Contact and in here you can do with it want you want, but first dismiss the view:

-(void)addContViewController:(addContViewController *)controller didAddContact:(Contact *)contact {
self dismissViewControllerAnimated:YES;

}

Upvotes: 1

Related Questions