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];
}];
}
}
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:
And after the interface section add
Then in your RootViewController.h add the protocol to the interface line
<addContViewControllerDelegate>Now in your
RootViewController.mmethodaddContactjust before you push the new view, add:Now in your
AddContViewController.minstead of dismissing the view, call: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: