Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C custom class methods not being called

So I have this custom class with just a test method that does nslog. I am going to reuse this method many times in my app. The interface looks like this.

@interface TestViewController: UIViewController {  CMImageMover * imageMover }

Then in the view did load I:

imageMover = [[CmImageMover alloc] init];

If I do:

[imageMover testMethod];

Right after the alloc and init it works in the viewDidLoad function but if I call it again from another function in the view controller nothing works and the class method does not get called.

What am I doing wrong here. Every other var I declare like NSArray/NSTimer, I do the say way and I am able to access and use it throughout my controller.

like image 720
Mike007 Avatar asked May 30 '26 18:05

Mike007


1 Answers

When you say "if I call it again from another function in the view controller nothing works" then first thing to check is what you are sending the testMethod. It could be nil, in which case nothing will happen. In objective C sending a message to nil does nothing. Add an NSLog to find out, e.g.

 NSLog(@"imageMover object is: %@", imageOver);
 [imageMover testMethod]; 

If the NSLog shows it is nil - or something crazy - then follow up what you are doing with the imageMover ivar.

You mention a class method in your question, but don't refer to it in your code snippets.

If you have defined testMethod as a class method it will, of course, fail if you send that message to an instance. (And it will fail noisily.) A class method would be introduced like this:

 + (void) testMethod
 {
       NSLog(@"CMImageMover testMethod called on Class");
 }

An instance method would be introduced like this:

 - (void) testMethod
 {
       NSLog(@"testMethod called on an instance of CMImageMover");
 }

Apologies if this is all screamingly obvious to you and missing the point of the question. It's not that clear from your question where the issue lies.

like image 74
Obliquely Avatar answered Jun 01 '26 09:06

Obliquely



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!