UI总结-UIViewController的跳转和textField的一些协议方法
在rootViewController.m文件中:
#import "RootViewController.h"
#import "SecondViewController.h"
@interface RootViewController ()
@property(nonatomic, retain)UITextField *textField;
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
NSLog(@"我被使用了");
}
return self;
}
- (void)loadView{
[super loadView];
NSLog(@"加载self.View");
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor cyanColor];
//创建2个button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(20, 80, 100, 30);
button.backgroundColor = [UIColor grayColor];
[button setTitle:@"下一页" forState:UIControlStateNormal];
button.layer.cornerRadius = 5;
button.layer.borderWidth = 1;
[self.view addSubview:button];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
UIButton *buttonPush = [UIButton buttonWithType:UIButtonTypeCustom];
buttonPush.frame = CGRectMake(20, 120, 100, 30);
buttonPush.backgroundColor = [UIColor grayColor];
[buttonPush setTitle:@"下一页" forState:UIControlStateNormal];
buttonPush.layer.cornerRadius = 5;
buttonPush.layer.borderWidth = 1;
[self.view addSubview:buttonPush];
[buttonPush addTarget:self action:@selector(clickPush:) forControlEvents:UIControlEventTouchUpInside];
self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 500, 100, 30)];
self.textField .backgroundColor = [UIColor redColor];
[self.view addSubview:self.textField];
[_textField release];
self.textField.delegate = self;
//给textField加一个清除按钮,点击按钮可以清除整个textField里的内容
self.textField.clearButtonMode = UITextFieldViewModeAlways;
[self.textField addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventEditingChanged];
//UIControlEventEditingChanged可以实时监控textField里面指的变化.
UIActionSheet *Actionsh = [[UIActionSheet alloc] initWithTitle:@"其他" delegate:self cancelButtonTitle:@"返回" destructiveButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
Actionsh.actionSheetStyle = UIActionSheetStyleAutomatic;
[Actionsh showInView:self.view];
}
- (void)changeValue:(UITextField *)textField{
NSLog(@"%@",textField.text);
}
//当clearButton按下时调用这个方法
- (BOOL)textFieldShouldClear:(UITextField *)textField{
NSLog(@"已经清空textField");
return YES;
}
//回收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
//当编辑框特别靠下时,打开键盘往往会盖住编辑框,我们可以调用一个方法来在编辑框出现之前让self.view 向上平移.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(@"开始编辑");
CGFloat h = textField.center.y - self.view.frame.size.height / 2;
if (h > 0) {
self.view.center = CGPointMake(self.view.center.x, self.view.center.y - h);
}
return YES;
}
//当编辑完事后,也要把self.View平移到原来的位置
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
NSLog(@"结束编辑");
CGFloat h = textField.center.y - self.view.frame.size.height / 2;
if (h > 0) {
self.view.center = CGPointMake(self.view.center.x, self.view.center.y + h);
}
return YES;
}
//当页面切换到该页面时,系统会自动调用前面两个方法,当页面离开该页面时,系统自动调用后面两个方法
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSLog(@"视图将要出现");
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSLog(@"视图已经出现");
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
NSLog(@"视图将要消失");
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
NSLog(@"视图已经消失");
}
//模态跳转
- (void)click:(UIButton *)button{
//创建目标页面对象
SecondViewController *vc = [[SecondViewController alloc]init];
//设置模态跳转的样式
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
//向目标页面进行跳转
[self presentViewController:vc animated:YES completion:^{
}];
//内存管理
[vc release];
}
//Push 跳转
- (void)clickPush:(UIButton *)button{
//创建目标页面对象
SecondViewController *vc = [[SecondViewController alloc]init];
//向目标页面进行跳转
[self.navigationController pushViewController:vc animated:YES];
//内存管理
[vc release];
}
- (void)dealloc{
[_textField release];
[super dealloc];
}
在跳转后的页面secondViewController.m文件中:
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 80, 100, 30);
button.backgroundColor = [UIColor cyanColor];
[button setTitle:@"返回" forState:UIControlStateNormal];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
//点击方法,实现返回之前页面
-(void)click:(UIButton *)button{
[self dismissViewControllerAnimated:YES completion:^{
}];
}