2012-06-21 176 views
0

我想要做的是一个带有操作方法的简单按钮,此按钮被初始化,创建,分配给其操作方法,并仅在Debug和AdHoc模式下显示。所以作为开发人员或测试人员,我可以看到按钮,但在发布中,客户端将无法看到该按钮。如何仅在DEBUG和AdHoc模式下执行特定功能

我做了到目前为止是这样的:

- 在我的project-->Build Settings标签,我Debug和即席设置调试值设置为1,这样的:

enter image description here

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

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

UIButton btnSwitch=[[UIButton alloc]init]; 

//Etc... 

上面的代码应在一个特定的文件(UIViewController类应包含的按钮)来调用。

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

Thanx提前。

+0

此链接可能会对您有所帮助。 http://stackoverflow.com/questions/7252176/ios-detect-ad-hoc-from-code – looyao

回答

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 
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 

而且 - 如果你正在使用的Xcode 4,你不需要自己定义DEBUG,它为你做。您可以控制它是否设置为查看该方案。

默认的Xcode方案针对设置调试标志的调试配置构建。如果您想创建一个设置此构建标志的AdHoc方案,请根据Debug配置添加一个AdHoc配置,然后根据此配置创建一个方案。

+0

Thanx为您的答复,我会测试您的解决方案,并回到你:) – Malloc

+0

嗨,这种解决方案适用于'Adhoc'模式呢?如果不是,我应该提及什么“指令”? Thanx :) – Malloc

+0

正如我所说 - 基于调试配置创建一个名为AdHoc的新配置,然后基于此创建一个新的配置。 – Abizern

相关问题