2014-06-19 17 views
0

中分配值我試圖讓我的iPad應用程序通用。不過,我有一個小問題。我有這個代碼。在我的masterviewcontroller中,根據選擇的國家/地區,所述國家/地區被傳遞到我的detailviewcontroller,我的國家被加載到我的detailview中。無法在iphone版本的程序

我的問題是,當這個代碼在我的iphone上運行時,我的activecountry爲null。但是當它在ipad上運行時,它有一個值。我爲iPhone版選擇了正確的類,我不完全確定爲什麼,因爲我的iphone故事板中有detailviewcontroller視圖。

因此,我的問題是,什麼阻止我將我的價值傳遞到self.detailviewcontroller.activecountry。

想得到任何幫助,我可以解決這個問題。

謝謝

PS。 ipad版本上的ipad detailview控制器是uisplitview控制器的詳細版本。

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
    { 

    lat = 15.4166152; 
    lon =-61.353149; 

    [self.detailViewController animateToPosition:MaplyCoordinateMakeWithDegrees(lon,lat) time:1.0 height:0.1]; 


    ThridViewController *thrid=[[ThridViewController alloc]init]; 
    [self.navigationController pushViewController:thrid animated:YES]; 

     [thrid setTitle:@"America"]; 
    [email protected]"America"; 
    NSLog(@"The active country is %@",self.detailViewController.activeCountry); 
    } 
    else 
    { 

     lat = 15.4166152; 
     lon =-61.353149; 

     [self.detailViewController animateToPosition:MaplyCoordinateMakeWithDegrees(lon,lat) time:1.0 height:0.1]; 


     ThridViewController *thrid=[[ThridViewController alloc]init]; 
     [self.navigationController pushViewController:thrid animated:YES]; 

     [thrid setTitle:@"America"]; 
     [email protected]"America"; 
     NSLog(@"The active country is %@",self.detailViewController.activeCountry); 
    } 

}

更新 我想我發現了什麼。

在我的應用程序委託中,在聲明我的iPad正在運行的代碼中,我調用了detailview控制器上的init,但在iPhone中我沒有這樣的事情。主要導致我不知道如何。因爲我的detailviewcontroller沒有開始看到。

更新,應用代理

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 


    [DBAccess copyDatabaseIfNeeded]; 

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
    { 
     //Place iPhone code 
    } else { 

     // Place iPad-specific code here... 

    MasterViewController *left=[[MasterViewController alloc]init]; 
    UINavigationController *navigLeft=[[UINavigationController alloc]initWithRootViewController:left]; 

    DetailViewController *detail=[[DetailViewController alloc]init]; 
    UINavigationController *navigRight=[[UINavigationController alloc]initWithRootViewController:detail]; 

    left.detailViewController = detail; 

    UISplitViewController *splitViewController=[[UISplitViewController alloc]init]; 
    splitViewController.delegate=detail; 

    splitViewController.viewControllers=[NSArray arrayWithObjects:navigLeft,navigRight,nil]; 

      } 
return YES; 

} 
+0

爲什麼不向我們展示您的應用代理的iPad代碼,我們將看到我們需要爲iPhone做些什麼。 – Fluffhead

+0

@Fluffhead嗨,我添加了應用程序委託代碼。我不確定何時初始化我的detailviewcontroller。我正在考慮在我的主視圖上做一些類似於uipopover的事情(蘋果是否允許在iPhone應用程序中使用uipopover?或者僅僅是ipad。我看到一個人在做YouTube視頻,所有人都告訴他他的應用程序將被拒絕) – Sammie

+0

Sammie,你可以使用UIView來「僞造」彈出窗口,或者嘗試一個自定義的類,例如:http://stackoverflow.com/questions/3987218/uipopover-for-iphone-or-is-it-fake。 – Fluffhead

回答

0

好了,我的第一個問題是:你爲什麼要爲iPhone和iPad的重複這麼多的代碼?一般情況下,你只需要在這些條件語句中放入不同的東西 - 通常這就是UI特定的代碼。我只會在這些條件中放置特定設備,並將常用代碼留在外面,供所有設備使用。這可以解決你的問題。

如果不解決這個問題,將在斷點處:

[email protected]"America"; 

並打印值:self.detailViewController 以及self.detailViewController.activeCountry,讓我們知道他們在說什麼。

+0

我知道需要不重複代碼,這段代碼甚至不需要指定正在使用的設備,但這是故障排除的一部分。據建議,我添加了一個斷點。在iPhone上我得到_detailViewController \t DetailViewController * \t無\t 0x00000000和self.detailViewController。activeCountry從我的nslog中爲null – Sammie

+0

嗯,有一個很大的線索:你的_detailViewController是零,所以你不能給它的activeCountry屬性賦值。看起來你需要檢查你在哪裏分配/啓動它。 – Fluffhead

相關問題