如何选择Uibutton

Ron*_*Ron 4 iphone objective-c uibutton ios

UIButton当我选择那个按钮时,我仍然选择了一个.这是我的代码:

- (IBAction)cloudclick:(UIButton *)sender {
    UIImage *bgImage = [UIImage imageNamed:@"black_cloud.png"];
    UIButton *tmpButton = (UIButton *)[self.view viewWithTag:sender.tag];
    [tmpButton setBackgroundImage:bgImage forState:UIControlStateSelected];

} 
Run Code Online (Sandbox Code Playgroud)

但是当我点击按钮时它不会保持选中状态.它会更改背景,但不会保留为选定按钮.

Har*_*ala 12

你必须在你的代码之后写一行

- (IBAction)cloudclick:(UIButton *)sender {
    ...
    [tmpButton setSelected:YES];
}
Run Code Online (Sandbox Code Playgroud)


Ish*_*shu 5

使用这一行代码

sender.selected=!sender.selected;
Run Code Online (Sandbox Code Playgroud)

并在时间分配中为diff状态设置不同的图像(如果您通过代码制作按钮),否则从xib设置图像。