I’m new to programming in Objective-C and have some C++ under my belt. As one of my first programs, I’m trying to make a fairly simple app that can record the user’s heart rate.
I made a class to store all of this information in (called HeartRate), from which I want to make an object. However, I am having issues with the scope and initializing the object. I am trying to initialize it within a method called ‘buttonPressed’, but the object is not accessible outside of this function.
I looked into using the AppDelgate, but didn’t have much luck – like I mentioned, I’m a beginner with Objective-C, so it could probably be done, but I did not know how to do it.
Where can I safely initialize this so that it would be accessible to other functions?
If you guys want me to post any code or explain in more detail, I would be glad to.
Here’s the code for the IBAction:
-(IBAction)buttonPressed:(id)sender{
HeartRate *HR = [[HeartRate alloc]init];
int beats = [HR getBeats];
[HR setBeats:(beats+1)];
NSString *outputBeats = [NSString stringWithFormat: @"%i", [HR getBeats]];
beatsText_.text = outputBeats;
Declare the object as an instance variable in order to access it anywhere inside the class that contains HeartRate: