How to execute a specific function only in DEBUG and AdHoc modes

筅森魡賤 提交于 2019-12-12 04:13:56

问题


What i would like to do is a simple button with an action method, This button is initialized, created, assigned to its action method and shown ONLY in Debug and AdHoc modes. So as a developer or tester, i can see the button, but in the release, the client won't be able to see that button.

What i did so far is the following:

-In my project-->Build Settings Tab, i set the Debug values to 1 in both Debug and Adhoc, like this:

-Then i opened up the prefix.pch file, and there, i am blocked and i don't know what to do.

Basically, my action method is something like this:

UIButton btnSwitch=[[UIButton alloc]init];

//Etc...

The above code should be called in a specific file (The UIViewController class which should contain the button).

How can i do that, i mean, how can i tell my application to execute that code in a specific file only in DEBUG and Adhoc modes.

Thanx in advance.


回答1:


I'm not sure what your thinking with regards to the prefix.pch file is. Leave that alone for the moment.

You can create a button in code inside your view controller and do it conditionally like this.

#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



回答2:


You can wrap code up in guards:

#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

Also - if you are using Xcode 4 you don't need to define DEBUG yourself, it's done for you. You can control whether or not it is set be looking at the scheme.

The default Xcode scheme builds against Debug configuration which sets the debug flag. If you want to create an AdHoc scheme that sets this build flag, add an AdHoc configuration based on the Debug configuration and then create a scheme based on that.



来源:https://stackoverflow.com/questions/11132287/how-to-execute-a-specific-function-only-in-debug-and-adhoc-modes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!