喂! 我有一个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
这是我的.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
非常感谢所有的帮助!
答案 0 :(得分:7)
您必须使用按钮的highlighted
属性将状态设置为突出显示或其他。但是,在Touch Up Inside
上立即执行操作似乎会重置它。因此我们将更改延迟到下一个运行循环开始。在触摸上调用的方法上执行此操作。
-(IBAction)buttonPressed:(id)sender
{
[self performSelector:@selector(flipButton) withObject:nil afterDelay:0.0];
}
并按如下方式定义翻转方法 -
- (void)flipButton {
if ( self.button.selected ) {
self.button.highlighted = NO;
self.button.selected = NO;
} else {
self.button.highlighted = YES;
self.button.selected = YES;
}
}
您可以稍后检查点按其他按钮时调用的方法self.button.selected
是否为YES
,然后对其采取行动。
- (IBAction)secondButtonPressed {
if ( self.button.selected ) {
// Load next page.
}
}
更好的方法
使用UISwitch
。你不觉得这很自然吗。