Search code examples
iosswiftinitializationaddressbook

Connecting methods to buttons and initializing


I recently started learning Swift and for my first app (Master-Detail Template), I am trying to pull data (add contacts to a list) from my Address Book. I am trying to connect my showAddressBook method to the + button in the Master. The build fails on the init line (error: "Initializers may only be declared within a type"), so I'm guessing my code is wrong.

func showAddressBook() {
    var addressBookController = ABPeoplePickerNavigationController.alloc()
    init(addressBookController = ABPeoplePickerNavigationController) {
        self.addressBookController = presentViewController(presentViewController: addressBookController, animated: true, completion: nil)
    }
}

I can upload the code for connecting Address Book data if necessary. Thanks!


Solution

  • this isn't the way you init the ABPeoplePickerNavigationController simply write :

        func showAddressBook() {
    
        var addBook = ABPeoplePickerNavigationController()
        self.presentViewController(addBook, animated: true) { () -> Void in}
    }
    

    or you can use another initialiser and then use the presentViewController. note : using the same name for two variables (addressBookController) is bad programming habit :D