iPhone toggle button implementation

前端 未结 7 1942
孤独总比滥情好
孤独总比滥情好 2021-02-04 09:05

I would like to create a toggle button in my iPhone application. However, I don\'t know exactly what would be the best approach for this.

I am considering two options.

相关标签:
7条回答
  • 2021-02-04 09:11

    I too looked at UISwitch, but in my case I do not want an on/off button, I really want a toggle button. What I am trying to do, again in my case, is have a block of buttons that look like a UISegmentedControl, but where the user can "toggle" more than one of the buttons on the control at one time. The issue I am trying to solve is that I have VERY limited screen realestate and a lot of toggles to get on the screen.

    My fall-back is probably going to be a UITableView full of toggle cells. This way I can add as many toggles as I want in a fixed, scrollable area of the screen.

    Sadly, UISwitch does not always fit the UI specs people have in mind, so I am guess the gentleman that posted the original post was not looking for an On/Off switch either, but rather wanted a button that changed its state somehow.

    0 讨论(0)
  • 2021-02-04 09:13

    Just make an (IBAction) method and also a BOOL. Set the BOOL default to FALSE. Your IBAction must look like this:

    - (IBAction) .... {
    
        if (bool) {
            // do your things
        }
    
        else {
            // do your things
        }
    
        bool =! bool
    }
    

    That works for me!

    0 讨论(0)
  • 2021-02-04 09:13

    I need to do this too. Wouldn't setting the enabled state to NO prevent the button being toggled back though?

    UPDATE

    People are suggesting UISwitch but can this be skinned with all the relevant up,over,disabled,selectedUp,SelectedOver,selectedDisabled states?

    0 讨论(0)
  • 2021-02-04 09:17
    -(IBAction)toggle{
        if (button.selected == NO) {
            button.selected = YES;
    
        }
        else {
            button.selected = NO;
        }
    }
    

    or

    -(IBAction)toggle:(UIButton*)sender
    {
    sender.selected = !sender.selected;
    }
    

    simple dude

    0 讨论(0)
  • 2021-02-04 09:21

    Isn't there already a toggle button, or am I missing something here?

    EDIT: As others have said, it's called UISwitch, and it toggles on and off.

    0 讨论(0)
  • 2021-02-04 09:33

    May be i dont understand the question properly ..why are you not using UISwitch

    0 讨论(0)
提交回复
热议问题