2013-01-24 156 views
1

在由按鈕激發的方法我把這個代碼:爲什麼viewcontroller屬性沒有設置?

//Get the sVC in order to se its property userLocation 

    UITabBarController *myTBC = (UITabBarController*)self.parentViewController; 
    for(UIViewController *anyVC in myTBC.viewControllers) { 
     if([anyVC.class isKindOfClass:[SecondViewController class]]) 
     self.sVC = (SecondViewController *)anyVC; 
     [self.sVC setUserLocation:self.userLocation]; 

     NSLog(@"userLocation ISSET to %@ from %@", self.userLocation, sVC.userLocation); 
    } 

控制檯日誌記錄總是正確self.userLocation值,而不是sVC.userLocation,它總是出現空。

此方法位於uitabbarcontroller的tab-uiviewcontrollers之一中,而SecondViewController是另一個tab-uiviewcontroller。

爲什麼sVC.userLocation沒有設置?

+0

VC如何爲userLocation設置屬性? –

+0

在sVC中,它由屬性合成器設置。我不明確地在sVC中設置它,只在fVC中。 – marciokoko

回答

0
  • SecondViewController是否有一個屬性userLocation?
  • 你可以分享如何定義這個屬性的代碼?
  • 您是否爲該屬性實現了自己的setUserLocation/userLocation方法?
  • 您確定在運行時,類SecondViewController的sVC?
+0

1。是SecondViewController有一個屬性,就像分配它的值2一樣。@property(strong,nonatomic)CLLocation * userLocation;來自「簡明英漢詞典」我沒有執行自己的二傳手。我不明白第四個問題。 – marciokoko

+0

Regd。第四個問題,如果它不是SecondViewController類的代碼,那麼代碼不會顯示sVC的值,所以我認爲它在這種情況下是零,因此任何嘗試訪問類似sVC.somePropName的東西都會返回nil。 –

+0

是的,但在tabbar的viewcontrollers屬性中有一個sVC。一個是FirstViewController,另一個是SecondViewController。所以它肯定會找到它並因此設定它。加上ISSET的NSLog正在打印出來,所以我知道這是工作 – marciokoko

0

,你可能需要考慮其他的事情:

  • 有你的分配/初始化SVC中的用戶位置的變量,如-init-viewDidLoad

  • 你有沒有在sVc類@property (nonatomic, strong) CLLocation *userLocation

+0

謝謝,我沒有發起財產... – marciokoko

+0

不,它沒有被設置。我認爲這是,但不是。我不認爲我應該在vDL中初始化它,因爲在用戶點擊tabbarcontroller中的tab時,tableVC不會被初始化。所以如果我將它設置在mapVC中,然後切換到tableVC(它將它放入),它將清除設置值。即使設置它的代碼仍然不起作用。 NSLog中的sVC.userLocation仍然爲null – marciokoko

1

這條線:

if([anyVC.class isKindOfClass:[SecondViewController class]]) 

大概應該是:

if([anyVC isKindOfClass:[SecondViewController class]]) 

,因爲你要知道,如果anyVC(不anyVC.class)是SecondViewController類型。


通過anyVC.class(或[anyVC class])返回的值將是Class類型的,並且決不會SecondViewController類型(因此if條件始終返回NO)的。

由於if條件永不滿足,永不self.sVC獲取設置和可能保持nil意味着setUserLocation調用什麼也不做,等


另外,你可能希望把所有相關self.sVC裏面的語句if塊否則setUserLocationNSLog即使if條件未能得到執行:

for (UIViewController *anyVC in myTBC.viewControllers) 
{ 
    if ([anyVC isKindOfClass:[SecondViewController class]]) 
    { 
     self.sVC = (SecondViewController *)anyVC; 
     [self.sVC setUserLocation:self.userLocation]; 
     NSLog(@"userLocation ISSET to %@ from %@", ... 
    } 
} 
以s
相關問題