简体   繁体   English

Objective C自定义类方法未被调用

[英]Objective C custom class methods not being called

So I have this custom class with just a test method that does nslog. 所以我有这个自定义类只有一个测试方法,它执行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: 然后在视图中加载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. 在alloc和init之后,它在viewDidLoad函数中工作,但是如果我从视图控制器中的另一个函数再次调用它,则没有任何工作,并且不会调用类方法。

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. 每个其他var我声明像NSArray / NSTimer,我说的方式,我能够访问并在我的控制器中使用它。

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 . 当你说“如果我从视图控制器中的另一个函数再次调用它没有任何作用”时,首先检查的是你发送testMethod It could be nil, in which case nothing will happen. 它可能是零,在这种情况下不会发生任何事情。 In objective C sending a message to nil does nothing. 在目标C中发送消息给nil什么都不做。 Add an NSLog to find out, eg 添加NSLog以查找,例如

 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. 如果NSLog显示它是零 - 或者说是疯狂的东西 - 那么用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. 如果您已将testMethod定义为类方法,那么当您将该消息发送到实例时,它将失败。 (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. 问题所在的问题并不清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM