开发者

I want to write to UI objects, on the screen, outside of the ViewController, how ??

What is the "appropriate means" of writing to UI objects, on the view screen, outside of the ViewController object... by means of a totally different object?

After thinking about it for a bit... I set a pointer to "self", in "viewDidLoad", and passed that along. It is just a memory location开发者_如何学Go and it has clearly worked for me on a number of occasions since then. As someone pointed out to be careful and use a 64 bit integer or long when doing so for 64 bit machines. That was good advice. But most people do not like that idea of using a pointer. So what is a better way?

(I posted my solution but it was removed... even though it was a working solution, it was voted down many times)


self is a hidden parameter that is passed to every instance method in Objective-C. It is a reference to the object on which the method was invoked. In your case, it appears that self is an instance of UIViewController or a more specific implementation of said class.


If your question is "what is the type of "self", the answer is "it's the type of the object you're calling it from". For example, if you're talking about a UIViewController subclass named calcRomanViewController, then the type of self is calcRomanViewController*.

It sounds like what you're trying to do is pass a reference self around to other objects. One way to do that is simply have the method you're calling on the other class take a parameter of the appropriate type (for example, UIViewController).

In cases where it's not appropriate/necessary for the other class to know exactly what type of object is going to be passed in, you can use the Objective-C generic object type id. For example:

- (void) echo:(NSString *) message withDelegate:(id) delegate {...}

You can take this a step further by declaring a protocol, in effect saying "I don't care what kind of object you pass me, as long as it implements the following methods". For example:

@protocol echoDelegate 
- (void) echoCompleted;
@end

- (void) echo:(NSString *) message withDelegate:(id<echoDelegate>) delegate {...}


First, it's self, not Self and it is the current object, i.e. the current instance of UIImageView / ViewController and you can treat it as any other object.


It depends on what class you're doing it from. Whatever object's method you run that from, is the instance and type that self will be. Same as C++ concept.


The questions you ask requires too long answers to fit on Stackoverflow. They are about the very fundamentals of Objective-C as a programming language, and Cocoa Touch as an application framework. There are already good guides available to get you started, so I instead point you to them.

Your first question tells me that you have not understood Objective-C. I suggest you start by reading Learning Objective-C: a Primer, available from Apple here: http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/

Your second questions tells me that you have not understood the concept of View Controllers. I suggest you begin by reading View Controllers Programming Guide for iOS, also by Apple and available here: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜