RomanHouse
RomanHouse

Reputation: 2552

Creating custom objects inherit NSObject class

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?

Upvotes: 0

Views: 1267

Answers (3)

Oscar Gomez
Oscar Gomez

Reputation: 18488

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.

Upvotes: 1

DrummerB
DrummerB

Reputation: 40201

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.

Upvotes: 4

rishi
rishi

Reputation: 11839

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.

Upvotes: 1

Related Questions