#import "RootViewController.h"
#import "RootView.h"
#define kColor arc4random() % 256 / 255.0
@interface RootViewController ()<UIAlertViewDelegate, UITextFieldDelegate>
@property (nonatomic, strong) RootView *rootView;
@property (nonatomic, strong) UIAlertController *colorAlert;
@property (nonatomic, strong) UIAlertController *warningAlert;
@end
@implementation RootViewController
- (void)loadView {
self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.rootView;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 调用长按手势
[self longPressGesture];
}
/**
* 添加长按手势UILongPressGestureRecognizer
*/
- (void)longPressGesture {
// 创建长按手势对象 -- UIAlertController
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureAction:)];
// 创建长按手势对象 -- UIAlertView
// UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
// 给myView添加长按手势
[self.rootView.myView addGestureRecognizer:longPress];
}
/**
* 实现长按事件 -- UIAlertController
*
* @param longPress 长按手势
*/
- (void)longPressGestureAction:(UILongPressGestureRecognizer *)longPress {
// 手势开始时
if (longPress.state == UIGestureRecognizerStateBegan) {
// 创建UIAlertController对象
self.colorAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"改变颜色吗?" preferredStyle:UIAlertControllerStyleAlert];
// 创建UIAlertAction对象
// 确定按钮
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// 实现给myView换随机背景颜色
self.rootView.myView.backgroundColor = [UIColor colorWithRed:kColor green:kColor blue:kColor alpha:];
// 获取当前alert上所有的textField
NSArray *textFieldArr = self.colorAlert.textFields;
// 遍历获取到的textField数组,分别获取text
for (UITextField *field in textFieldArr) {
NSLog(@"text = %@", field.text);
}
}];
// 取消按钮
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
// 将action添加到alert上
[self.colorAlert addAction:action1];
[self.colorAlert addAction:action2];
// 添加TextField
// weak 弱引用(内存问题)
__weak RootViewController *rootVC = self;
[self.colorAlert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"用户名";
textField.delegate = rootVC;
}];
[self.colorAlert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"密码";
// 密文输入
textField.secureTextEntry = YES;
textField.delegate = rootVC;
}];
// 模态推出
[self presentViewController:self.colorAlert animated:YES completion:nil];
}
}
/**
* 实现textField代理方法,按return按钮回收键盘
*
* @param textField 当前textField
*
* @return
*/
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
/**
* 实现长按事件 -- UIAlertView
*
* @param longPress 长按手势
*/
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
// 手势开始时
if (longPress.state == UIGestureRecognizerStateBegan) {
// 创建UIAlertView对象,并添加按钮
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"改变颜色吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"按钮1", nil];
/**
* 设置alertView样式
UIAlertViewStyleDefault = 0, -- 默认,没有输入框
UIAlertViewStyleSecureTextInput, -- 密文输入
UIAlertViewStylePlainTextInput, -- 明文输入
UIAlertViewStyleLoginAndPasswordInput -- 明文输入&密文输入结合,类似登录
*/
[alertView setAlertViewStyle:UIAlertViewStyleDefault];
// 显示alertView
[alertView show];
}
}
/**
* 实现确定按钮点击事件
*
* @param alertView 当前alertView
* @param buttonIndex 按钮下标
*/
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == ) { // index为按钮添加顺序,取消 -- 0 、确定 -- 1 、按钮1 -- 2
// 改变myView背景颜色
self.rootView.myView.backgroundColor = [UIColor colorWithRed:kColor green:kColor blue:kColor alpha:];
}
}
/**
* 发生内存警告会自动调用
*/
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
// 创建UIAlertController对象
self.warningAlert = [UIAlertController alertControllerWithTitle:@"内存问题" message:nil preferredStyle:UIAlertControllerStyleAlert];
// 创建UIAlertAction对象,类型为警告
UIAlertAction *action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDestructive handler:nil];
// 将action添加到alert上
[self.warningAlert addAction:action];
// 模态推出alert
[self presentViewController:self.warningAlert animated:YES completion:nil];
}
@end