在这个小程序中,我们将开发如何在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:完成后保存,点击运行小三角,查看效果:
本文介绍如何在iPhone应用中实现按钮的显示与隐藏功能。通过Xcode创建项目,并使用Swift编程语言,在ViewController类中实现ButtonHide方法,根据UITextField输入状态切换按钮可见性。
885

被折叠的 条评论
为什么被折叠?



