0

Using different frameworks in my projects I have often faced with custom elements which created as class inherit from NSObject (correct me if something is wrong). What are the main rules of creating such UI elements?

3 Answers 3

4

NSObject is the very basic class in Cocoa. It is responsible for the most basic things that every class needs, like memory management. (Almost) all classes in Cocoa inherit from NSObject and you usually subclass NSObject if you want to implement a model class.

If you want to create your own GUI element, you should subclass UIView or UIControl. UIView will give you capabilities to do custom drawing, handling touch events and others. UIControl (which itself is a UIView subclass) adds functionality for control elements which the user can interact with, like UITextField, UISlider etc. This is what you should subclass if you plan to implement a custom control.

Sign up to request clarification or add additional context in comments.

1 Comment

Not all classes inherit from NSObject. For instance, NSProxy is used for message-forwarding ("proxy") classes.
1

Main purpose of using custom objects is to create model classes, which help in storing data which can be used through out the application.

For e.g. -

@interface ServerResponse
.....
@property (nonatomic, retain) NSString *responseString;
@property (nonatomic, retain) NSArray *errorCodes;
.....
@end

In addition to this NSObject is root class in Objective C. Most classes inherit the feature of NSObject Classs.

1 Comment

NSObject is only one root class in Objective-C. Not all classes inherit from it.
1

If you are creating UI elements you could inherit from NSObject, however I would strongly recommend to inherit from UIView or UIControl. Otherwise you will just be recreating functionality already given by UIControl.

Additionally if you simply want to add additional functionality to a existing UI element, you could extend (create a category) to add that functionality.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.