<span style="font-size:24px;">#import "AppDelegate.h"
#import "RootViewController.h"
@implementation AppDelegate</span>
<span style="font-size:24px;">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//<span style="background-color: rgb(51, 204, 255);">采用抽象工廠設計模式</span>:
//提供一個抽象的基類(提供基本的功能),适用時使用具體的子類
//比如UIView,UIViewController都是抽象的基類
//1.建立視圖控制器對象
RootViewController *rootVC = [[RootViewController alloc] init];
//2.指定為window的根視圖控制器
self.window.rootViewController = rootVC;
//3.釋放所有權
[rootVC release];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
-(void)dealloc
{
[_window release];
[super dealloc];
}#import "RootViewController.h"
#import "loginView.h"
@interface RootViewController ()
@end
@implementation RootViewController
<span style="background-color: rgb(51, 204, 255);">//initWithNibName:bundle:初始化方法為指定初始化方法,不管調用該類哪一個初始化方法</span>,該方法都會被調用
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
//self.view;//建立視圖不要放在這裡,因為還沒有初始化,但是調用方法的是視圖控制器的對象,會影響程式運作
}
return self;
}
//當通路控制器的view時,如果view為空,還沒有建立(為視圖控制器建立),會調用loadView方法,為視圖控制器建立view。
//當執行玩loadView之後,就會立即執行viewDidLoad。
//父類對loadView方法的實作,是建立一個和大螢幕大小一樣的view
#pragma mark - loadView
- (void)loadView
{
//[super loadView];//父類對loadView方法的實作就是建立了一個UIView類型的對象,并且作為控制器的view
//将loginView視圖對象指定為視圖控制器的view;
//建立登入界面
loginView *lgView = [[loginView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];//不管frame為多少,在運作的時候系統都會指定view的frame為全螢幕
[lgView.useBtn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
//設定textField代理
lgView.useTF.delegate = self;
//點選空白區域鍵盤回收
lgView.backgroundColor = [UIColor redColor];
//将lgView指定為視圖控制器的view
self.view = lgView;//調用setter方法
[lgView release];
}
- (void)login:(UIButton *)btn
{
NSLog(@"點選了");
}
<span style="background-color: rgb(102, 102, 204);">//回收鍵盤也在這裡</span>
#pragma mark - textFieldShouldReturn
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
/**
* <span style="background-color: rgb(102, 102, 204);">視圖控制器來分擔AppDelegate的任務</span>,管理子視圖,是以也要一個類似于containewView的視圖來管理子視圖,是以對于視圖控制器自身就帶着一個view,大小和螢幕大小相同,通過 self.view通路控制器的額view。
視圖控制器不是視圖,在螢幕上時看不到的,隻要在螢幕上能看到的東西都是視圖.
*/
//<span style="background-color: rgb(153, 153, 255);">當視圖控制器的視圖加載完成後觸發.(隻要loadView方法被調用之後,就會立即調用viewDidLoad方法)</span>
//隻是視圖view建立完畢,但是此時視圖還沒有添加到父視圖上
#pragma mark - viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
//self.view.backgroundColor = [UIColor greenColor];//這裡的view相當于containewView
}
//當重寫父類方法時,如果不知道父類方法對該方法是如何實作的,當自己在實作時,先調用父類對該方法的實作
//當視圖控制器的view布局自身的子視圖的時候,該方法就會被觸發。(視圖控制器的view将要顯示上邊的子視圖時觸發),<span style="background-color: rgb(204, 102, 204);">可以設定view在總螢幕上的大小
- (void)viewWillLayoutSubviews//layout:布局
{
[super viewWillLayoutSubviews];
NSLog(@"%@", self.view.superview);
self.view.frame = CGRectMake(0, 200, 320, 400);
}</span>
/**
* <span style="background-color: rgb(255, 204, 255);">當應用程式收到記憶體警告(當記憶體吃緊,記憶體不足)時觸發,釋放一些暫時不使用(該資源已經配置設定)的系統資源,供目前程式運作</span>
*/
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
//<span style="background-color: rgb(102, 102, 204);">點選view的空白處回收鍵盤
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//讓鍵盤回收
loginView *lgView = (loginView *)self.view;
[lgView.useTF resignFirstResponder];
}</span>
#import <UIKit/UIKit.h>
@interface loginView : UIView
/**
* <span style="background-color: rgb(153, 153, 255);">将控件定義成屬性,給外界提供通路的接口</span>
*/
@property (nonatomic, retain) UILabel *useLabel;
@property (nonatomic, retain) UITextField *useTF;
@property (nonatomic, retain) UIButton *useBtn;
@end
#import "loginView.h"
#define k_color whiteColor
@implementation loginView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//封裝控件
self.useLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 80, 40)];
_useLabel.text = @"使用者名";
_useLabel.layer.cornerRadius = 6;
_useLabel.layer.masksToBounds = YES;
_useLabel.backgroundColor = [UIColor k_color];
_useLabel.textAlignment = NSTextAlignmentLeft;
[self addSubview:_useLabel];
[_useLabel release];
self.useTF = [[UITextField alloc] initWithFrame:CGRectMake(130, 100, 150, 40)];//使用點文法作用:将所有權保留了一份,記憶體優化,能使用點文法就使用點文法
[self addSubview:_useTF];
_useTF.textAlignment = NSTextAlignmentLeft;
_useTF.placeholder = @"請輸入使用者名";
_useTF.borderStyle = UITextBorderStyleRoundedRect;
_useTF.backgroundColor = [UIColor k_color];
[_useTF release];
self.useBtn = [UIButton buttonWithType:UIButtonTypeSystem];
_useBtn.frame = CGRectMake(50, 200, 220, 40);
[_useBtn setTitle:@"登入" forState:UIControlStateNormal];
_useBtn.backgroundColor = [UIColor k_color];
<span style="background-color: rgb(51, 102, 255);"> //這裡不寫button添加點選事件,是因為我們隻想讓loginView具有一個顯示的功能(view本來就隻有顯示的功能),如果添加點選事件,則它就不僅僅是有了一個顯示的功能,也有了處理點選事件的功能,對點選事件做出響應,而controller控件是幫助applacation完成任務的,幹活都讓控件controller控件去幹</span>
[self addSubview:_useBtn];
}
return self;
}
-(void)dealloc
{
[_useLabel release];
[_useTF release];
[_useBtn release];
[super dealloc];
}
</span>