How can I do something like this ?
#interface SomeClass:NSViewController **:NSTableViewController** #end
How can i put two subclases in my class ??
Objective-C does not support Multiple Inheritance.
Typically, you work around this by using protocols when you want to program to an interface.
#interface SomeClass : NSViewController < SomeProtocol >
#end
Another option is composition:
#interface SomeClass : NSObject
{
#private
NSViewController * viewController;
NSTableViewController * tableViewController;
}
#end
Related
I am curious if and how to make a Controller be the delegate for two different objects.
Is this allowed or is this like multiple inheritance in Java?
Suppose I wanted to have one controller that responded to: <UIAccelerometerDelegate> and <CLLocationManagerDelegate>
Would the header file look like this?
#interface MainViewController : UIViewController <UIAccelerometerDelegate> AND <CLLocationManagerDelegate> {
Actually, it works quite well. Declare your interface like this:
#interface MainViewController : UIViewController <UIAccelerometerDelegate, CLLocationManagerDelegate>
and then implement the methods from both delegate interfaces.
Nope, like this:
#interface MainViewController : UIViewController <UIAccelerometerDelegate, CLLocationManagerDelegate> {
#interface MainViewController : UIViewController <UIAccelerometerDelegate, CLLocationManagerDelegate>
As simple as that:
#interface MainViewController : UIViewController <UIAccelerometerDelegate, CLLocationManagerDelegate>
I'll appreciate if anyone can explain the logic behind protocol inheritance. e.g. what does the following mean (UITableView.h):
#protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>
The following class implementation doesn't work. I have a class View1 (which inherits UIView), with an associated protocol. I have another class, View2 (which inhertits View1). Now i want to inherit the the protocol as well. Can anyone please point me in the right direction.
Class 1:
#protocol View1Delegate;
#interface View1 : UIView {
id <View1Delegate> delegate;
// . . .
}
#property (nonatomic, assign) id <View1Delegate> delegate; // default nil. weak reference
#end
#protocol View1Delegate <NSObject>
- (void)View1DelegateMethod;
#end
#implementation View1
#synthesize delegate;
// . . .
#end
Class 2:
#protocol View2Delegate;
#interface View2 : View1 {
id <View2Delegate> delegate;
// . . .
}
#property (nonatomic, assign) id <View2Delegate> delegate; // default nil. weak reference
#end
#protocol View2Delegate <NSObject>
- (void)View2DelegateMethod;
#end
#implementation View2
#synthesize delegate;
// . . .
#end
Think of it more as composition rather than inheritance.
#protocol UITableViewDelegate<NSObject, UIScrollViewDelegate> defines a protocol that includes all the methods of the NSObject protocol, the UIScrollViewDelegate protocol, as well as any methods defined for the UITableViewDelegate protocol. When you subclass and create a new property, you're overriding the type of the superclasses property. To make this work how I think you want, you should declare View2Delegate as #protocol View2Delegate <NSObject, View1Delegate>.
It's exactly the same as the inheritance of interfaces in Java (interface UITableViewDelegate extends NSObject, UIScrollViewDelegate), C# (interface UITableViewDelegate : NSObject, UIScrollViewDelegate), etc.
#interface MainViewController : UIViewController < FlipsideViewControllerDelegate >{
}
I am quite new to object oriented languages & especially obj.c.
I need to use addressbook framework within this class.Apple documentation suggests this code:
#interface ViewController : UIViewController < ABPeoplePickerNavigationControllerDelegate > {}
I was wondering how to use at the same time FlipSideControllerDelegate & ABPeoplePickerControllerDelegate.
In fact I really can't understand what i am doing:)
You can declare your class as conforming to both the FlipsideViewControllerDelegate and the ABPeoplePickerNavigationControllerDelegate protocols like this:
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate,
ABPeoplePickerNavigationControllerDelegate> {}
Then implement the required methods in MainViewController's #implementation.
I have a large class, which I have divided into several different class extension files for readability.
#protocol MyProtocol
#required
-(void)required;
#end
#interface MyClass : NSObject <MyProtocol>
#end
#interface MyClass (RequiredExtension)
-(void)required;
#end
Is there a better way to do this, without the compiler warning?
warning: class 'MyClass' does not fully implement the 'MyProtocol' protocol
Use a category for each protocol implementation. I use this when I have complex viewControllers.
For example, I have a category that implements NSTextDelegate protocol.
So, MyComplexViewController+NSTextDelegate.h:
#import "MyComplexViewController.h"
#interface MyComplexViewController (NSTextDelegate) <NSTextDelegate>
#end
and MyComplexViewController+NSTextDelegate.m:
#import "MyComplexViewController+NSTextDelegate.h"
#implementation MyComplexViewController (NSTextDelegate)
- (BOOL)textShouldBeginEditing:(NSText *)textObject{
...
}
- (BOOL)textShouldEndEditing:(NSText *)textObject{
...
}
- (void)textDidBeginEditing:(NSNotification *)notification{
...
}
- (void)textDidEndEditing:(NSNotification *)notification{
...
}
- (void)textDidChange:(NSNotification *)notification{
....
}
#end
Then I take all the headers for the main class definition and the categories and combine them into one header which I then import where I need to use the class.
#interface MyClass : NSObject
#end
#interface MyClass (RequiredExtension) <MyProtocol>
-(void)required;
#end
Adopt the protocol in the category.
You don't need to change your style of coding. To get around the warning, it only need to implement "required" method of the protocol, not "optional"
If that's only for readability, you should use categories only. A protocol is not needed in such a case.
I am assigning protocols in a couple classes that follow an inheritance tree. Like so:
first class
#protocol LevelOne
- (void) functionA
#end
#interface BaseClass : NSObject <LevelOne> {
}
second class
#protocol LevelTwo <LevelOne>
- (void) functionB
#end
#interface SubClass : BaseClass <LevelTwo> {
}
Later I am assigning the class as delegate properties of other classes
base class
#interface AppClass : NSObject {
#protected
id<LevelOne> levelOneDelegate;
}
#property (assign) id<LevelOne> levelOneDelegate;
subclass
#interface AppClassesFriend : AppClass {
#protected
id<LevelTwo> levelTwoDelegate;
}
#property (assign) id<LevelTwo> levelTwoDelegate;
At the end of this journey, AppClassesFriend has 2 properties on it.
"levelOneDelegate" has access to "functionA", when it is assigned with a BaseClass object.
However, I am finding that "levelTwoDelegate" only has access to "functionB" (it is assigned with a SubClass object).
In order to have AppClassesFriend be able to use both functions, it seems I need to assign BOTH a levelOneDelegate AND levelTwoDelegate.
Is there any way to make "levelTwoDelegate" have access to both? Since, both functions are available on "SubClass".
So, what I would like to be able to do is :
SubClass *s = [SubClass alloc];
AppClassesFriend *a = [AppClassesFriend alloc];
a.levelTwoDelegate = s;
so inside AppClassesFriend (a) I could use :
[self.levelTwoDelegate functionA]; <---- this is never found
[self.levelTwoDelegate functionB];
but it seems I have to add
a.levelOneDelegate = s;
Thanks if anyone took the time to read all the way down this far. So in summary the question is, how do I get "levelTwoDelegate" to have access to both functionA and functionB?
Simply declare that your subclass's delegate property implements both level one and level two protocols (i.e. implements both functionA and functionB):
#interface AppClassesFriend : AppClass {
#protected
id<LevelOne,LevelTwo> levelOneAndTwoDelegate;
}
#property (assign) id<LevelOne,LevelTwo> levelOneAndTwoDelegate;