在这个小程序中,我们将开发如何在iPhone APP中隐藏按钮。
step1:打开Xcode,创建一个给予“Single View Application”的应用,起名为“ButtonHide”。
step2:打开ViewController.h 文件,作如下编写:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
UIButton *button1;
UITextField *textfield1;
}
@property (nonatomic,retain) IBOutlet UIButton *button1;
@property (nonatomic,retain) IBOutlet UITextField *textfield1;
-(IBAction)ButtonHide:(id)sender;
@end
step3:打开ViewController.xib文件,从控件库中拖拽一个UIButton和UITextField到视图中,点击“File‘s Owner”连接控件和方法。
step4:打开ViewController.m 文件,作如下编写:
#import "ViewController.h"
@implementation ViewController
@synthesize button1,textfield1;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)ButtonHide:(id)sender
{
if([textfield1.text isEqualToString:@""])
{
button1.hidden = NO;
}
else
{
button1.hidden = YES;
}
NSLog(@"%@",textfield1.text);
}
step5:完成后保存,点击运行小三角,查看效果: