Xcode 错误消息 - ViewController 没有可见的 @interface 声明了选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11551984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:56:12  来源:igfitidea点击:

Xcode Error Message - No visible @interface for ViewController declares the selector

iphoneobjective-ciosxcodeuibutton

提问by Paul Morris

I have a very simple method that allows me to specify the name of a UIButton and then set its title. I previously had the method in my main ViewController which worked fine. But then I have decided to create a new file called CustomMethods.h/m and put all of my methods in there.

我有一个非常简单的方法,它允许我指定 UIButton 的名称,然后设置其标题。我以前在我的主 ViewController 中有这个方法,它运行良好。但后来我决定创建一个名为 CustomMethods.h/m 的新文件并将我所有的方法放在那里。

As soon as I have moved the method across and #import the header into the main view controller I am getting the error message below

一旦我移动了方法并将标题 #import 到主视图控制器中,我就会收到以下错误消息

No visible @interface for ViewController declares the selector 'SetTitleOfButtonNamed:withTitle:'

In my CustomMethods file I have created my method as follows:

在我的 CustomMethods 文件中,我按如下方式创建了我的方法:

-(void)setTitleOfButtonNamed:(UIButton *)button withTitle:(NSString *)buttonTitle
{
    [button setTitle:buttonTitle forState:(UIControlStateNormal)];
}

In the viewDidLoad of the main ViewController I am trying to call this method and set the button title as follows:

在主 ViewController 的 viewDidLoad 中,我尝试调用此方法并按如下方式设置按钮标题:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *btnOneTitle = @"Button 1";
    [self setTitleOfButtonNamed:buttonOne withTitle:btnOneTitle]; // ERROR OCCURS HERE
}

Before I copied the method into its own file it worked perfectly. Any ideas?

在我将该方法复制到它自己的文件中之前,它运行良好。有任何想法吗?

回答by Gambit

You are still calling setTitleOfButtonNamed on "self" which is the ViewController. You need to call it from the CustomMethods class that implements the method now.

您仍然在 ViewController 的“self”上调用 setTitleOfButtonNamed。您现在需要从实现该方法的 CustomMethods 类调用它。

[self setTitleOfButtonNamed:buttonOne withTitle:btnOneTitle];