Hello all,

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. I've spent a few hours trying to figure this out...

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 (so far) for the 'buttonPressed' method.

-(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;
}

Thanks in advance for the help.

I presume you have solved this by now, but you need to shift the declaration of the object from the button press event to the .h file. Also declare it as a property if you want it a publically accessible object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.