开发者

Keeping UIButton highlighted

Hey! I have a xib file were i would like a round rect button to stay highlighted after it is pressed. I also would like to have a different button that is pressed after the first one that takes you to the next page. How can I do this. Some code would be greatly appreciated!

This is my .h:

#import <UIKit/UIKit.h>

@interface Test1ViewController : UIViewController {

    IBOutlet UIButton *button1;
    IBOutlet UIButton *button2; 


}

-(IBAction) buttonPressed:(id)sender;
-(IBAction) secondButtonPressed:(id)sender;
- (void)flipButton;


@end

This is my .m:

#import "Test1ViewController.h"
#import "Page2.h"

@implementation Test1ViewController

-(IBAction)buttonPressed:(id)sender
{
    [self performSelector:@selector(flipButton) withObject:nil afterDelay:0.0];
}

- (IBAction)secondButtonPressed {
    if ( button1.selected ) {

        Page2 *page2 = [[Page2 alloc] initWithNibName:@"Page2" bundle:nil];
        page2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:page2 animated:YES];
        [page2 release];

    }
}




/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceO开发者_高级运维rientation == UIInterfaceOrientationPortrait);
}
*/

- (void)flipButton {
    if ( button1.selected ) {
        button1.highlighted = NO;
        button1.selected = NO;
    } else {
        button2.highlighted = YES;
        button2.selected = YES;
    }
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

Thanks a lot for all the help!


You will have to use the highlighted property of the button to set the state as highlighted or otherwise. However doing immediately on Touch Up Inside seems to reset it. So we delay the change until the next run loop starts. Do this on the method called on touch.

-(IBAction)buttonPressed:(id)sender
{
    [self performSelector:@selector(flipButton) withObject:nil afterDelay:0.0];
}

and define the flip method as follows –

- (void)flipButton {
    if ( self.button.selected ) {
        self.button.highlighted = NO;
        self.button.selected = NO;
    } else {
        self.button.highlighted = YES;
        self.button.selected = YES;
    }
}

You can later check on the method called on tap of the other button whether self.button.selected is YES or not and then act on it.

- (IBAction)secondButtonPressed {
    if ( self.button.selected ) {
        // Load next page.
    }
}

Better Approach

Use a UISwitch. Don't you think that it is a natural fit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜