getting at a UIButton with the tag property IPhone

限于喜欢 提交于 2019-12-21 16:19:45

问题


I'm having a little trouble using the tag property to access a 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;

Then later on in code I try to get at the button in the following manner which gives me an error saying

Incompatible Objective-C types initializing 'struct UIView *', expected 'struct UIButton *'

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

回答1:


Try:

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

Also, why are you assigning the tag after you have released the button?



来源:https://stackoverflow.com/questions/2802802/getting-at-a-uibutton-with-the-tag-property-iphone

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