ios - 如何定义宏以在 ios 中执行操作

标签 ios objective-c prefixheader.pch

我正在尝试编写一个在调用时执行某些操作的自定义宏。

这是我的常量文件:

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


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

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

然后我创建了一个 pch 文件:

#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

#endif /* MYPrefixHeader_pch */

在我的 View Controller 中,我尝试调用宏:

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    SHOW_VIEW;
}

我在 SHOW_VIEW 行看到错误:

enter image description here

最佳答案

在常量 header MYGlobals.h 中包含您的应用程序委托(delegate)

#import "AppDelegate.h"

我强烈建议重新考虑此方法以获得更具可读性和一致性的代码。相反,您可以为您的应用委托(delegate)实例定义一个宏。

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

然后直接调用它的方法:

[MY_APP_DELEGATE showView];
[MY_APP_DELEGATE hideView];

关于ios - 如何定义宏以在 ios 中执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37664397/

相关文章:

objective-c - 在Xcode中调用 "(id)sender"方法

iphone - 应用程序可以找到有关当前在 iPhone/iPod Touch 上播放歌曲的信息吗?

objective-c - 在 swift 中从前缀 header 引用类定义

ios - Firebase Storage iOS 放置在上传完成之前调用的文件完成处理程序

objective-c - 使用 OpenGL-ES 在 iOS 上播放视频

iphone - iOS - 在用作 UITableView header 的 ViewController 上设置 socket

ios - 使用 GCD 在后台创建 UIKit 对象是一种不好的做法吗?

objective-c - 如何确定用于 Metal 顶点计算的线程组和线程大小