2013-01-24 267 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
相关问题