2012-04-20 107 views
2

好的我知道這裏有很多帖子,但我仍然遇到麻煩。下面是我想要做的僞代碼:如何在頭文件中使用預處理器檢查?

if(device is running iOS 5 or up) 

    @interface RootViewController : UIViewController <UIPageViewControllerDelegate, UIGestureRecognizerDelegate> 

    @property (strong, nonatomic) UIPageViewController *pageViewController; 

else 

    @interface RootViewController : UIViewController <LeavesViewDelegate, UIGestureRecognizerDelegate> 

    @property (strong, nonatomic) LeavesViewController *leavesViewController; 

endif 

我說得對不對思考我需要使用預處理器宏檢查,因爲它是在頭文件?這是一本書應用程序,如果它是iOS 5或更高版本(因此具有UIPageViewController),應使用UIPageViewController,否則它會退回到Leaves(https://github.com/brow/leaves)上。我有所有的代碼設置。只需要知道如何告訴編譯器使用哪個。我不認爲使用任何運行時檢查是可行的,因爲我只需要UIPageViewController或Leaves編譯的協議方法,而不是兩者。我寧願不使用完全獨立的源文件。我一直在使用這些檢查嘗試:

#ifdef kCFCoreFoundationVersionNumber_xxx

#ifdef __IPHONE_xxx

#if __IPHONE_OS_VERSION_MAX_ALLOWED <__IPHONE_xxx

(各種XXX的)

缺少什麼我在這裏?

編輯:

我也注意到這個默認.PCH:

#ifndef __IPHONE_5_0 
#warning "This project uses features only available in iOS SDK 5.0 and later." 
#endif 

這使我想知道爲什麼同樣的試驗並沒有在我的.h文件中工作?

+1

大多數時候,iOS的二進制文件不是分別編譯的不同版本的操作系統;只有一個二進制文件用版本X的「基本SDK」設置和版本Y的「部署目標」(向後兼容性)設置進行編譯,其中X> = Y。因此,除非預處理器宏執行此操作,否則不能執行此操作您打算在商店上部署單獨的應用程序,但是從相同的代碼庫構建。 – 2012-04-20 06:33:01

+0

那麼處理這個問題的標準方法是什麼?我是否需要將其更改爲僅使用運行時檢查? – Marty 2012-04-20 06:34:34

+0

我有兩個目標,然後使用預處理器檢查,以便以不同的方式編譯兩個目標? – Marty 2012-04-20 06:38:18

回答

0

正如我在評論中提到的那樣,在編譯時你不能這樣做。

但這裏是你的一個想法:看來的UIPageViewControllerDelegateLeavesViewDelegate方法名不相交,所以你可以添加以下內容到你的頭文件:

-(void) leavesView:(LeavesView*)leavesView willTurnToPageAtIndex:(NSUInteger)pageIndex; 
-(void) leavesView:(LeavesView*)leavesView didTurnToPageAtIndex:(NSUInteger)pageIndex; 
-(void) pageViewController:(UIPageViewController*)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray*)previousViewControllers transitionCompleted:(BOOL)completed; 
-(UIPageViewControllerSpineLocation) pageViewController:(UIPageViewController*)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation; 

,並沒有明確採用委託協議在頭文件中(忽略< >中的代表)。

無論您使用哪兩個類都可以在*中實例化。在條件M檔沿線:

// check for existence of class to determine which controller to instantiate 
if(NSClassFromString(@"UIPageViewController")) 
{ 
    // do something and set UIPageViewController delegate to "self" 
} 
else 
{ 
    // do something else and set LeavesViewController delegate to "self" 
} 

最後,爲了得到這個編譯,你可能會需要轉發聲明所有LeavesViewController - 在您使用它們,UIPageViewController - 相關課程,並可能utilize weak linking一些框架。

我還沒有使用Apple的UIPageViewController類和協議,所以我不能提供更多的見解。一定要讓我們知道,如果你得到了一些錘打出來的東西:)

0

你不能這樣做,因爲預處理器宏是在編譯時處理的。編譯器應該知道,你的目標是哪個iOS,因爲你在Mac上編譯而不是在iPhone上編譯每個人?

您無法在運行時輕鬆切換代碼。有可能性,但我不認爲這意味着你想要它。

您可以在運行時檢查方法是否可以從特定的SDK中獲得。這更簡單直接。然而,你無法實現你的目標。

我建議: 創建一個超類,其中沒有包含特定的委託協議。在那裏你寫下你想要分享的所有代碼。

然後從上層超類創建2個子類。在每個類中放入你的特定代碼。

和那個它。 這是應該的方式。

+0

有兩個不同的目標呢? – Marty 2012-04-20 07:01:21

相關問題