开发者

Is there a way to automagically get the ID that the object was allocated in?

开发者 https://www.devze.com 2023-01-17 21:02 出处:网络
Say I do this: @implementation currentViewController ... SomeClass *myObject = [SomeClass alloc]; How can myObject get a reference of currentViewController without sending a message to myObject lik

Say I do this:

@implementation currentViewController
...
SomeClass *myObject = [SomeClass alloc];

How can myObject get a reference of currentViewController without sending a message to myObject like this:

[myObject wasCreat开发者_运维知识库edIn:self];

Side note: Where do action sheets and notifications get displayed in? The current view or the appDelegate window? Does the iphone save a current hierarchy of views or do I have to keep track of it myself?


No, there's no "automagic" for this. You can create an instance of SomeClass anywhere, and alloc/init have no idea where you're calling from unless you want to tell them by creating a property and setting it explicitly as you suggest.

This is true of all objects in general.

UIViewControllers know who their parents are, though, so if the object you're talking about is a view controller that's been pushed onto a navigation stack or presented modally from another controller, you should be able to figure out who the parent controller is.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号