? UI總結(jié)-UIViewController的跳轉(zhuǎn)和textField的一些協(xié)議方法
在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];
//創(chuàng)建2個(gè)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加一個(gè)清除按鈕,點(diǎn)擊按鈕可以清除整個(gè)textField里的內(nèi)容
self.textField.clearButtonMode = UITextFieldViewModeAlways;
[self.textField addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventEditingChanged];
//UIControlEventEditingChanged可以實(shí)時(shí)監(jiān)控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);
}
//當(dāng)clearButton按下時(shí)調(diào)用這個(gè)方法
- (BOOL)textFieldShouldClear:(UITextField *)textField{
NSLog(@"已經(jīng)清空textField");
return YES;
}
//回收鍵盤
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
//當(dāng)編輯框特別靠下時(shí),打開鍵盤往往會(huì)蓋住編輯框,我們可以調(diào)用一個(gè)方法來在編輯框出現(xiàn)之前讓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;
}
//當(dāng)編輯完事后,也要把self.View平移到原來的位置
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
NSLog(@"結(jié)束編輯");
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;
}
//當(dāng)頁面切換到該頁面時(shí),系統(tǒng)會(huì)自動(dòng)調(diào)用前面兩個(gè)方法,當(dāng)頁面離開該頁面時(shí),系統(tǒng)自動(dòng)調(diào)用后面兩個(gè)方法
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSLog(@"視圖將要出現(xiàn)");
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSLog(@"視圖已經(jīng)出現(xiàn)");
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
NSLog(@"視圖將要消失");
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
NSLog(@"視圖已經(jīng)消失");
}
//模態(tài)跳轉(zhuǎn)
- (void)click:(UIButton *)button{
//創(chuàng)建目標(biāo)頁面對(duì)象
SecondViewController *vc = [[SecondViewController alloc]init];
//設(shè)置模態(tài)跳轉(zhuǎn)的樣式
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
//向目標(biāo)頁面進(jìn)行跳轉(zhuǎn)
[self presentViewController:vc animated:YES completion:^{
}];
//內(nèi)存管理
[vc release];
}
//Push 跳轉(zhuǎn)
- (void)clickPush:(UIButton *)button{
//創(chuàng)建目標(biāo)頁面對(duì)象
SecondViewController *vc = [[SecondViewController alloc]init];
//向目標(biāo)頁面進(jìn)行跳轉(zhuǎn)
[self.navigationController pushViewController:vc animated:YES];
//內(nèi)存管理
[vc release];
}
- (void)dealloc{
[_textField release];
[super dealloc];
}
在跳轉(zhuǎn)后的頁面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];
}
//點(diǎn)擊方法,實(shí)現(xiàn)返回之前頁面
-(void)click:(UIButton *)button{
[self dismissViewControllerAnimated:YES completion:^{
}];
}