0

In the MainViewController .h file:

-(void)startTrackingParking:(int)hour :(int)minute :(int)second;

In the MainViewController .m file:

-(void)startTrackingParking:(int)hourTrack :(int)minuteTrack :(int)secondTrack {
    NSLog(@"%d %d %d",hourTrack,minuteTrack,secondTrack);

        //Set buttons alpha to 0
        self.extendParking.alpha = 0;
        self.bannerCancelButton.alpha = 0;
        self.timeLeftLabel.alpha = 0;

}

In my other class I then do:

MainViewController *class = [[MainViewController alloc]init]; [class startTrackingParking:hourTrack :minuteTrack :secondTrack];

For some reason the NSlog calls, but the buttons alpha do not get updated? Why is this happening? Is this the wrong way to pass data between classes and call methods?

Josh
  • 745
  • 1
  • 7
  • 22
  • did MainViewController still presenting when u calling that code? – Tj3n Nov 25 '15 at 06:14
  • @Tj3n I have tried calling the `startTrackingParking` with a delay and it hasn't helped? – Josh Nov 25 '15 at 06:16
  • 2
    How you are initializing your UI components? – aViNaSh Nov 25 '15 at 06:19
  • Check if you have connected the button outlet – Saheb Roy Nov 25 '15 at 06:20
  • 1
    maybe your `MainViewController` is not presenting the UI so its not updating.. – Tj3n Nov 25 '15 at 06:21
  • Yes, I have managed to get it to work by using `[NSNotificationCenter DefaultCenter]`, however I still want to know why this method doesn't work? I have made SURE everything is initialised and connected outlets. – Josh Nov 25 '15 at 06:25

2 Answers2

1

When you do this -

MainViewController *class = [[MainViewController alloc]init];
[class startTrackingParking:hourTrack :minuteTrack :secondTrack];

You are allocating a new instance of your MainViewController but not initialising it from a NIB or Storyboard, so it won't have any controls. This also means that it won't actually be presented on screen. You may have some other instance of this view controller that is being set up and presented (presumably it is the initial view controller in your storyboard since it is called MainViewController) but that is not this one.

Also, since this is a local variable it will be discarded as soon as that method exits.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
1

When you do :

MainViewController *class = [[MainViewController alloc]init];
[class startTrackingParking:hourTrack :minuteTrack :secondTrack];

you create an instance of the class MainViewController. The view which is associated with class is not being initialized.

View controllers load their views lazily. Accessing the view property for the first time loads or creates the view controller’s views. Instead, you can use UIStoryBoard method instantiateViewControllerWithIdentifier:

Have a look at the docs.

Sagar D
  • 2,588
  • 1
  • 18
  • 31
  • It is nothing to do with lazy loading (which UIViewController's don't do). If the UIViewController comes from a storyboard or NIB you need to use the appropriate method to create the VC. If it is set up programatically then you do this in the init method. – Paulw11 Nov 25 '15 at 07:57
  • @Paulw11 Please have a look in the "View Management" section : https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIViewController_Class/ – Sagar D Nov 25 '15 at 08:14
  • Ok, fair enough, but that still isn't the issue here because the OP hasn't correctly loaded the VC from a NIB or storyboard – Paulw11 Nov 25 '15 at 08:16