0

I'm picking the name and surname from AddressBookUI. And I'm controlling if the user name exist in my array and I can find it.

My issue is I'm trying to present and alertView after the user tap(select) and I'm getting this issue but I'm getting this issue below while I want to present it on the contactList.

Warning: Attempt to present on whose view is not in the window hierarchy!

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior ()

Please where would be my issue?

func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController, didSelectPerson person: ABRecord) {

    let firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue() as? String ?? ""
    let lastName  = ABRecordCopyValue(person, kABPersonLastNameProperty)?.takeRetainedValue() as? String ?? ""

    if let parentVC = self.parentViewController as? UIPageViewController{
        if let parentNewCarRequestVC = parentVC.parentViewController as? NewCarRequestViewController{

            let people:RequestPeople = RequestPeople()
            people.name = first
            people.surname = last

            if first.isEmpty || last.isEmpty{

               print("Error")

            }else{

                for item in parentNewCarRequestVC.request.peoples{

                    if item.name == firstName && item.surname! == lastName{

                        let alert:UIAlertController = UIAlertController(title: “Error”, message: “Same Number”, preferredStyle:.Alert)
                        viewController.presentViewController(alert, animated:true, completion:nil);

                        return
                    }
                }

                parentNewCarRequestVC.request.peoples.addObject(people)
            }

        }
        tableView.reloadData()
    }
}
CAN
  • 1,677
  • 4
  • 19
  • 28

1 Answers1

0

How about putting your entire code inside dispatch block.

dispatch_async(dispatch_get_main_queue()) { () -> Void in
    let firstName ...
    ...
    if {...
    tableView.reloadData()
    }   
}
Karthik
  • 1,386
  • 12
  • 7