alexrothenberg / motion-addressbook

MIT License
89 stars 30 forks source link

Cannot open Picker from Modal ViewController #63

Open cspeer opened 10 years ago

cspeer commented 10 years ago

Hey,

first of all: thanks for your awesome work :) In my application I open a ViewController as a modal. When I try to run the AddressBook.pick I get:

Warning: Attempt to present <ABPeoplePickerNavigationController: 0x124d11e0> on <MyController> whose view is not in the window hierarchy!

I'm not sure what exactly the problem here is since MyController is obviously in the hierarchy. I tried to work around with the presenter option on the pick method with some luck: When I explicitly select a ViewController to use the Picker comes up, but when it's closed it also closes my underlying modal window.

Any ideas?

Thanks, Chris

jmay commented 10 years ago

@cspeer I've seen problems when using the global methods. Please try

ab = AddressBook::AddrBook.new
ab.picker(...same options go here...)

That's how the test suite is doing it. Please advise if that works & I'll update the README.

cspeer commented 10 years ago

@jmay thanks for the reply. I'm not sure what's going wrong here, but calling the picker method on an AddressBook::AddrBook instance gives me:

#<NoMethodError: undefined method `picker' for #<AddressBook::AddrBook:0x13026850 live>>
jmay commented 10 years ago

Are you using the latest version? Should be 1.7.2

cspeer commented 10 years ago

Unfortunately I wasn't. I didn't use the git repo as the source and rubygems only offers 1.7.0. So, no the picker method is known. However I get the same result as when I'm using the global methods. That's what my code looks like:

   address_book = AddressBook::AddrBook.new
    if AddressBook.request_authorization
      address_book.picker do |person|
        select_phone(person) if person
      end
    else
      App.alert('To add a person please enable this app in System Settings > Privacy > Contacts'.t)
    end
jmay commented 10 years ago

I pushed the 1.7.2 release to rubygems. My error for not doing it earlier.

I tried opening a picker from the RubyMotion command line, in both the current version at 1.7. No problems in either case. This worked in 1.7.2:

ab = AddressBook::AddrBook.new
# grant permission
ab.picker {|p| puts p}
cspeer commented 10 years ago

Yes, assuming permissions are granted I can open a picker exactly like that. However, when I open up a new ViewController as a modal screen first, I get the following message:

Warning: Attempt to present <ABPeoplePickerNavigationController: 0xca8ad60> on <NavigationController: 0xc94dcf0> whose view is not in the window hierarchy!
=> #<AddressBook::Picker:0xca840f0 @ab=#<AddressBook::AddrBook:0xc947940 live> @after=#<Proc:0xca88310> @people_picker_ctlr=#<ABPeoplePickerNavigationController:0xca8ad60> @presenter=#<ProMotion::NavigationController:0xc94dcf0> @animated=true>

And no picker shows. It's got something to do with the modal screen.

jmay commented 10 years ago

@cspeer could you please post a larger code fragment that shows the error. I haven't done much with Cocoa controllers, this is a good time to learn more. Perhaps the PeoplePicker just doesn't work as modal?

chunlea commented 10 years ago

@cspeer Try this. Add presenter: self to picker.

   address_book = AddressBook::AddrBook.new
    if AddressBook.request_authorization
      address_book.picker presenter: self do |person|
        select_phone(person) if person
      end
    else
      App.alert('To add a person please enable this app in System Settings > Privacy > Contacts'.t)
    end