marciokoko
marciokoko

Reputation: 4986

Why is viewcontroller property not being set?

In a method fired by a button I call this code:

//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);
    }

The console log always logs the correct self.userLocation value, but not the sVC.userLocation, which always comes up null.

This method is in one of the tab-uiviewcontrollers of a uitabbarcontroller whereas the SecondViewController is the other tab-uiviewcontroller.

Why is sVC.userLocation not being set?

Upvotes: 1

Views: 175

Answers (2)

user467105
user467105

Reputation:

This line:

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

should probably be:

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

because you want to know if anyVC (not anyVC.class) is of type SecondViewController.


The value returned by anyVC.class (or [anyVC class]) will be of type Class and will never be of type SecondViewController (so the if condition always returns NO).

Since the if condition is never satisfied, self.sVC never gets set and probably stays nil meaning the setUserLocation call does nothing, etc.


Also, you probably want to put all the statements related to self.sVC inside the if block otherwise the setUserLocation and the NSLog get executed even if the if condition fails:

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 %@", ...
    }
}

Upvotes: 1

Yuliani Noriega
Yuliani Noriega

Reputation: 1083

Other things you might need to take into consideration:

  • Have your alloc/init your userLocation variable in sVc such as in -init or -viewDidLoad?

  • Do you have a @property (nonatomic, strong) CLLocation *userLocation in sVc class?

Upvotes: 0

Related Questions