实现了一个简单的登陆界面,密码正确则登陆,错误则报错,未写注册及登录后的显示界面
代码示例:
ViewController.m文件
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//用户名提示的标签创建
_lUserName = [[UILabel alloc] init];
_lUserName.frame = CGRectMake(20, 100, 80, 40);
_lUserName.text = @"用户名:";
_lUserName.font = [UIFont systemFontOfSize: 20];
_lUserName.textAlignment = NSTextAlignmentLeft;
//密码提示创建
_lPassWord = [[UILabel alloc] init];
_lPassWord.frame = CGRectMake(20, 200, 80, 40);
_lPassWord.text = @"密码:";
_lPassWord.font = [UIFont systemFontOfSize: 20];
_lPassWord.textAlignment = NSTextAlignmentLeft;
//用户名输入框
_kUserName = [[UITextField alloc] init];
_kUserName.frame = CGRectMake(120, 100, 180, 40);
_kUserName.placeholder = @"请输入用户名";
_kUserName.borderStyle = UITextBorderStyleRoundedRect;
//密码输入框
_kPassWord = [[UITextField alloc] init];
_kPassWord.frame = CGRectMake(120, 200, 180, 40);
_kPassWord.placeholder = @"请输入密码";
_kPassWord.borderStyle = UITextBorderStyleRoundedRect;
_kPassWord.secureTextEntry = YES;
//登陆与注册按钮的创建
_bLogin = [UIButton buttonWithType: UIButtonTypeRoundedRect];
_bLogin.frame = CGRectMake(150, 300, 80, 40);
[_bLogin setTitle: @"登录" forState: UIControlStateNormal];
[_bLogin addTarget: self action: @selector(pressLogin) forControlEvents: UIControlEventTouchUpInside];
_bRegister = [UIButton buttonWithType: UIButtonTypeRoundedRect];
_bRegister.frame = CGRectMake(150, 380, 80, 40);
[_bRegister setTitle: @"注册" forState: UIControlStateNormal];
[_bRegister addTarget: self action: @selector(pressRegister) forControlEvents: UIControlEventTouchUpInside];
//将所有控件添加到视图中
[self.view addSubview: _lUserName];
[self.view addSubview: _lPassWord];
[self.view addSubview: _kUserName];
[self.view addSubview: _kPassWord];
[self.view addSubview: _bRegister];
[self.view addSubview: _bLogin];
}
- (void)pressLogin {
NSString *strName = @"bob";
NSString *strPass = @"222333444";
//获取输入框中的用户名和密码
NSString *getName = _kUserName.text;
NSString *getPass = _kPassWord.text;
if ([getName isEqualToString: strName] && [getPass isEqualToString: strPass]) {
NSLog(@"用户名与密码输入正确!");
//创建一个UIAlertController对象
//P1:弹出框的标题 P2:弹出框的内容 P3::弹出的警告框的样式为UIAlertControllerStyleAlert 中心弹出
UIAlertController *alertController = [UIAlertController alertControllerWithTitle: @"提示" message: @"验证成功" preferredStyle: UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle: @"确认" style: UIAlertActionStyleDefault handler: nil];
[alertController addAction: defaultAction];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler: nil];
[alertController addAction: cancelAction];
[self presentViewController: alertController animated: YES completion: nil];
} else {
NSLog(@"用户名或密码错误");
UIAlertController *alertController = [UIAlertController alertControllerWithTitle: @"提示" message: @"用户名或密码错误,登陆失败" preferredStyle: UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle: @"确认" style: UIAlertActionStyleDefault handler: nil];
[alertController addAction: defaultAction];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler: nil];
[alertController addAction: cancelAction];
[self presentViewController: alertController animated: YES completion: nil];
}
}
- (void)pressRegister {
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//回收键盘对象
[_kUserName resignFirstResponder];
[_kPassWord resignFirstResponder];
}
@end
运行结果:
若点击输入框为出现小键盘,有可能需要在模拟器窗口点击菜单栏 I/O,进入子菜单keyboard,取消勾选Connect Hardware再进行尝试
实现了一个简单的照片墙案例,可简单显示插入的图片:
代码示例:
SceneDelegate.m:
#import "SceneDelegate.h"
#import "RootViewController.h"
@interface SceneDelegate ()
@end
@implementation SceneDelegate
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
//获取当前屏幕参数
self.window.frame = [UIScreen mainScreen].bounds;
//创建导航控制器
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
//将该导航控制器赋为根视图控制器
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
}
RootViewController.m:
#import "RootViewController.h"
#import "ImageViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"照片墙";
self.view.backgroundColor = [UIColor whiteColor];
//设置导航栏是否透明
self.navigationController.navigationBar.translucent = YES;
//创建一个滚动视图并设置位置与画布大小
UIScrollView *sv = [[UIScrollView alloc] init];
sv.frame = CGRectMake(5, 10, 394, 852);
sv.contentSize = CGSizeMake(394, 825 * 1.5);
//打开交互事件,关闭的话会导致无法使用点击等手势操作成功运行
sv.userInteractionEnabled = YES;
//使用for循环给根视图上添加图片对象
for (int i = 0; i < 7; i++) {
//创建一个代表所传图片文件名字的字符串对象
NSString *strName = [NSString stringWithFormat: @"NARUTO%d.jpg", i + 1];
//创建一个图片对象,传入的图片就是名字为字符串对象的那个图
UIImage *image = [UIImage imageNamed: strName];
//创建一个图片视图对象
UIImageView *iView = [[UIImageView alloc] initWithImage: image];
//为该图片视图对象设置位置,按要求进行相应计算使其排列
iView.frame = CGRectMake(10 + (i % 3) * 125, (i / 3) * 170, 110, 150);
//使该视图在滚动视图上显示
[sv addSubview: iView];
//打开交互事件,关闭可能会使点击手势操作无法运行
iView.userInteractionEnabled = YES;
//创建点击手势使其执行事件函数
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(pressTap:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[iView addGestureRecognizer: tap];
//图像对象的tag值
iView.tag = 101 + i;
}
[self.view addSubview: sv];
}
- (void)pressTap: (UITapGestureRecognizer*) tap {
UIImageView *imageView = (UIImageView*)tap.view;
//创建显示视图控制器
ImageViewController *imageShow = [[ImageViewController alloc] init];
//tag在一些时候可以作为索引,如这里可以作为图片的位置数
imageShow.imagetag = imageView.tag;
//推入导航栏
[self.navigationController pushViewController: imageShow animated: YES];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
ImageViewController.h:
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface ImageViewController : UIViewController
//为使调用该方法的对象将其图片传给本对象,可通过创建属性来记录传进来的图片是哪一张,以下有创建两种种属性的方式
//1.图像视图的tag,在根视图的图片部分为其添加特殊的tag值,在调用函数的时候将该tag值setter进创建的对象的属性中
@property (nonatomic, assign) NSUInteger imagetag;
//2.图像对象,直接在根视图中对象调用函数时将图片set进该属性
@property (nonatomic, retain) UIImage *image;
//图像视图对象,不能用来传,传了的话就会导致点开后的图片在根视图上消失
@property (nonatomic, retain) UIImageView *imageView;
@end
NS_ASSUME_NONNULL_END
ImageViewController.m:
#import "ImageViewController.h"
#import "RootViewController.h"
@interface ImageViewController ()
@end
@implementation ImageViewController
@synthesize imageView = _imageView;
@synthesize image = _image;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"图片展示";
UIImageView *_imageView = [[UIImageView alloc] init];
_imageView.frame = CGRectMake(0, 100, 394, 650);
//通过图片属性image传图片
//_imageView.image = _image
//通过imageTag属性传图片
_imageView.image = [UIImage imageNamed: [NSString stringWithFormat: @"NARUTO%lu.jpg", _imagetag - 100]];
[self.view addSubview: _imageView];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
运行结果: