Apple iPhone UIKit Buttons

UIKit ButtonsHaving just spent a day or so working out how to get the iPhone’s UIKit push buttons to work, and connected to the Devicescape application, I thought I’d share what I had discovered.

I ended up using UIPushButton class for my buttons, though since this is the base class for a lot of the other button types almost everything here applies to those too.


Creating The Buttons
I wrote a short method for my class that created the buttons for me since they are all basically the same, only the images change. Here’s what I ended up with for that function:

 - (id) createButton:(NSString *)name
{
        UIPushButton *button =
                [[UIPushButton alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 100.0f, 60.0f)];
        NSString *onFile = [NSString
                stringWithFormat:@"/Applications/Devicescape.app/%@-btn-pressed.png", name];
        UIImage* on = [[UIImage alloc] initWithContentsOfFile: onFile];
        [button setImage:on forState:1];
        NSString *offFile = [NSString
                stringWithFormat:@"/Applications/Devicescape.app/%@-btn.png", name];
        UIImage* off = [[UIImage alloc] initWithContentsOfFile: offFile];
        [button setImage:off forState:0];
        [button setEnabled:YES];
        [button setDrawContentsCentered:YES];
        [button setAutosizesToFit:NO];
        [button setNeedsDisplay];
        [button addTarget:self action:@selector(buttonEvent:) forEvents:255];
        return button;
}

What does all of that do? Well, the first parts set up the two images for the pressed and unpressed states of the button. I couldn’t get the background images to do anything for me, so I ended up just making images for each button with the text I wanted on them. Then we set up some basic properties for the buttons. Finally, the most important part, registers the current class to receive actions from the button. I don’t know the values for the forEvents mask, so I put in 255. That seems to cover mouse up and down events, which are all I really care about for a push button.

The other thing to note here is that I initialise the button with a rectangle. In my main code though I change that to place the button where I really want it:

        _login = [self createButton:@"login"];
        [_login setFrame:CGRectMake(5.0f, 0.0f, 100.0f, 60.0f)];
        [barView addSubview: _login];

Handling Button Pushes

In the last section we attached a handler method (by means of a selector). That method looks like this:

- (void) buttonEvent:(UIPushButton *)button
{
        if ([button isPressed]) {
                if (button == _logout) {
                        /* Logout stuff here ... */
                } else if (button == _login) {
                        /* Login stuff here ... */
                } else if (button == _settings) {
                        /* Settings stuff here ... */
                }
        }
}

The method is called with a reference to the button that was pressed as its argument. We can use the isPressed method to determine whether the user is currently pressing the button or not, and if so do whatever we need to do.

4 thoughts on “Apple iPhone UIKit Buttons

  1. Thank you!!!!

    ——————-
    – (void) buttonEvent:(UIPushButton *)button
    {
    if ([button isPressed]) {
    if (button == _logout) {
    /* Logout stuff here … */
    } else if (button == _login) {
    /* Login stuff here … */
    } else if (button == _settings) {
    /* Settings stuff here … */
    }
    }
    }
    ——————————-

    SOOO SIMPLE!!! 😀 😀 😀

  2. I purchased a used iphone,i was trying to upgrade to 3.1.3. After that my phone saying the sim card inserted in this iphone does not appear to be supported.
    Now i cannot access the iphone at all, can only see a screen that shows the itunes logo and the docking cable.
    i can make, receive calls. Is here any wayi can re-open my phone.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.