Undeclared identifier “viewDidLoad”

最后都变了- 提交于 2019-12-13 05:13:24

问题


    //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

Here is the updated code, some before, and everything after. There's a lot more to the program but it is all just setting up different visual elements for a GUI. The error is tagged on the line that sais..

-(void)viewDidLoad {

And the error sais this..

Use of undeclared identifier 'viewDidLoad'

Would you like more info??


回答1:


you cannot have method inside another method.

this is part of your code

    //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

change it to

    //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];
}


来源:https://stackoverflow.com/questions/21998365/undeclared-identifier-viewdidload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!