Re: finding my UIViewController
Re: finding my UIViewController
- Subject: Re: finding my UIViewController
- From: Roland King <email@hidden>
- Date: Thu, 04 Aug 2011 18:58:36 +0800
Thanks for the reply, but that wouldn't really have done it for me. This problem isn't really about tableViews, I just happened to have a table view in this case.
Imagine you have a custom UIView subclass which is able to view and edit some particular type of object to which it contains a reference. Now assume that as part of its editing duties that view has a button which needs to call up a modal view controller (in my case it's a UIImagePicker). Now instantiate one of those views at some random point in a random view hierarchy; where it is doesn't matter, it's a view, it has a model object, whether it's the top level view, or in a table, or 18 levels of views down in a vastly complicated iPad screen, its function remains the same, to display the contents of the object and allow you to edit it.
When you hit the button and want to bring up the image picker you need to do it by calling presentModalViewController:animated: on some UIViewController. The question was how do you find a UIViewController to call it on? UIViews have no link back to any UIViewController. You could of course make all your custom views have such a link and hook it up every time you added them to a superview, but it would be a real pain and not very generic.
What I did eventually find is that just using [ [ view window ] rootViewController ] was fine, that gave you the root view controller for the window and that was good enough to push a modal VC onto. My problem was that I was popping it incorrectly when I first tried it and ended up with a black screen. I don't love using the root view controller, would prefer to find the view controller furthest down the chain but it's working for now. I have a feeling that more explicit support for hierarchies of view controllers may show up in iOS one day and then it would be easier to figure out what view controller is the 'closest' above you. Until then I have something workable.
On Aug 4, 2011, at 12:58 AM, Evadne Wu wrote:
> I think you just need a way to present a view controller from a button in a table view cell returned by a UITableViewController subclass. In that case, keyword: iOS 4 — on the cell itself one can say…
>
> @property (nonatomic, readwrite, copy) void (^actionBlock)();
> @property (nonatomic, readwrite, copy) void (^dismisssalBlock)();
>
> (or use objc_setAssociatedObject() and objc_getAssociatedObject() if you absolutely don’t want to subclass) …then when configuring the cell to be sent off to the table view:
>
> __block __typeof__(self) nrSelf = self;
> // We’ll assume as long as the cell is alive, `self`, which is the view controller, is alive.
>
> cell.dismissalBlock = ^ {
> [nrSelf dismissModalViewControllerAnimated:YES];
> };
> cell.actionBlock = ^ {
> [nrSelf presentModalViewController:someStuff animated:YES];
> };
>
> Might work if you’ve got less than a dozen of cells and are running on an iPhone 3GS or up. :) Just note that calling -dismissModalViewControllerAnimated: anywhere in the stack, even on the view controller currently being presented modally, works fine. So you might won’t even need the dismissal block at all. Or just do the old target-and-action assignments with some context info tucked somewhere in between.
>
> -ev
>
> On Aug 4, 2011, at 00:07, Roland King wrote:
>
>> I feel I've asked this question before but google doesn't think I have.
>>
>> My code is dealing with a button press in a control on a UITableViewCell in a UITableView which is contained in another UIView subclass and on and up through several views until eventually there is a topmost UIView which was presented from a UIViewController subclass. I want the button press to pop up a modal UIImagePickerController which will eventually pick the image for the cell I'm in. To pop it up I need a UIViewController instance to pop it up from, and to pop it down again afterwards.
>>
>> Is there any way to find, given a UIView, what the closest presenting UIViewController is?
>>
>> I tried going the other way, used [ [ UIView window ] rootViewController ] to pop up the image picker, that pops it up fine, but popping it down leaves a black screen, not really surprising, that's not the UIViewController which was really the controller at the point I pushed it, that's the root one, there's a few on the stack after that. I wasn't able to find a way to navigate down the UIViewController stack to find the bottom one, even if I could it sounds a bit fragile.
>>
>> Do I really need to pass the UIViewController down through why whole view hierarchy to my UITableViewCells so they know what to use for a modal popup and down; or crawl upwards superview by superview until I find one which perhaps implements some interface telling me it knows what the UIViewController which presented it was?
>>
>> I must have missed a method somewhere which does this. This is iOS 4.x on the phone. _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> Xcode-users mailing list (email@hidden)
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>> _______________________________________________
>>
>> Cocoa-dev mailing list (email@hidden)
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden