为什么没有设置viewcontroller属性?

时间:2013-01-24 15:16:30

标签: ios uiviewcontroller

在按钮触发的方法中,我调用此代码:

//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-uiview控制器之一,而SecondViewController是另一个tab-uiviewcontroller。

为什么没有设置sVC.userLocation

2 个答案:

答案 0 :(得分:1)

这一行:

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

应该是:

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

因为您想知道 anyVC (不是anyVC.class)是否属于SecondViewController类型。


anyVC.class(或[anyVC class])返回的值将为Class类型,且永远不会为SecondViewController类型(因此if条件始终返回{{ 1}})。

由于永远不会满足NO条件,if永远不会被设置,可能会停留self.sVC,这意味着nil调用什么也不做,等等。


此外,您可能希望将与setUserLocation相关的所有语句放在self.sVC块中,否则即使if条件setUserLocationNSLog也会执行失败:

if

答案 1 :(得分:0)

您可能需要考虑的其他事项:

  • 在sVc中将alloc / init设置为userLocation变量,例如-init-viewDidLoad

  • 您在sVc课程中有@property (nonatomic, strong) CLLocation *userLocation吗?