保持UIButton突出显示

Mar*_*cus 1 iphone uibutton

嘿! 我有一个xib文件,我希望一个圆形的矩形按钮在按下后保持高亮显示.我也希望在第一个按钮后按下一个不同的按钮,将您带到下一页.我怎样才能做到这一点.一些代码将不胜感激!

这是我的.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
Run Code Online (Sandbox Code Playgroud)

这是我的.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 (interfaceOrientation == 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
Run Code Online (Sandbox Code Playgroud)

非常感谢所有的帮助!

Dee*_*olu 7

您必须使用highlighted按钮的属性将状态设置为突出显示或其他.然而立即行动Touch Up Inside似乎重置它.因此我们将更改延迟到下一个运行循环开始.在触摸上调用的方法上执行此操作.

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

并定义翻转方法如下 -

- (void)flipButton {
    if ( self.button.selected ) {
        self.button.highlighted = NO;
        self.button.selected = NO;
    } else {
        self.button.highlighted = YES;
        self.button.selected = YES;
    }
}
Run Code Online (Sandbox Code Playgroud)

你可以叫上其他按钮的自来水的方法后检查是否self.button.selectedYES或者不是,然后采取行动.

- (IBAction)secondButtonPressed {
    if ( self.button.selected ) {
        // Load next page.
    }
}
Run Code Online (Sandbox Code Playgroud)

更好的方法

用一个UISwitch.难道你不认为它是天生的.