【问题标题】:UIViewController does not recognize methodUIViewController 无法识别方法
【发布时间】:2011-06-28 08:15:46
【问题描述】:

请帮我解决这个问题

我在名为 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 
    }

任何人都可以请帮助并建议我做错了什么我认为我已经正确连接了所有内容,所以我想这与我的方法声明有关

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

}

【问题讨论】:

  • 见下面阿德里安的回答。我建议一个好的命名方案是对实例变量使用小写的第一个字母,对类名使用大写,这将有助于将来解决此问题。
  • @Nick Bull 100% 与您同在。命名非常重要,而且确实有助于减少错误。

标签: iphone objective-c cocoa-touch uiviewcontroller


【解决方案1】:

问题在于updateLabel1 是一个实例方法,而您将其发送到一个类(错误消息中的+updateLabel1 而非-updateLabel1 告诉您这一点)。

由于您已将实例变量命名为与类相同,因此您应该可以通过编写来解决这个问题

[self.MetricPickerController updateLabel1];

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

【讨论】:

  • +1 采用平台的coding conventions 会是不错的选择。
  • 同意,应该补充一点,ta。
  • 可能链接(不重复)的问题可以是stackoverflow.com/questions/1053592/…
  • 嗨,阿德里安,感谢您的支持和及时的回答,我刚刚插入了建议的修复程序,它起作用了——多年来我一直在摸索这件事真是一种解脱。
  • 嗨,deepak 感谢您对编码约定的评论,我想我需要更仔细地考虑这些事情,而不是仅仅向前迈进,犯一些基本的错误。这就是你边做边学的问题,我想周围没有人指导你。我相信随着时间的推移我会变得更加熟练 - 同时我会回溯并尝试纠正我所犯的命名约定错误
猜你喜欢
  • 1970-01-01
  • 2021-11-21
  • 2016-04-19
  • 2017-07-24
  • 2015-09-02
  • 2016-07-29
  • 2018-08-29
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多