简体   繁体   English

无法从其他类调用实例方法

[英]Cant call instance methods from other classes

I hope you can help me with the following problem in Objective-C. 希望您能为我解决Objective-C中的以下问题。 I´ve been sitting around for two hours now, but I have no idea what´s wrong. 我已经坐了两个小时了,但是我不知道怎么了。

Here´s my code: 这是我的代码:

BMICalc.h: BMICalc.h:

#import <UIKit/UIKit.h>

@interface BMICalc : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

@property (strong, nonatomic) NSMutableArray *bmiArray;

-(int)bmiRows;

@end

BMICalc.m: BMICalc.m:

#import "BMICalc.h"

@implementation BMICalc

@synthesize bmiArray;

- (int) bmiRows
{
    if (!bmiArray)
    {
      bmiArray = [[NSMutableArray alloc] init];
    }
    return [bmiArray count];
}

@end

Now I want to create a object of BMICalc in another class called BMIDiaryController: 现在,我想在另一个名为BMIDiaryController的类中创建BMICalc对象:

BMIDiaryController.m: BMIDiaryController.m:

#import "BMIDiaryController.h"
#import "BMICalc.h"

@interface BMIDiaryController ()

@end

@implementation BMIDiaryController

- (void)viewDidLoad
{
    [super viewDidLoad];

    BMICalc *bmiSender = [[BMICalc alloc] init];

    int bmiVariable = [bmiSender bmiRows];       
}

@end

So when I start the programme i get the following error: "No visible @interface for 'BMICalc' declares the selector 'bmiRows' 因此,当我启动程序时,出现以下错误:“'BMICalc'的无可见@interface声明了选择器'bmiRows'

I can´t call the method "bmiRows" with the object "bmiSender" (both of Class "BMICalc" 我无法使用对象“ bmiSender”(均为“ BMICalc”类)调用方法“ bmiRows”

I hope you can help me with this. 希望您能帮到我。 I searched and searched...I think it´s just a small thing, but I won´t see it... 我搜寻了...我认为这只是一件小事,但我看不到...

Thank you and regards, 谢谢你,谢谢

Stefan 斯特凡

Try creating a new project. 尝试创建一个新项目。 Then add the BMICalc.m and BMICalc.h files. 然后添加BMICalc.m和BMICalc.h文件。 Try to instantiate your BMICalc object and call the bmiRows method from the appDelegate just to test it. 尝试实例化您的BMICalc对象,并从appDelegate调用bmiRows方法只是为了对其进行测试。 There could be any number of things wrong in your main project that may be interfering with the class. 您的主项目中可能有许多错误可能会干扰该类。 At least doing this will confirm that you aren't crazy and that this code isn't the problem :) 至少这样做会确认您没有疯,并且此代码不是问题:)

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

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