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

Mal*_*loc 0 iphone ios xcode4.3 ios5.1

我想做的是一个带有动作方法的简单按钮,该按钮被初始化,创建,分配给它的动作方法,并且仅在Debug和AdHoc模式下显示.所以作为开发人员或测试人员,我可以看到按钮,但在发布中,客户端将无法看到该按钮.

到目前为止我所做的是以下内容:

- 在我的project-->Build SettingsTab中,我在Debug和Adhoc中将Debug值设置为1,如下所示:

在此输入图像描述

- 然后我打开了prefix.pch文件,在那里,我被阻止了,我不知道该怎么做.

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

UIButton btnSwitch=[[UIButton alloc]init];

//Etc...
Run Code Online (Sandbox Code Playgroud)

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

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

Thanx提前.

Dam*_*amo 9

我不确定你对prefix.pch文件的看法是什么.暂时不管它.

您可以在视图控制器内的代码中创建一个按钮,并像这样有条件地执行.

#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
Run Code Online (Sandbox Code Playgroud)


Abi*_*ern 5

您可以将代码包装在警卫中:

#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
Run Code Online (Sandbox Code Playgroud)

另外 - 如果你使用Xcode 4,你不需要自己定义DEBUG,它就是为你完成的.您可以控制是否设置该方案.

默认Xcode方案针对Debug配置构建,该配置设置调试标志.如果要创建设置此构建标志的AdHoc方案,请根据Debug配置添加AdHoc配置,然后基于该方案创建方案.