简体   繁体   English

ViewDidLoad()中的调用方法

[英]Call method in ViewDidLoad()

I'm calling a method on a key event which updates a label. 我正在调用一个更新标签的关键事件的方法。

    [self updateFolderText];

When I do this in the event method it works, but when I try to do this in the ViewDidLoad() I get a compiler error. 当我在事件方法中执行此操作时,它可以工作,但是当我尝试在ViewDidLoad()中执行此操作时,我收到编译器错误。

- (void)viewDidLoad
{
    [super viewDidLoad];     
    [self updateFolderText];
}

I get the following error: 我收到以下错误:

    error: Automatic Reference Counting Issue: Receiver type 'ViewController' 
for instance message does not declare a method with selector 'updateFolderText'

Ehm... I guess this must be trivial... I'm new to objective-c. 嗯......我想这一定是微不足道的......我是Objective-c的新手。 Thanks. 谢谢。 :) :)

The issue is probably because your method updateFolderText is defined below viewDidLoad . 问题可能是因为您的方法updateFolderText定义在viewDidLoad下面。 Move your code for updateFolderText below, and it should work. 移动下面的updateFolderText代码,它应该工作。

Or, you can declare the method explicitly in your class. 或者,您可以在类中明确声明该方法。 Add the following above @implementation . 添加以上@implementation

@interface className()
-(void)yourMethod; //not sure how youve fully define updateFolderText
@end

or, as you say, you can also define it in your header file: 或者,如您所说,您也可以在头文件中定义它:

-(void)yourMethod; //not sure how youve fully define updateFolderText

You need to make sure that you have declared the function updateFolderText in the .h of teh viewcontroller before the @end usually (for public functions) or for private functions you would declare it at the top of the .m with 您需要确保在@end通常(对于公共函数)之前已经在.h of viewcontroller中声明了函数updateFolderText,或者对于私有函数,您将在.m的顶部声明它。

@interface viewcontrollername()
-(void)updateFolderText;
@end

it was probably working else where in code because you have the function itself above where it gets called 它可能在代码中的其他地方工作,因为你在上面调用它的函数

Make sure your method is declared in the interface for your class. 确保在您的类的接口中声明您的方法。 There are two options for this. 有两种选择。 To declare it as a publicly accessible method for your class, you must put it in the header (.h) file. 要将其声明为类的公共可访问方法,必须将其放在标题(.h)文件中。

The declaration would look something like: 声明看起来像:

@interface myClass : UIViewController
- (void)updateFolderText;
@end

To declare is as a method that is only accessible within your class (well, not really, but effectively) then you can put this at the top of your .m file, before @implementation 声明是一种只能在你的类中访问的方法(好吧,不是真的,但有效),然后你可以把它放在.m文件的顶部,然后再实现@implementation

@interface myClass ()
- (void)updateFolderText;
@end

This second approach makes the method "private" as far as build time warnings are concerned. 就构建时间警告而言,第二种方法使该方法“私有”。 Note that actually the class will still respond to this selector at runtime though. 请注意,实际上该类仍将在运行时响应此选择器。

Make sure -(void)updateFolderText is defined, either in your .h file or in your private interface in the .m file. 确保-(void)updateFolderText已在.h文件或.m文件的私有界面中定义。 Also, if -(void)updateFolderText is physically located ABOVE the viewDidLoad method in your .m file, it should be able to see it ok. 此外,如果-(void)updateFolderText在物理上位于.m文件中的viewDidLoad方法-(void)updateFolderText ,它应该能够看到它。

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

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