如何仅在DEBUG和AdHoc模式下执行特定function

我想要做的是一个带有操作方法的简单button,这个button被初始化,创build,分配给它的操作方法,并仅在Debug和AdHoc模式下显示。 所以作为开发人员或testing人员,我可以看到button,但是在发行版中,客户端将无法看到该button。

我到目前为止所做的是以下几点:

– 在我的project-->Build Settings选项卡中,我将Debug和Adhoc中的Debug值设置为1,如下所示:

在这里输入图像说明

然后我打开prefix.pch文件,在那里,我被封锁,我不知道该怎么做。

基本上,我的行动方法是这样的:

 UIButton btnSwitch=[[UIButton alloc]init]; //Etc... 

上面的代码应该在特定的文件中调用(UIViewController类应该包含button)。

我该怎么做,我的意思是,我怎么才能告诉我的应用程序只能在DEBUG和Adhoc模式下在特定文件中执行该代码。

提前感谢。

我不知道你对prefix.pch文件的看法是什么。 暂时独自一人。

你可以在你的视图控制器中的代码中创build一个button,并像这样有条件地进行。

 #ifdef DEBUG UIImage *buttonImage = [UIImage imageNamed:@"btnSwitchImage"]; btnSwitch = [UIButton buttonWithType:UIButtonTypeCustom]; //frame size given as a point of reference only but when you create a button this //way you have to set the frame otherwise you will see nothing. btnSwitch.frame = CGRectMake(0.0f, 0.0f, buttonImage.size.width, buttonImage.size.height); [btnSwitch setBackgroundImage:buttonImage forState:UIControlStateNormal]; [btnSwitch addTarget:self action:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnSwitch]; #endif 

你可以把代码包装起来:

 #ifdef DEBUG // Code here to only run when DEBUG is defined #else // Code here to run only when DEBUG is not defined #endif // code here to execute regardless of the state of DEBUG 

另外 – 如果你使用的是Xcode 4,你不需要自己定义DEBUG,这是为你完成的。 你可以控制它是否设置在看计划。

默认的Xcodescheme针对设置debugging标志的Debugconfiguration进行构build。 如果要创build一个设置此构build标志的AdHocscheme,请根据Debugconfiguration添加一个AdHocconfiguration,然后根据此configuration创build一个scheme。