This recipe shows how to create a new contact using the ABNewPersonController.
Recipe

To create a contact using the built-in ABNewPersonController follow these steps.
- Add a UINavigationController as the RootViewController in the FinishedLaunching method of the AppDelegate. For this example, the project is named CreateContact and the CreateContactViewController is added to the top of the UINavigationController’s stack.
// class-level declarations UIWindow window; UINavigationController navController; CreateContactViewController viewController; public override bool FinishedLaunching (UIApplication app, NSDictionary options) { window = new UIWindow (UIScreen.MainScreen.Bounds); viewController = new CreateContactViewController (); navController = new UINavigationController (viewController); window.RootViewController = navController; window.MakeKeyAndVisible (); return true; }
- In the CreateContactViewController, add a UIButton and a UILabel. The button will open the ABNewPersonController and the label will show name of the new contact after the contact is created. Also add a class variable for the ABNewPersonController.
UIButton _createContact; UILabel _contactName; ABNewPersonViewController _newPersonController; public override void ViewDidLoad () { base.ViewDidLoad (); Title = "How to Create a Contact"; View.BackgroundColor = UIColor.White; _createContact = UIButton.FromType (UIButtonType.RoundedRect); _createContact.Frame = new RectangleF (10, 10, 300, 50); _createContact.AutoresizingMask = UIViewAutoresizing.FlexibleWidth; _createContact.SetTitle ("Create a Contact", UIControlState.Normal); _contactName = new UILabel{Frame = new RectangleF (10, 70, 300, 50)}; _contactName.AutoresizingMask = UIViewAutoresizing.FlexibleWidth; View.AddSubviews (_createContact, _contactName);
- Create an ABNewPersonController instance.
_newPersonController = new ABNewPersonViewController ();
- In the button’s TouchUpInside event handler, create an ABPerson, set it to the DisplayedPerson property of the ABNewPersonController, and push the controller onto the navigation controller’s stack.
_createContact.TouchUpInside += (sender, e) => { var person = new ABPerson (); person.FirstName = "John"; person.LastName = "Doe"; _newPersonController.DisplayedPerson = person; NavigationController.PushViewController (_newPersonController, true); } ;
-
Implement a handler for the NewPersonComplete event of the ABNewPersonController, setting the label’s text to the name of the new contact and popping the ABNewPersonController off the navigation controller’s stack. This completes the ViewDidLoad’s implementation.
_newPersonController.NewPersonComplete += (object sender, ABNewPersonCompleteEventArgs e) => { if (e.Completed) { _contactName.Text = String.Format ( "new contact: {0} {1}", e.Person.FirstName, e.Person.LastName); } else { _contactName.Text = "cancelled"; } NavigationController.PopViewControllerAnimated (true); } ; }
Additional Information
The ABNewPersonController must be used with a UINavigationController. The NewPersonComplete event will contain an ABPerson instance if the Done button is selected. The Completed property will be false if Cancel is selected.