views:

169

answers:

4

I am new to the iPhone development environment so be gentle:

Currently writing an iPhone game app that will also have a high score view. Was wanting to make this view so that it was on its own and I could call it from another class (.m file)

Question is, how do you call another class file? I know I need to include the header from that file, but how do I call a "function/message" from that class...something like updating the high score?

I hope this makes send. Thanks in advance for any and all help.

Geo...

A: 

If your function is static, call it like this:

[ClassName theFunction:parameter];

If your function is a member of the class, then create an instance of that class and call the function like this:

ClassName obj = [[ClassName alloc] init];
[obj theFunction:parameter];
Aaron
You generally do not want to be creating instances of views all willy-nilly.
Chuck
+3  A: 

You really should work your way through the introductory documentation on Apple's developer website: Learning Objective-C: A Primer and Your First iPhone Application

Mark Bessey
A: 

Don't think of it as calling functions/methods/procedures/whatever. Think of it as one object talking to another. What do they need to do this? You need a reference to the object you want to talk to.

Often, you'll want to create an instance variable that gets assigned to the object you're interested in. If both objects are in a nib together, you can draw connections between them in Interface Builder. Otherwise, you'll need to structure your code so that they can find each other (e.g., give the nib's owner a reference to whatever other object needs to talk to the view).

You might want to try working through one of the many tutorials out there (for instance, on Apple's developer site) to get a feeling for how this works.

Chuck
A: 

Hello,

The preferred technique for this would be delegation. So that your main view delegates the task of scoring to your HighScore view.

@protocol myDelegate;


@interface myClass : UIView {
id <myDelegate> delegate;
}

@property (nonatomic, assign) id <myDelegate> delegate;


@end

@protocol myDelegate
- (void)myMethod //Method to be implemented by your High Score View

To implement this protocol in your High Score View do:

@interface HighScore : UIview <myDelegate>

The in your HighScore.m implement the method mymethod:

- (void)myMethod {
//update Score etc...
}

To call the method from your other view do:

myHighScoreView.delegate = self // self is your other view
[delegate myMethod] // calls the method in the other view.

I hope that's clear.

-Oscar

OscarMk