iphoneiosxcode4.3ios5.1

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


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:

enter image description here

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


Solution

  • 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