Teja Nandamuri Teja Nandamuri - 2 years ago 68
iOS Question

How to define a macro to perform an action in ios

I'm trying to write a custom macro that performs some action when called it.

Here is my constants file:

#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

#define SHOW_VIEW [(AppDelegate*)[UIApplication sharedApplication].delegate showView]

#define HIDE_VIEW [(AppDelegate*)[UIApplication sharedApplication].delegate hideView]

and I created a pch file:

#ifndef MYPrefixHeader_pch
#define MYPrefixHeader_pch

// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.

#ifdef __OBJC__
#import "MYGlobals.h"

#endif /* MYPrefixHeader_pch */

and in my view controller, I'm trying to call the macro:

[super viewWillAppear:animated];

And I see errors on the line SHOW_VIEW :

enter image description here

Answer Source

Include your app delegate in the constants header MYGlobals.h

#import "AppDelegate.h"

I would urge to reconsider this method to get more readable and consistent code. Instead, what you can do is define a macro for your app delegate instance.

#define MY_APP_DELEGATE ((AppDelegate *)[UIApplication sharedApplication].delegate)

Then call its methods directly:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download