R
R
Reikyavik2014-05-04 01:24:06
Objective-C
Reikyavik, 2014-05-04 01:24:06

Objective-C. How to call a method from another class?

Hello! I'm just starting to learn Objective C and understand the basics of OOP.
Actually, the question is:
there is a view controller ViewController.
In the interface of the ViewController.h file, the resumeGame and pauseGame methods are declared:

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIView *viewRocket;
@property (weak, nonatomic) IBOutlet UIView *viewBall;
@property (weak, nonatomic) IBOutlet UIView *viewBucket;
@property (weak, nonatomic) IBOutlet UILabel *viewScore;

- (void)resumeGame;
- (void)pauseGame;

@end

There is an AppDelegate class, and the pauseGame and resumeGame calls from the ViewController must be placed on the device lock and unlock events.
- (void)applicationWillResignActive:(UIApplication *)application
{
    //Здесь нужен вызов pauseGame  
}

I import the file #import "ViewController.h", then in the applicationWillResignActive method I write a [ViewController pauseGame] call, which I get the error No known class method for selector "pauseGame".
Tried instantiating the class:
ViewController* object = [[ViewController alloc] init];
and call
[object pauseGame];
This is how it works: the build compiles, but incorrectly, and swears at some kind of BAD_ACCESS (I think something is related to memory allocation).
It seems that there is some simple way and it is on the surface, but I just can’t find it.
I ask for help and, please, if possible, describe in more detail how to correctly declare a method in one class, how to correctly make a call in another?
PS I hope I didn't mess up anything in terminology, I'm just learning. Sorry if the question has already been raised.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
An, 2014-05-04
@Reikyavik

not everything is so simple and depends on the "architecture". If you used standard templates when creating the project, then you have UINavigationController
available, or the UIViewController itself is available. More precisely, it can only be said if I see the code of the
application:didFinishLaunchingWithOptions method:
In this method, you need to save this UIViewController into a variable that will be declared in the AppDelegate and later call it in these methods.
In general, your task is solved more simply
in the viewDidLoad method of the ViewController class
at the end add

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(pauseTheTimer:)
                                             name:UIApplicationWillResignActiveNotification
                                           object:nil];

stackoverflow.com/questions/9247654/iphone-applic...
plus add method
-(void) pauseTheTimer:(NSNotifycation*)notify{
   [self pauseGame];
}

You should also read about the work of NSNotificationCenter and the observer pattern (observer)

E
Evgeny Elchev, 2014-05-04
@rsi

habrahabr.ru/post/103221
In short, then - className methodName and import the required class

M
MagoVinch, 2014-05-04
@MagoVinch

Implement a singleton and easily call functions from it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question