我在使用tag属性访问UIButton时遇到了一些麻烦

UIButton   *randomButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect ]];
    randomButton.frame = CGRectMake(205, 145, 90, 22); // size and position of button
    [randomButton setTitle:@"Random" forState:UIControlStateNormal];
    randomButton.backgroundColor = [UIColor clearColor];
    randomButton.adjustsImageWhenHighlighted = YES;
    [randomButton addTarget:self action:@selector(getrandom:)
           forControlEvents:UIControlEventTouchUpInside];
    randomButton.reversesTitleShadowWhenHighlighted=YES;
    randomButton.toggleButton

    [self.view addSubview:randomButton];

    randomButton.tag=333;


然后稍后在代码中,我尝试按以下方式操作按钮,这给了我一个错误:


  不兼容的Objective-C类型
  初始化“ struct UIView *”,
  预期的'struct UIButton *'


UIButton *random = [self.view viewWithTag:333];
    random.highlighted=NO;

最佳答案

尝试:

UIButton *random = (UIButton *)[self.view viewWithTag:333];


另外,为什么在释放按钮后分配标签?

08-05 23:54