【问题标题】:Undeclared identifier "viewDidLoad"未声明的标识符“viewDidLoad”
【发布时间】:2014-03-26 17:47:49
【问题描述】:
    //Objects list
    //objects scroll
    UIScrollView *objects;
    objects=[[UIScrollView alloc]initWithFrame:CGRectMake(0,80,512,688)];
    objects.showsVerticalScrollIndicator=YES;
    objects.scrollEnabled=YES;
    objects.userInteractionEnabled=YES;
    objects.backgroundColor = [UIColor clearColor];
    [self.view addSubview:objects];
    [objects setUserInteractionEnabled:YES];
    [objects setCanCancelContentTouches:YES];
    objects.contentSize = CGSizeMake(512,689);

    //divider
    UIButton *divider = [UIButton buttonWithType:UIButtonTypeCustom];
    [divider setBackgroundImage:[UIImage imageNamed:@"white.png"]forState:UIControlStateNormal];
    divider.frame = CGRectMake(300, 0, 1, 768);
    [divider setTitle:@"" forState:UIControlStateNormal];
    [self.view addSubview:divider];
    divider.adjustsImageWhenHighlighted = NO;
    //array colors
    UIImage *color[3000];

    //color setup
    color[0] = [UIImage imageNamed:@"item.png"];
    UIImageView *originalImageView = [[UIImageView alloc] initWithImage:color[0]];
    [originalImageView setFrame:CGRectMake(300, 0, 212, 62)];
    [objects addSubview:originalImageView];

    //array buttons
    UIImageView *button[3000];

    //button setup
    button[0] = [[UIView alloc] initWithFrame:[originalImageView frame]];
    UIImageView *maskImageView = [[UIImageView alloc] initWithImage:color[0]];
    [maskImageView setFrame:[button[0] bounds]];
    [[button[0] layer] setMask:[maskImageView layer]];
    button[0].backgroundColor = [UIColor blueColor];
    [objects addSubview:button[0]];

    //add object button
    UIButton *plus = [UIButton buttonWithType:UIButtonTypeCustom];
    [plus setBackgroundImage:[UIImage imageNamed:@"plus.png"]forState:UIControlStateNormal];
    plus.frame = CGRectMake(394, 106, 25, 25);
    [plus setTitle:@"" forState:UIControlStateNormal];
    [objects addSubview:plus];
    plus.adjustsImageWhenHighlighted = YES;


    -(void)viewDidLoad {
        [super viewDidLoad];
        UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [add addTarget:self action:@selector(aMethod:)forControlEvents:UIControlEventTouchDown];
        [add setTitle:@"add new" forState:UIControlStateNormal];
        add.frame = CGRectMake(100, 100, 100, 100);
        [self.view addSubview:add];
    }
    - (void) aMethod:(id)sender {
        button[0].backgroundColor = [UIColor greenColor];
    }

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

这是更新后的代码,一些是之前的,而所有的都是之后的。该程序还有很多其他功能,但它只是为 GUI 设置不同的视觉元素。错误标记在 sais.. 的行上。

-(void)viewDidLoad {

错误是这样的..

Use of undeclared identifier 'viewDidLoad'

您想了解更多信息吗?

【问题讨论】:

  • 这是在哪个班级? viewDidLoad 是 UIViewController 上的一个方法,所以你的类需要是 UIViewController 的一个实例或一个的子类。我认为您的问题没有提供足够的信息。
  • 如果能回答你的问题,它就在 viewcontroller.m 文件中
  • @AndrewSavage,Dave 在您的 viewcontroller.h @interface ViewController : UIViewController 中谈到了继承
  • 哦,这就是我的 .h 文件中的内容
  • 发布完整的错误消息和错误周围的代码,包括前面的行。很可能您缺少分号或其他内容

标签: ios methods viewdidload


【解决方案1】:

你不能在另一个方法中拥有方法。

这是您的代码的一部分

    //add object button
    UIButton *plus = [UIButton buttonWithType:UIButtonTypeCustom];
    [plus setBackgroundImage:[UIImage imageNamed:@"plus.png"]forState:UIControlStateNormal];
    plus.frame = CGRectMake(394, 106, 25, 25);
    [plus setTitle:@"" forState:UIControlStateNormal];
    [objects addSubview:plus];
    plus.adjustsImageWhenHighlighted = YES;


    -(void)viewDidLoad {
        [super viewDidLoad];
        UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [add addTarget:self action:@selector(aMethod:)forControlEvents:UIControlEventTouchDown];
        [add setTitle:@"add new" forState:UIControlStateNormal];
        add.frame = CGRectMake(100, 100, 100, 100);
        [self.view addSubview:add];
    }
    - (void) aMethod:(id)sender {
        button[0].backgroundColor = [UIColor greenColor];
    }

} // <----this should not be here

改成

    //add object button
    UIButton *plus = [UIButton buttonWithType:UIButtonTypeCustom];
    [plus setBackgroundImage:[UIImage imageNamed:@"plus.png"]forState:UIControlStateNormal];
    plus.frame = CGRectMake(394, 106, 25, 25);
    [plus setTitle:@"" forState:UIControlStateNormal];
    [objects addSubview:plus];
    plus.adjustsImageWhenHighlighted = YES;

} // <--- should be here to end method

-(void)viewDidLoad {
    [super viewDidLoad];
    UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [add addTarget:self action:@selector(aMethod:)forControlEvents:UIControlEventTouchDown];
    [add setTitle:@"add new" forState:UIControlStateNormal];
    add.frame = CGRectMake(100, 100, 100, 100);
    [self.view addSubview:add];
}
- (void) aMethod:(id)sender {
    button[0].backgroundColor = [UIColor greenColor];
}

【讨论】:

  • 非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-30
  • 1970-01-01
  • 2021-06-07
  • 2016-04-09
  • 2020-12-09
  • 2016-04-22
相关资源
最近更新 更多