initWithNibName:bundle:初始化方法爲指定的初始化方法,不管調用該類的哪一個初始化方法,該方法都會被調用
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
NSLog(@"%s",__FUNCTION__);
//self.view;(如果提前訪問view,會影響到程序的一個執行順序)
}
return self;
}
//當訪問 控制器的view時,如果view爲空,還沒有創建,會調用loadview方法,爲試圖控制器創建view
//在執行完loadView之後,就會立即執行viewdidload
//父類對loadview方法的實現,是創建一個和屏幕一樣大小的view
- (void)loadView
{
[super loadView];//父類對loadView方法的實現就是創建了一個UIView類型的對象,並且作爲控制器的view(根視圖)
NSLog(@"%s",__FUNCTION__);
//將loginView視圖對象指定爲視圖控制器的view
LoginView *lgview = [[LoginView alloc]initWithFrame:CGRectZero];
[lgview.button addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
//將lgView制定爲視圖控制器的view
self.view = lgview;
[lgview release];
//設置textfield代理
lgview.field.delegate = self;
}
- (void)login:(UIButton *)button
{
NSLog(@"死了");
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
視圖控制器用來分擔Appdalegate的任務,管理子視圖,所以也需要一個類似於containerView的視圖來管理子視圖,所以對於視圖控制器自身就自帶一個view,大小和屏幕大小相同,通過self.view訪問控制器的view
視圖控制器不是視圖,在屏幕上是看不到的,只要在屏幕上看到東西,都是視圖
當視圖控制器的視圖加載完成後觸發viewdidload(只要loadview方法被調用之後,就會立即調用viewdidload)
只是視圖view創建完畢,但是此時視圖還沒有添加到父視圖上
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(120, 100, 100, 40)];
label.backgroundColor = [UIColor grayColor];
[self.view addSubview:label];
[label release];
UITextField *field = [[UITextField alloc]initWithFrame:CGRectMake(120, 180, 100, 40)];
field.backgroundColor = [UIColor greenColor];
[self.view addSubview:field];
//field.borderStyle =
field.layer.cornerRadius = 10;
[field release];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.backgroundColor = [UIColor blueColor];
[button setTitle:@"點擊" forState:UIControlStateNormal];
button.layer.cornerRadius = 10;
[self.view addSubview:button];
// Do any additional setup after loading the view.
//self.view.backgroundColor = [UIColor greenColor];
NSLog(@"%s",__FUNCTION__);
}
當視圖控制器的view佈局自身的子視圖時,該方法就會被觸發(視圖控制器的view將要顯示上邊的子視圖時觸發)(該方法在視圖控制器的view所在的類裏)
-(void)viewWillLayoutSubviews
{
當重寫父類方法實現時,如果不知道父類是對該方法如何實現的,當自己在實現是,先調用父類對該方法的實現
[super viewWillLayoutSubviews];
NSLog(@"%@",self.view.superview);
self.view.frame = CGRectMake(0, 200, 300, 400);
}
當應用程序收到內存警告(當內存吃緊,內存不足時)時觸發.釋放一些暫時不使用(該資源已經分配,但暫時沒使用)的系統資源,供當前程序運行
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//NSLog(@"%@",self.view.superview);
// Dispose of any resources that can be recreated.
//NSLog(@"%s",__FUNCTION__);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//讓鍵盤迴收
//self.view
LoginView *lgview = (LoginView *)self.view;
[lgview.field resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
//當收到內存警告時,移除當前未在屏幕上的顯示的視圖
//判斷是否可以安全的移除視圖控制器的view
//1判斷當前視圖控制器的view是否在屏幕上顯示self.view.window = nil(是否爲空)
//self.view.window
//2判斷視圖控制器的view是否已經成功加載
if (!self.view.window && [self isViewLoaded]) {
//這時可以安全的移除
NSLog(@"%@",self.view.window);
self.view = nil;
//等價於[_view release] ,_view = nil(set方法)
}
//NSLog(@"%@",self.view.window);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
LoginView *lgView = (LoginView *)self.view;
[lgView.tf resignFirstResponder];
}
#pragma mark - UIViewControllerRotation
//用來設置當前視圖控制器是否支持旋轉
- (BOOL)shouldAutorotate{
return YES;
}
- (void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
NSLog(@"呵呵");
}
//設置屏幕旋轉的方法,系統默認支持三個方法方向,豎直方法(正),左橫屏,和右橫屏
- (NSUInteger)supportedInterfaceOrientations{
//豎直方法的正方向
//return UIInterfaceOrientationMaskPortrait;
//左橫屏旋轉
//return UIInterfaceOrientationMaskLandscapeRight;
//
//return UIInterfaceOrientationMaskLandscape;
//隨着屏幕旋轉
return UIInterfaceOrientationMaskAll;
}
//當屏幕將要旋轉時觸發
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
//當屏幕將要旋轉時,需要暫停音樂播放,視頻播放,關閉用戶交互
NSLog(@"%s",__FUNCTION__);
}
//當屏幕旋轉時觸發
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
//在該方法中,如果想要在屏幕旋轉時,添加自己的動畫,在該方法中實現
NSLog(@"%s",__FUNCTION__);
}
//當屏幕旋轉之後觸發
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
//旋轉之後,繼續播放音樂,繼續播放視頻,打開用戶交互
NSLog(@"%s",__FUNCTION__);
}
//當屏幕每次旋轉時,都會觸發的視圖的重新佈局方法,爲該視圖的子視圖重新佈局
- (void) layoutSubviews
{
[super layoutSubviews];
NSLog(@"layoutsubviews");
//對子視圖重新佈局
//根據屏幕旋轉的方向決定佈局的樣式
//1.獲取屏幕旋轉的的方向
switch ([UIApplication sharedApplication].statusBarOrientation) {
case UIInterfaceOrientationPortrait:
NSLog(@"豎直方向(正)");
_btn.frame = CGRectMake(50, 200, 100, 40);
break;
case UIInterfaceOrientationPortraitUpsideDown :
NSLog(@"豎直反方向");
_btn.frame = CGRectMake(50, 200, 100, 40);
break;
case UIInterfaceOrientationLandscapeLeft :
NSLog(@"左方向");
_btn.frame = CGRectMake(320, 100, 60, 40);
break;
case UIInterfaceOrientationLandscapeRight:
NSLog(@"右方向");
//設置橫屏時視圖顯示的樣式
_btn.frame = CGRectMake(320, 100, 60, 40);
break;
default:
break;
}
}
- (void)dealloc{
[_label release];
[_tf release];
[_btn release];
[super dealloc];
}
//點擊空白處鍵盤迴收實現
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[_tf resignFirstResponder];
}