0

I have a problem with protocols. I have a class like this :

@class SideToolBarDelegate;
@interface AuthentificationViewController : UIViewController <ASIHTTPRequestDelegate, UITextFieldDelegate,SideToolBarDelegate> {
}
...

I would like that my class "AuthentificationViewController" conforms to the protocol "SideToolBarDelegate" just i am in the iPhone version and not to conform it i am in the iPad version. How i can declare this ? thanks.

samir
  • 4,501
  • 6
  • 49
  • 76

3 Answers3

2

You can make some defins set in project properties and use ifdefs. Like:

@interface AuthentificationViewController : UIViewController <ASIHTTPRequestDelegate, UITextFieldDelegate
#ifdef IPAD
,SideToolBarDelegate>
#else
>
#endif

But this is old C way. It is a very bad idea to do this in program conforming to Object Oriented Application Design. In this case you have two more ways:

  1. make separate subclasses for iPhone and iPad
  2. make this one class as is and dont use SideToolBarDelegate methods from iPhone. This makes code clear and better to maintain in the future.

After all I suggest making two classes for viewControllers:

AuthentificationiPadViewController : UIViewController <ASIHTTPRequestDelegate, UITextFieldDelegate>
AuthentificationiPhoneViewController : UIViewController <ASIHTTPRequestDelegate, UITextFieldDelegate, SideToolBarDelegate>

Just think of future debugging of your code!

pro_metedor
  • 1,176
  • 10
  • 17
0

If you did not create a universal app, you could define a couple of preprocessor macros to determine whether it's iPad or iPhone at compile time:

#define IS_IPAD   (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

and then do something like

#if IS_IPHONE

@class SideToolBarDelegate;
@interface AuthentificationViewController : UIViewController <ASIHTTPRequestDelegate, UITextFieldDelegate,SideToolBarDelegate> {
}

#else

@interface AuthentificationViewController : UIViewController <ASIHTTPRequestDelegate, UITextFieldDelegate> {
}

#endif

If you did create a universal app (which sounds like your case), this can't be determined at compile-time. It has to be done at run-time.

If that's the case. you are better off creating separate views for iPhone and iPad like @pro_metedor mentioned.

jere
  • 4,306
  • 2
  • 27
  • 38
0

It is really needed:

  1. You can create separate build targets for iPhone and iPad apps
  2. With the [app]-iPad target chosen as the active target, pick Project -> Edit Active Target [app]-iPad. Search for "c flags" and double-click. Add a flag for "-D TARGET_IPAD". Now the symbol TARGET_IPAD will be defined only for your iPad target (grabed from here).
  3. Change Your code to:

@class SideToolBarDelegate;

@interface AuthentificationViewController : UIViewController <
    ASIHTTPRequestDelegate,
    UITextFieldDelegate
#ifdef TARGET_IPAD 
    ,SideToolBarDelegate
#endif
>
{
}
Community
  • 1
  • 1
CAMOBAP
  • 5,523
  • 8
  • 58
  • 93