Objective-C Design Help Needed, Need Protocol Possibly? - iphone

I have a situation where ClassA contains a UIView property. I have two children of ClassA, ChildA and ChildB, both set this UIView property for themselves with two different types of UIView subclasses. I'd like to require that both of these UIView subclasses have a property called maximumDimension, and then I'd like to be able to set it in ClassA using its own UIView property.
What is the best way to accomplish this? Would it be a protocol? If so would someone be able to provide code on how this can be achieved in Objective-C?

Would it be a protocol?
Yes:
#protocol Foo <NSObject>
#property (nonatomic, assign) int maximumDimensions; // wild guess to type from name
#end
Then the actual property:
#property (nonatomic, strong) id <Foo> bar;

For discussion's sake, here's a solution that uses a category instead of a protocol, extending UIView to have a maximumDimensions property:
UIView+MaximumDimensions.h
#interface UIView (MaximumDimensions)
#property ( nonatomic, readonly ) CGSize maximumDimensions ;
#end
UIView+MaximumDimensions.m
#implementation UIView (MaximumDimensions)
-(CGSize)maximumDimensions { return self.bounds.size ; }
#end
Now, in the methods of ClassA, you can access self.view.maximumDimensions. (Because UIView now has a maximumDimensions property.) Your subclasses of UIView, ChildA and ChildB can implement -maximumDimensions to return the correct answer.

Related

How to use #protocol in AppDelegate iPhone app?

I am working in iPhone app with 5 screens. I want to refresh the values in the screen 4th in UITabBarController. I have added #protocol in AppDelegate but it is not calling. This is the first time am using #protocol could you please help me to solve this issue,
In AppDelegate.h
#protocol ReloadViewControllerDelegate <NSObject>
-(void) refreshViewController:(NSString *)result;
#end
id refreshViewControllerDelegate;
#property (nonatomic, retain) id refreshViewControllerDelegate;
and i have synthesized.
In AppDelegare.m
#synthesize refreshViewControllerDelegate;
if ([refreshViewControllerDelegate conformsToProtocol:#protocol(ReloadViewControllerDelegate)])
{
[refreshViewControllerDelegate performSelectorOnMainThread:#selector(refreshViewController:) withObject:#"YES" waitUntilDone:NO];
}// Control not come inside of if Condition.... From here i want to update the fourthViewController..
But control not go inside of the if condition. Could you please guide me where am doing wrong?
In my 4th ViewController.h
#import "AppDelegate"
#interface fourthViewController : UIViewController <ReloadViewControllerDelegate>
In my 4th ViewController.m
-(void) refreshViewController:(NSString *)result
{
NSLog(#"Result : %#", result);
}
Can anyone please help me to do this? Thanks in advance.
You need to declare your delegate like this:
#property (nonatomic, weak) IBOutlet id<ReloadViewControllerDelegate> delegate;
The id will work, but by using the <>, you can make sure that the delegate you assign is actually implementing the protocol, you might still have to make sure it responds to selector but that is only if some methods are declared as
#optional
make sure you synthesize it and most important make sure you set it, and it is not nil.
You're getting a warning because you are typing your delegate as an id. An id is a generic type, which means the compiler has no idea of what methods or properties might be available. In order to remove your warning, declare your delegate to be an NSObject:
#property (nonatomic, retain) NSObject <ReloadViewControllerDelegate> *refreshViewControllerDelegate;
By declaring as an NSObject, the compiler now knows about all the methods NSObject has, which will then allow you to call:
-performSelectorOnMainThread:withObject:waitUntilDone:
on your delegate without warnings. Good luck!
Try this:
#protocol ReloadViewControllerDelegate <NSObject>
-(void) refreshViewController:(NSString *)result;
#end
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (weak) id <ReloadViewControllerDelegate>refreshViewControllerDelegate;
#end
In AppDelegate.m
#implementation AppDelegate
#synthesize window, refreshViewControllerDelegate;
...
here Tab4ViewController is name of class.
if ([Tab4ViewController conformsToProtocol:#protocol(ReloadViewControllerDelegate)])
{
[refreshViewControllerDelegate performSelectorOnMainThread:#selector(refreshViewController:) withObject:#"YES" waitUntilDone:NO];
}
...
#end
#import "AppDelegate.h"
#interface Tab4ViewController<ReloadViewControllerDelegate>
...
#end
#implementation Tab4ViewController
...
appDelegate.refreshViewControllerDelegate = self;
...
#end
You are calling this code:
if ([refreshViewControllerDelegate conformsToProtocol:#protocol(ReloadViewControllerDelegate)])
But refreshViewControllerDelegate is this:
id refreshViewControllerDelegate;
conformsToProtocol checks to see if the object declares that it conforms to the protocol, which yours does not. If you want to specify conformity to a protocol you need to:
id<ReloadViewControllerDelegate> refreshViewControllerDelegate;
EDIT
OK, on the performSelectorOnMainThread problem... That method is provided in a category for NSThread, and is not declared in the NSObject protocol. So, if you want to call that, then you need to declare your type as NSObject, which conforms to your protocol.
NSObject<ReloadViewControllerDelegate> refreshViewControllerDelegate;
EDIT
OK, it looks like this is not a simple question about using a protocol, but a full tutorial. Since SO isn't the place for such, I'll try to give a brief one...
A protocol is an interface declaration.
#protocol ReloadChatViewControllerDelegate <NSObject>
- (void)refreshViewController:(NSString *)result;
#end
That says there is a new protocol in town, with the name ReloadChatViewControllerDelegate and it also conforms to the NSObject protocol. Any class that adopts the new protocol must provide an implementation of refreshViewController. You can make a protocol method optional, by putting in an #optional section.
#protocol ReloadChatViewControllerDelegate <NSObject>
- (void)refreshViewController:(NSString *)result;
#optional
- (void)optRefresh;
#end
Now, let's leave the adoption of the protocol for later. Say you are writing generic code, and you just want to know if the object you are given conforms to the protocol, and if so, invoke a method on it. Something like...
#interface Bar : NSObject
#property (nonatomic, weak) NSObject<ReloadChatViewControllerDelegate> *refreshViewControllerDelegate;
- (void)blarg;
#end
Now, the Bar class is providing a delegate property, so that it can be give some object that will help it do some work. However, that delegate object must at least be an NSObject, and conform to the ReloadChatViewControllerDelegate protocol.
Now, ObjC (and C) is quite permissive, so you can force an object to be any type you want, but then you deserve the crash you get. Now, when blarg is called, the delegate is notified to do some work.
Since the property type of the delegate already says it conforms to the given protocol, there is no need to check for conformity. We can just call the delegate method. Note that we must see if the object implements any optional protocol methods.
#implementation Bar
#synthesize refreshViewControllerDelegate = _refreshViewControllerDelegate;
- (void)blarg {
// Do something, then invoke the delegate
[self.refreshViewControllerDelegate
performSelectorOnMainThread:#selector(refreshViewController:)
withObject:#"YES"
waitUntilDone:NO];
if ([self.refreshViewControllerDelegate respondsToSelector:#selector(optRefresh)]) {
[self.refreshViewControllerDelegate optRefresh];
}
}
#end
However, if you want to be generic, and accept any object as a delegate (maybe you want to make it optional that the delegate conforms to some given protocol), then you can accept a plain id and then check to see it it conforms. In that case, you could declare your delegate as just an id (or some other type).
#property (nonatomic, weak) id refreshViewControllerDelegate;
Now, in your code, you need to check for conformity.
- (void)blarg {
// Do something, then invoke the delegate
if ([self.refreshViewControllerDelegate
conformsToProtocol:#protocol(ReloadChatViewControllerDelegate)]) {
[self.refreshViewControllerDelegate
performSelectorOnMainThread:#selector(refreshViewController:)
withObject:#"YES" waitUntilDone:NO];
if ([self.refreshViewControllerDelegate
respondsToSelector:#selector(optRefresh)]) {
[self.refreshViewControllerDelegate optRefresh];
}
}
}
OK, now you have a protocol defined, and you have code that calls methods on the protocol. Two caveats.
First, the delegate has to be set to an object. nil will respond false for any method, so it will of course not conform, nor do anything when sent any message.
Second, you have to make sure that your delegate declares conformity to the protocol. Just implementing the methods is not conformity. If a class does not explicitly specify that is conforms to a protocol, then conformsToProtocol will return false, even if it implements the methods of the protocol.
So, let's specify some class that will act as our delegate by conforming to the protocol.
#interface Foo : NSObject<ReloadChatViewControllerDelegate>
- (void)refreshViewController:(NSString *)result;
#end
#implementation Foo
- (void)refreshViewController:(NSString *)result {
NSLog(#"Look, ma, I'm refreshed with %#", result);
}
#end
It conforms to the protocol, provides an implementation for the mandatory method, and omits the optional one.
Now, if you ran this code, you should see that marvelous code in all its splendor.
Foo *foo = [[Foo alloc] init];
Bar *bar = [[Bar alloc] init];
bar.refreshViewControllerDelegate = foo;
[bar blarg];

objective c multiple inheritance conflict with many

I just started to build in objective c from iphone.
I have interface:
#interface test : UIView
that implements all methods from UIView, now i want it interface inherit from CDVPlugin also.
How can i do it?
i read that objective c doesn't offer multiple inheritance.
you can't inherent from CDVPlugin and UIView.
I would suggest doing something like this
#interface test : NSObject {
UIView *view;
CDVPlugin *plugin;
}
#property(nonatomic, retain) UIView *view;
#property(nonatomic, retain) CDVPlugin *plugin;
#end
and you can add methods to that class to handle anything that you're trying to do. Then just use test.view to access the view and test.plugin to access the plugin.

How to use delegates in ios 5

I need to use a delegate object in an iOS application. I have declared the delegate as this:
In the class where the function is defined:
#interface OOObjectCommandInterface : NSObject<OOCameraControllerDelegate>
In the class where the function must be invoqued:
(In de .h file)
#protocol OOCameraControllerDelegate
- (void)drawFrame:(CVImageBufferRef) imageBuffer:(BOOL)flip;
#end
and
#interface OOCameraController : UIViewController
{
...
id<OOCameraControllerDelegate> delegate;
}
#property (nonatomic, readwrite) id<OOCameraControllerDelegate> delegate;
Aditionally, where the second class is initialized:
_hardwareController.delegate = [OOObjectCommandInterface ocInterface];
where _hardwareController is an instance of OOCameraController class.
So, when I try to invoque the delegate object, I do this:
[delegate drawFrame:imageBuffer:flip];
but the function is not executed. Any idea?
P.D.: The function I am calling is a singleton class. Could be any problem there?
Have you set delegate to self in the second class? Create an object in the second class like
#property (nonatomic, readwrite) id<OOCameraControllerDelegate> delegate;
and then [_hardwareController setDelegate:self];
By definition, a singleton is a design patron to access an object, unique, that only can be created 1 time (first get_instance you do). With get_instance, you can access from everywhere, to the functions inside the singleton, so, Why you are not using it directly?
Write something like [[MySingletonClass get_instance] FunctionThatIWantToUse:...]; And don't use a delegate

Forward declaring a protocol in objective-c

My class .h looks like:
#protocol AppInfoDelegate;
#class InfoTextView;
#interface AppInfoViewController : UIViewController <AppInfoDelegate> {
}
#property (nonatomic, retain) NSArray *textObjectsArray;
#property (nonatomic, retain) InfoTextView *itView;
#property (nonatomic, retain) UIButton *pgBackButton;
#property (nonatomic, retain) UIButton *pgFwdButton;
#end
#protocol AppInfoDelegate <NSObject>
- (void)closeButtonPressed:(id)sender;
#end
I get a warning that the protocol definition for AppInfoDelegate cannot be found. What is the proper way to do this and why cannot it not be found? Do I need to have the whole definition of the protocol before the interface? Thanks!
Using #protocol MyProtocol; is useful when you are asserting, for example, that a method will take id <MyProtocol> as an argument.
It is not useful when you are claiming that your class conforms to <MyProtocol>. The reason for this is that the compiler needs the full protocol declaration in order to verify that your class actually conforms to the protocol. (This compile-time check is one great reason to use formal protocols instead of the older informal ones.)
You can fix in two ways. One, as #skram suggests, is to just forward-declare the whole thing. This works, but it's also rather limited in my view. Why bother with a protocol in that case - just put everything in the class #interface and be done with it.
The second approach, which I prefer, is to actually have a separate header, such as MyProtocol.h. You can then freely import this into any header or implementation files as needed. This allows you to reuse a protocol easily (and avoid the headaches of circular imports that sometimes arise).
Try this:
#protocol AppInfoDelegate <NSObject>
- (void)closeButtonPressed:(id)sender;
#end
#class InfoTextView;
#interface AppInfoViewController : UIViewController <AppInfoDelegate> {
}
#property (nonatomic, retain) NSArray *textObjectsArray;
#property (nonatomic, retain) InfoTextView *itView;
#property (nonatomic, retain) UIButton *pgBackButton;
#property (nonatomic, retain) UIButton *pgFwdButton;
#end
Yes, superclass and adopted protocol definitions need to be defined (verbatim or by using #import) before the class definition. They cannot be forward-declared.
I've always seen the whole protocol definition before the #interface. I believe you can also put it into a separate file though

Understanding Protocol Inheritance in Objective-C

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.