在 UIButton 中实现切换功能

发布于 2024-10-02 14:59:02 字数 1160 浏览 4 评论 0原文

我想知道如何向 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];
    }
}

I want to know how to add the toggling feature to a UIButton, something like the user taps a unselected button the button becomes selected and stays selected till the user taps it again there by making unselected like it was before.

I was thinking of making an IBAction which changes it from unselected to selected, how can I do that?

Heres what I tried:

-(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;
    }
}

Problem...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌路黄昏 2024-10-09 14:59:02
- (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];
    }
}

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

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

}

Your toggleFav code doesn't make much sense.
if (favButton == nil) { checks, if favButton is present. But if you are wiring it up with IB, it should always been there at that point. And if it wasn't how could the button call this method? So do it like this:

-(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 you'll find an example project, with a DetaiView, that holds a Button with the 2 states.
Note: I am saving the information of what button was selected in the NSUserDefaults. You should not do that. Instead you'll want to save it in the model. But as I dont have informations on your model, I am just using NSUserDefaults.

记忆里有你的影子 2024-10-09 14:59:02
-(void)hitButton:(UIButton*)button
{
    buttonOnFlag = !buttonOnFlag;
    if( buttonFlag )
        [self performSelector:@selector(setHighlight:) withObject:button afterDelay:0];
}

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

- (void)setHighlight:(UIButton*)button 
{
    button.highlighted = true;
}
我不吻晚风 2024-10-09 14:59:02

使用button.highlighted属性

Use button.highlighted property

请远离我 2024-10-09 14:59:02

你应该这样编码:

-(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;
    }
}

You should code like this:

-(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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文