您的位置:首页 > 房产 > 家装 > UI案例——登陆系统

UI案例——登陆系统

2025/3/10 14:11:47 来源:https://blog.csdn.net/qq_73106050/article/details/139390582  浏览:    关键词:UI案例——登陆系统

UI的登陆界面实例

在学习了UILabel,UIButton,UIView,UITextField的内容之后,我们就可以写一个简单的登陆界面

我们可以通过UILabel来编写我们显示在登陆界面上的文字比方说下面这两行字就是通过UILabel去实现的。

在这里插入图片描述

下面给出一下实现的代码

_lbUserName = [[UILabel alloc] init];_lbUserName.frame = CGRectMake(20, 60, 80, 40);//设置位置和大小_lbUserName.text = @"用户名";_lbUserName.font = [UIFont systemFontOfSize:20];//设置字体大小_lbUserName.textAlignment = NSTextAlignmentLeft;_lbUserPassword = [[UILabel alloc] initWithFrame:CGRectMake(20, 140, 80, 40)];_lbUserPassword.text = @"密码";_lbUserPassword.font = [UIFont systemFontOfSize:20];_lbUserPassword.textAlignment = NSTextAlignmentLeft;

为了给用户返回提示,设计了一个警告对话框来用于给用户返回提示,这个警告对话框是通过UIAlertController来实现的,下面给出代码

在这里插入图片描述

				UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名和密码正确" preferredStyle:UIAlertControllerStyleAlert];//p1:标题名字,p2:显示的信息,p3是用来指定 UIAlertController 的样式的。UIAlertControllerStyleAlert 是其中一种可选的样式。UIAlertAction* confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* _Nonnull action) {NSLog(@"点击确认");}];//UIAlertAction 是用来创建 UIAlertController 中的按钮的。p1:按钮标题,p2:风格,p3:按钮的点击事件处理[alertController addAction:confirm];//给这个弹窗添加上确认这个按钮UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction* _Nonnull action) {NSLog(@"点击取消");}];[alertController addAction: cancel];[self presentViewController:alertController animated:YES completion:nil];

创建登陆和注册两个按钮,来实现点击登陆的效果

在这里插入图片描述

		_btLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];_btLogin.frame = CGRectMake(120, 300, 80, 40);[_btLogin setTitle:@"登陆" forState:UIControlStateNormal];//设置按钮类型[_btLogin addTarget:self action:@selector(pressLogin) forControlEvents:UIControlEventTouchUpInside];//为按钮添加点击事件[self.view addSubview:_btLogin];

下面给出一下整体实现的代码

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor orangeColor];_lbUserName = [[UILabel alloc] init];_lbUserName.frame = CGRectMake(20, 60, 80, 40);_lbUserName.text = @"用户名";_lbUserName.font = [UIFont systemFontOfSize:20];_lbUserName.textAlignment = NSTextAlignmentLeft;_lbUserPassword = [[UILabel alloc] initWithFrame:CGRectMake(20, 140, 80, 40)];_lbUserPassword.text = @"密码";_lbUserPassword.font = [UIFont systemFontOfSize:20];_lbUserPassword.textAlignment = NSTextAlignmentLeft;[self.view addSubview:_lbUserName];[self.view addSubview:_lbUserPassword];_tfUserName = [[UITextField alloc] initWithFrame:CGRectMake(120, 60, 150, 40)];_tfUserName.placeholder = @"请输入用户名";_tfUserName.borderStyle = UITextBorderStyleRoundedRect;_tfUserPassword = [[UITextField alloc] initWithFrame:CGRectMake(120, 140, 150, 40)];_tfUserPassword.placeholder = @"请输入密码";_tfUserPassword.borderStyle = UITextBorderStyleRoundedRect;_tfUserPassword.secureTextEntry = YES;[self.view addSubview:_tfUserName];[self.view addSubview:_tfUserPassword];_btLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];_btLogin.frame = CGRectMake(120, 300, 80, 40);[_btLogin setTitle:@"登陆" forState:UIControlStateNormal];[_btLogin addTarget:self action:@selector(pressLogin) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:_btLogin];_btRegister = [UIButton buttonWithType:UIButtonTypeRoundedRect];_btRegister.frame = CGRectMake(120, 360, 80, 40);[_btRegister setTitle:@"注册" forState:UIControlStateNormal];[_btRegister addTarget:self action:@selector(pressRegister) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:_btRegister];// Do any additional setup after loading the view.
}
- (void) pressLogin {NSString* strName = @"michael";NSString* strPass = @"12345678";if ([strPass isEqual: _tfUserPassword.text] && [strName isEqual: self.tfUserName.text]) {NSLog(@"用户名密码正确");UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名和密码正确" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction* confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* _Nonnull action) {NSLog(@"点击确认");}];[alertController addAction:confirm];UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction* _Nonnull action) {NSLog(@"点击取消");}];[alertController addAction: cancel];[self presentViewController:alertController animated:YES completion:nil];} else {NSLog(@"用户名或密码错误");UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码错误" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction* confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* _Nonnull action) {NSLog(@"点击确认");}];[alertController addAction:confirm];UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction* _Nonnull action) {NSLog(@"点击取消");}];[alertController addAction: cancel];[self presentViewController:alertController animated:YES completion:nil];}
}
- (void) pressRegister {NSLog(@"ddd");
}-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {[_tfUserName resignFirstResponder];[_tfUserName resignFirstResponder];
}
@end

最后实现的效果:

在这里插入图片描述

登陆成功的界面:

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com