English 中文(简体)
What are the advantages of use accessors to instances of IB elements?
原标题:

Usually, when we have to link an interface element to a field of a class, we use the keyword IBOutlet to inform the pre-copiler:

@interface MyController : NSObject {
    IBOutlet NSWindow *theWindow;
}

and in the implementation we use directly the pointer theWindow to call methods of the class NSWindow!

But what are the advantages of tell to the pre-compiler to create some accessors to the object that is pointed by "theWindow" and manage the object through the accessors?

Example:

@interface MyController : NSObject {
   NSWindow *theWindow;
}
@property(retain) IBOutlet NSWindow *theWindow;


@implementation MyController

@synthesize theWindow;

@end

Does the use of the second solution (for all the pointers to interface s elements) slows the performances of the application?

When is it a good practice to use the second way instead of the first?

Thank you!

最佳回答

Well in general I don t retain the properties, and the only way I ever even expose Outlets is if something needs to access an element from the outside, and this is generally a bad sign for your design anyway.

I ve not tried it but I d be tempted to only synthesis the read portion, and I d probably only use it in places where atomic access where important. Some portions of AppKit aren t always threadsafe.

问题回答

Bryan is correct in that you only need to expose UI element properties if access to them is needed from another instance or another class entirely. It is usually a sign of an architectural design problem but not always.

To answer the last two questions:

Does the use of the second solution (for all the pointers to interface s elements) slows the performances of the application?

No. No more than adding a method called -doSomethingReallyComplicated slows the application if that method is never called. It can slow launch having a bunch of unused code (thereby making the binary larger for no reason) and most certainly slows compile time, but it won t impact the application s runtime performance.

When is it a good practice to use the second way instead of the first?

As Bryan said, you expose a UI element as a property (via @synthesize or a hand-coded accessor) when you need to access it from outside a given instance of that class. Again, the need to do this is usually a sign of a design problem.

The time an accessor takes is completely imperceptible. Even if you ran this code millions of times, all the IB machinery would positively dwarf it.

If these variables are only set at that time and never accessed outside the class, accessors might be unnecessary.

When a nib is being unarchived, if an accessor exists to set a given outlet, then the unarchiver will use it. If there is no accessor, then the ivar will be set directly using the Obj-C runtime functions to find it by name.

The accessor will be faster, since you ll avoid all the slogging through the runtime to find the ivar in question. That being said, unless you re going to be loading that nib over and over, the difference will be negligible.

The main reason why you would want an accessor for an outlet is the same as for any other ivar or property you want to publish. It s a handy way to make sure that memory management is covered properly.





相关问题
How to start to create an application GUI using C#?

HI! I am new to C# and plan to use it for my application GUI. I am trying to make my GUI similar to SPSS:http://www.spss.com/images/08/statistics_screens/ez_rfm-big.jpg Is this easy in C#? Is there ...

Automatic height of edit box

My shoes application has three items stacked on top of each other (with a stack, of course), in order: A banner An edit box Two buttons in a flow What I want to do is have the banner stay at it s ...

Search by using the keyboard in a list/grid - algorithm

I need to implement a custom search in a grid and I would like to find some user interface guidelines that explain the standard way to implement it. I mean this kind of search that is initiated by ...

UI And TcpClient Issue in vb.net

I m having some problems with a small ircbot i m writing. Basically I connect to the server using a tcpclient in a seperate class, which also runs on its own thread. I want to display the server text ...

UI Convention: Shortcut key for application exit? [closed]

Is there a convention for the shortcut keys for application exit? Some applications uses Alt+X some others use Ctrl+ X and Ctrl+Q. Applications like FF and IE doesnot assign a shortcut at all. So is ...

热门标签