2012-06-21 174 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

相關問題