iOS create macro

后端 未结 4 1642
情歌与酒
情歌与酒 2021-02-06 05:18

I have a piece of code I\'m using fairly often and would like to make a macro out of it. I\'m not exactly sure how to do that though. Here\'s the code I want to use



        
相关标签:
4条回答
  • 2021-02-06 05:47

    #define Image_Macro @"myLogo.png" UIImage *titleImage = [UIImage imageNamed:Image_Macro]; UIImageView *titleImageView =[[UIImageView alloc] initWithImage:titleImage]; self.navigationItem.titleView = titleImageView;

    0 讨论(0)
  • 2021-02-06 05:56
    #define MY_MACRO( img ) \
        {\
            UIImage *titleImage = [UIImage imageNamed:img]; \
            UIImageView *titleImageView = [[UIImageView alloc] initWithImage:titleImage]; \
            self.navigationItem.titleView = titleImageView; \
            [titleImageView release];\
        }
    

    Use it like this:

    MY_MACRO( @"myLogo.png" )
    

    The use of {} creates a scope block, which will prevent problems with variable redefinitions (if you have variables with the same name, where you use the macro).

    0 讨论(0)
  • 2021-02-06 05:56

    use #define with the preprocessor, write a function, or write a method for your class.

    0 讨论(0)
  • 2021-02-06 05:56

    Try the following macro

    #define addImage( __imageName__)    \
    UIImage *titleImage = [UIImage imageNamed:__imageName__];    \
    UIImageView *titleImageView = [[UIImageView alloc] initWithImage:titleImage]; \
    self.navigationItem.titleView = titleImageView; \
    [titleImageView release]; \
    
    0 讨论(0)
提交回复
热议问题