在UIButton中实现切换功能

时间:2010-11-17 19:34:08

标签: objective-c ios cocoa-touch uiimageview uibutton

我想知道如何将切换功能添加到UIButton,类似于用户点击一个未选中的按钮,按钮被选中并保持选中状态,直到用户再次点击它为止,就像之前一样取消选择

我正在考虑制作一个IBAction,将其从未选中更改为选中,我该怎么做?

继承我尝试的内容:

-(IBAction)toggle {

    //Toggle on implementation.

        button.selected = YES;
        button.highlighted = NO;
        button.enabled = YES;

    //Toggle off implementation.

    if (button.highlighted == YES) {

        button.selected = NO;
        button.highlighted = YES;
        button.enabled = NO;
    }
}

...问题

-(IBAction)toggleFav {

    if (favButton == nil) {

        UIImage *unselectedImage = [UIImage imageNamed:@"favUntapped.png"];
        UIImage *selectedImage = [UIImage imageNamed:@"favTapped.png"];
        [favButton setImage:unselectedImage forState:UIControlStateNormal];
        [favButton setImage:selectedImage forState:UIControlStateSelected];
        [favButton setFrame:CGRectMake(0, 0, 40, 40)];
    }       

    if([favButton isSelected]){

        //Add to menu.
        [favButton setSelected:NO];
    } else {

        //Remove from menu.

        [favButton setSelected:YES];
    }
}

4 个答案:

答案 0 :(得分:11)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIImage *unselectedImage = [UIImage imageNamed:@"unselected.png"];
        UIImage *selectedImage = [UIImage imageNamed:@"selected.png"];
        UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
        [b setBackgroundImage:unselectedImage forState:UIControlStateNormal];
        [b setBackgroundImage:selectedImage forState:UIControlStateSelected];
        [b addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [b setFrame:CGRectMake(0, 0, 40, 40)];
        [cell.contentView addSubview:b];
    }       
    return cell;
}

-(void) buttonPressed:(UIButton *)sender
{
    if([sender isSelected]){
        //...
        [sender setSelected:NO];
    } else {
        //...    
        [sender setSelected:YES];
    }

}

您的toggleFav代码没有多大意义 如果if (favButton == nil) {存在,则favButton检查。但是如果你将它与IB连接起来,它应该始终存在于那里。如果不是,那么按钮如何调用此方法?所以这样做:

-(void)viewDidLoad
{
    //....
    UIImage *unselectedImage = [UIImage imageNamed:@"favUntapped.png"];
    UIImage *selectedImage = [UIImage imageNamed:@"favTapped.png"];
    [favButton setImage:unselectedImage forState:UIControlStateNormal];
    [favButton setImage:selectedImage forState:UIControlStateSelected];
    [favButton setFrame:CGRectMake(0, 0, 40, 40)];
    //....
}


-(IBAction)toggleFav:(UIButton *)sender {
    if([sender isSelected]){
        //...
        [sender setSelected:NO];
    } else {
        //...    
        [sender setSelected:YES];
    }
}

Here你会找到一个带有DetaiView的示例项目,它包含一个具有2个状态的按钮。
注意:我正在保存NSUserDefaults中选择了哪个按钮的信息。你不应该这样做。相反,您需要将其保存在模型中。但由于我没有关于你的模型的信息,我只是使用NSUserDefaults。

答案 1 :(得分:2)

-(void)hitButton:(UIButton*)button
{
    buttonOnFlag = !buttonOnFlag;
    if( buttonFlag )
        [self performSelector:@selector(setHighlight:) withObject:button afterDelay:0];
}

- (void)setHighlight:(UIButton*)button 
{
    button.highlighted = true;
}

答案 2 :(得分:0)

使用button.highlighted属性

答案 3 :(得分:0)

你应该像这样编码:

-(IBAction)toggle:(id)sender {

    //Toggle on implementation.
   if (sender.highlighted == NO)
     {
        sender.selected = YES;
        sender.highlighted = NO;
        sender.enabled = YES;
     }

    //Toggle off implementation.

   else{

        sender.selected = NO;
        sender.highlighted = YES;
        sender.enabled = NO;
    }
}