0

I determined I need my button action (findMe) to be a part of viewDidAppear and cannot have it as an individual IBAction because it has showsUserLocation errors.

A: is this even possible?

B: does it make any sense to do that?

C: What would it look like?

My current code which I know is WRONG looks like this:

-(void)viewDidAppear:(BOOL)animated
{
  if (findMe == YES)
  {
    self.mapView.showsUserLocation = YES;
    [mapView setCenterCoordinate:mapView.userLocation.coordinate animated:YES];
  }
}

Thanks!

Greg
  • 276
  • 1
  • 3
  • 25

1 Answers1

1

Your question is very confusingly phrased and incomplete (please include code that has errors and the exact error message(s)), but I'll answer the three things I think you might be asking about.

If your problem is that you want to have the same code in both methods, extract the common part of the two methods into a third method, and have them both call that.

- (IBAction) button {
    stuff
    [self commonPart];
}

- (void) viewDidAppear {
    stuff
    [self commonPart];
}

- (void) commonPart {
    stuff they both do
}

If your problem is that you need to know something about the state of your UI in your viewDidAppear method, you can use an IBOutlet to reference the controls in question.

- (void) viewDidAppear {
    if (self.findMeButton.someThingYouWantToCheck) {
          self.mapView.showsUserLocation = YES;
          [mapView setCenterCoordinate:mapView.userLocation.coordinate animated:YES];
    }
}

If your problem is that you need to store whether a button has been pressed so that you can do different actions based on it later, just add a BOOL instance variable to your object and set it to YES in the button pressed IBAction.

Were any of those what you were trying to do?

Catfish_Man
  • 41,261
  • 11
  • 67
  • 84
  • Yes, I think the latter of your answer is what I want to do. I'm a beginner, sorry. I want the button to execute "showsUserLocation" inside of the viewDidAppear at any given time the user pushes the button. So, I don't want to store whether it's been pressed, i just want it to react immediately on press. Sorry if I confused you! – Greg Oct 30 '12 at 01:09
  • You can't execute part of one method inside another. You can either move that code to a separate method like my first example showed, or copy-paste it. – Catfish_Man Oct 30 '12 at 01:10
  • Just want to thank you. The first part solved it for me and I accomplished what I wanted. :) – Greg Oct 30 '12 at 02:50