UIViewController无法识别方法

时间:2011-06-28 08:15:46

标签: iphone objective-c cocoa-touch uiviewcontroller

请帮我解决此问题

我在名为firstviewcontroller的导航堆栈中有一个视图控制器

FirstViewController.h

@class ImperialPickerController;
@class FractionPickerController;
@class MetricPickerController;

@interface FirstViewController : UIViewController {

    UIView *pickerViewContainer;

    ImperialPickerController *ImperialPickerController;
    FractionPickerController *FractionPickerController;
    MetricPickerController *MetricPickerController;

    UIView *ImperialPickerViewContainer;
    UIView *FractionPickerViewContainer;
    UIView *MetricPickerViewContainer;

    UISegmentedControl *segmentedControl;

    NSInteger selectedUnit;
}

@property(nonatomic,retain) IBOutlet UIView *pickerViewContainer;

@property(nonatomic,retain) IBOutlet UIView *ImperialPickerViewContainer;
@property(nonatomic,retain) IBOutlet UIView *FractionPickerViewContainer;
@property(nonatomic,retain) IBOutlet UIView *MetricPickerViewContainer;

@property(nonatomic,retain) IBOutlet ImperialPickerController *ImperialPickerController;
@property(nonatomic,retain) IBOutlet FractionPickerController *FractionPickerController;
@property(nonatomic,retain) IBOutlet MetricPickerController *MetricPickerController;

@property(nonatomic,retain) IBOutlet UISegmentedControl *segmentedControl;

-(IBAction)toggleUnit;

@end

FirstViewController.m

@implementation FirstViewController

@synthesize ImperialPickerController;
@synthesize FractionPickerController;
@synthesize MetricPickerController;
@synthesize ImperialPickerViewContainer;
@synthesize FractionPickerViewContainer;
@synthesize MetricPickerViewContainer;
@synthesize pickerViewContainer;
@synthesize segmentedControl;

define METRIC_INDEX 0
define IMPERIAL_INDEX 1
define FRACTION_INDEX 2



-(IBAction)toggleUnit
{
    selectedUnit = [segmentedControl selectedSegmentIndex];

    if (selectedUnit == METRIC_INDEX)
    {
        [MetricPickerController updateLabel1];
    }

}
@end

MetricPickerController.h

@interface MetricPickerController : NSObject <UIPickerViewDataSource,UIPickerViewDelegate> {

    UIPickerView *pickerView;
    UILabel *label;
}
@property(nonatomic,retain)UIPickerView *pickerView;
@property(nonatomic,retain)UILabel *label;

-(void)updateLabel1;

@end

MetricPickerController.m

import "MetricPickerController.h"

@implementation MetricPickerController

@synthesize pickerView;
@synthesize label;


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{
    return 10;
}

-(void)updateLabel1
{
    label.text = @"test"
}

问题是我在firstviewcontroller中编译时收到错误消息

-(IBAction)toggleUnit
{
    selectedUnit = [segmentedControl selectedSegmentIndex];

    if (selectedUnit == METRIC_INDEX)
    {
        [MetricPickerController updateLabel1]; <<<<< (MetricPickerController might not respond to +updateLabel1)!!
also if i click the toggle in IB xcode will crash with sigbart error 
    }

任何人都可以请帮助并告知我做错了什么我认为我已经把所有东西都搞定了所以我想这与我的方法声明不知何故

我知道代码在这个阶段是不完整的,但它让我疯狂试图摆脱这个错误,我希望你能体会到我只是一个学习者

}

1 个答案:

答案 0 :(得分:5)

问题是updateLabel1是一个实例方法,并且您要将它发送到一个类(错误消息中的+updateLabel1而不是-updateLabel1告诉您这个。)

由于您已将实例变量命名为与类相同,因此您应该能够通过编写

来解决此问题。
[self.MetricPickerController updateLabel1];

相反 - 这使编译器清楚地指出实例变量而不是实际的类。