新建一個工程,關閉ARC , ?AppDelegate.h 中window屬性strong改成retain ? ?
APPDelegate.m中window 初始化時加autorelease ? ? 上面重寫dealloc方法
建一個UIViewController 的子類?RootViewController ??
把建好的三個頁面添加到控制器默認視圖上,并加上頁面中按鈕的觸發跳轉事件
,設置APPDelegate.m中window 的根控制器為RootViewController ??
具體代碼
UILabel建立category ?自定義初始化
// UILabel+Creat.h中代碼 #import <UIKit/UIKit.h> @interface UILabel (Creat) // 自定義label的初始化方法 - (instancetype)initWithFrame:(CGRect)frame text:(NSString * )text; @end // UILabel+Creat.m中代碼 #import " UILabel+Creat.h " @implementation UILabel (Creat) // 自定義label的初始化方法 - (instancetype)initWithFrame:(CGRect)frame text:(NSString * )text { self = [self initWithFrame:frame]; if (self) { self.text = text; // 設置文字顯示 // self.textAlignment = NSTextAlignmentRight; } return self; } @end
UITextField建立category ?自定義初始化
// UITextField+Creat.h中代碼 @interface UITextField (Creat) // 初始化 - (instancetype)initWithFrame:(CGRect)frame text:(NSString * )text; @end // UITextField+Creat.m中代碼 #import " UITextField+Creat.h " @implementation UITextField (Creat) - (instancetype)initWithFrame:(CGRect)frame text:(NSString * )text { self = [self initWithFrame:frame]; if (self) { // 默認輸入框中文字 self.placeholder = [text substringToIndex:[text length]- 1 ]; // 輸入框邊框樣式 self.borderStyle = UITextBorderStyleRoundedRect; } return self; } @end
UIButton建立category ?自定義初始化
// UIButton+Creat.h中代碼 #import <UIKit/UIKit.h> @interface UIButton (Creat) // 初始化 + (UIButton *)systemButtonWithFrame:(CGRect)frame title:(NSString *)title target:( id )target action:(SEL)action; + (UIButton *)systemButtonWithFrame:(CGRect)frame title:(NSString * )title; @end // UIButton+Creat.m中代碼 #import " UIButton+Creat.h " @implementation UIButton (Creat) + (UIButton *)systemButtonWithFrame:(CGRect)frame title:(NSString *)title target:( id )target action:(SEL)action { UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = frame; // 設置button frame [button setTitle:title forState:UIControlStateNormal]; // 設置標題 聲明 [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; // 添加button動作事件 return button; } + (UIButton *)systemButtonWithFrame:(CGRect)frame title:(NSString * )title { return [UIButton systemButtonWithFrame:frame title:title target:nil action:nil]; } @end
自定義視圖LTView ? 前面是文字后面是輸入框
// LTView.h中代碼 #import <UIKit/UIKit.h> @interface LTView : UIView // 初始化 - ( id )initWithFrame:(CGRect)frame text:(NSString * )text; @end // LTView.m中代碼 #import " LTView.h " #import " UILabel+Creat.h " #import " UITextField+Creat.h " // 坐標使用宏來計算,方便 #define kPaddingLeft 2 #define kPaddingTop 2 #define kMiddleSpaceing 2 @interface LTView () { UILabel *_label; // 私有變量 UITextField *_textfield; // 私有變量 } @end @implementation LTView -( void )dealloc { [_label release]; [_textfield release]; [super dealloc]; } - ( id )initWithFrame:(CGRect)frame text:(NSString * )text { self = [super initWithFrame:frame]; if (self) { // 這是一個label- textfield 自定義視圖 CGFloat width = frame.size.width; CGFloat height = frame.size.height; // label寬 #define kLabelWidth (width-kPaddingLeft*2)/3 // label高 #define kLabelHeight height-kPaddingTop*2 // textfield寬 #define kTextFieldWidth width-kPaddingLeft*2-kLabelWidth-kMiddleSpaceing _label = [[UILabel alloc] initWithFrame:CGRectMake(kPaddingLeft, kPaddingTop, kLabelWidth, kLabelHeight) text:text]; _label.textAlignment = NSTextAlignmentRight; // label文字右對齊 [self addSubview:_label]; // label添加到LTView _textfield = [[UITextField alloc] initWithFrame:CGRectMake(kPaddingLeft+kLabelWidth+ kMiddleSpaceing, kPaddingTop, kTextFieldWidth, kLabelHeight) text:text]; [self addSubview:_textfield]; // Initialization code } return self; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end
建立登錄頁面
// LoginView.h中代碼 #import <UIKit/UIKit.h> #import " LTView.h " @interface LoginView : UIView @property (nonatomic ,retain) UIButton * loginButton; @property (nonatomic ,retain) UIButton * passwordButton; @property (nonatomic ,retain) UIButton * registButton; @end // LoginView.m中代碼 #import " LoginView.h " #import " UIButton+Creat.h " @implementation LoginView -( void )dealloc { [super dealloc]; } - ( id )initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { CGFloat y = 40 ; // 設置開始y坐標 // label的文字放到一個數組,用一個循環展示LTView NSArray *text = [[NSArray alloc] initWithObjects: @" 用戶名: " , @" 密碼: " , nil]; // 設置一個有用戶名,密碼帶輸入框的LTView,用循環來實現 for ( int i = 0 ; i< 2 ; i++ ) { LTView *ltview = [[LTView alloc] initWithFrame:CGRectMake( 40 , y, 240 , 40 ) text:[text objectAtIndex:i]]; [self addSubview:ltview]; [ltview release]; y += 60 ; } // 設置登陸頁面的三個按鈕 _loginButton = [UIButton systemButtonWithFrame:CGRectMake( 40 , 160 , 70 , 40 ) title: @" 登錄 " ]; [self addSubview:_loginButton]; _passwordButton = [UIButton systemButtonWithFrame:CGRectMake( 120 , 160 , 80 , 40 ) title: @" 找回密碼 " ]; [self addSubview:_passwordButton]; _registButton = [UIButton systemButtonWithFrame:CGRectMake( 210 , 160 , 70 , 40 ) title: @" 注冊 " ]; [self addSubview:_registButton]; // Initialization code } return self; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end
找回密碼頁面
// FindPasswordView.h中代碼 #import <UIKit/UIKit.h> @class UIButton; @interface FindPasswordView : UIView @property (nonatomic,retain) UIButton * undo; @property (nonatomic,retain) UIButton * tofind; @end // FindPasswordView.m中代碼 #import " FindPasswordView.h " #import " UIButton+Creat.h " #import " UITextField+Creat.h " @implementation FindPasswordView -( void )dealloc { [super dealloc]; } - ( id )initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 找回秘密頁面,輸入郵箱的輸入框 UITextField *email = [[UITextField alloc] initWithFrame:CGRectMake( 50 , 100 , 220 , 40 ) text: @" 輸入郵箱: " ]; [self addSubview:email]; [email release]; // 找回密碼頁面的兩個按鈕,使用自定義的初始化初始化 _undo = [UIButton systemButtonWithFrame:CGRectMake( 60 , 160 , 70 , 40 ) title: @" 取消 " ]; [self addSubview:_undo]; _tofind = [UIButton systemButtonWithFrame:CGRectMake( 190 , 160 , 80 , 40 ) title: @" 找回 " ]; [self addSubview:_tofind]; // Initialization code } return self; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end
注冊頁面
// RegisterView.h中代碼 #import <UIKit/UIKit.h> @interface RegisterView : UIView @property (nonatomic ,retain) UIButton * registButton; @property (nonatomic,retain) UIButton * undoButton; @end // RegisterView.m中代碼 #import " RegisterView.h " #import " LTView.h " #import " UIButton+Creat.h " @implementation RegisterView -( void )dealloc { [super dealloc]; } - ( id )initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 設置初始y坐標 CGFloat y = 40 ; // 設置注冊頁面label文字放到一個數組,供循環使用 NSArray *text = [[NSArray alloc] initWithObjects: @" 用戶名: " , @" 密碼: " , @" 重輸密碼: " , @" 郵箱: " , @" 手機: " , nil]; // 設置循環,實現注冊頁面輸入 for ( int i = 0 ; i< 5 ; i++ ) { LTView *ltview = [[LTView alloc] initWithFrame:CGRectMake( 40 , y, 240 , 40 ) text:[text objectAtIndex:i]]; [self addSubview:ltview]; [ltview release]; y += 60 ; } // 設置注冊頁面的兩個按鈕 _undoButton = [UIButton systemButtonWithFrame:CGRectMake( 60 , 340 , 70 , 40 ) title: @" 取消 " ]; [self addSubview:_undoButton]; _registButton = [UIButton systemButtonWithFrame:CGRectMake( 190 , 340 , 80 , 40 ) title: @" 注冊 " ]; [self addSubview:_registButton]; // Initialization code } return self; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end
視圖控制器
// RootViewController.h中代碼 #import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end // RootViewController.m中代碼 #import " RootViewController.h " #import " LoginView.h " #import " FindPasswordView.h " #import " RegisterView.h " @interface RootViewController () @end @implementation RootViewController // 登錄button的方法觸動事件跳轉到登錄頁面 - ( void )login:(UIButton * )button { NSLog( @" 登錄 " ); [self.view endEditing:YES]; [self.view bringSubviewToFront:[self.view viewWithTag: 100 ]]; } // 找回button的方法觸動事件跳轉到找回密碼頁面 - ( void )undo:(UIButton * )button { NSLog( @" 取消 " ); [self.view endEditing:YES]; [self.view bringSubviewToFront:[self.view viewWithTag: 101 ]]; } // 注冊button的方法觸動事件跳轉到注冊頁面 - ( void )regist:(UIButton * )button { NSLog( @" 注冊 " ); [self.view endEditing:YES]; [self.view bringSubviewToFront:[self.view viewWithTag: 102 ]]; } - ( id )initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle * )nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // 初始化方法中一定不要使用self.view 因為這里一使用馬上會調用下面的loadview了 // Custom initialization } return self; } // 重寫加載視圖方法 // - (void)loadView // { // LoginView *loginView = [[LoginView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; // self.view = loginView; // [loginView.loginButton addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside]; // [loginView release]; // // 我們在loadview里面,必須實現以下內容: // // self.view = 你的視圖 // // 所謂的加載視圖,就是給self.view賦值 // // } - ( void )viewDidLoad { // self.view.backgroundColor = [UIColor redColor]; // 視圖給自帶的視圖設置color // 視圖控制器自帶的view是一個空白的view // 如果我們要實現一個Loginview.我們需要在空白的view上面添加很多很多控件 [super viewDidLoad]; // 創建登錄界面 LoginView *loginview = [[LoginView alloc] initWithFrame:CGRectMake( 0 , 0 , 320 , 480 )]; loginview.backgroundColor = [UIColor whiteColor]; // 設置登錄界面背景顏色 // 設置登陸界面tag值 loginview.tag = 100 ; // 登錄界面三個按鈕觸發事件 [loginview.loginButton addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside]; [loginview.passwordButton addTarget:self action:@selector(undo:) forControlEvents:UIControlEventTouchUpInside]; [loginview.registButton addTarget:self action:@selector(regist:) forControlEvents:UIControlEventTouchUpInside]; // 添加到視圖 [self.view addSubview:loginview]; [loginview release]; // 創建找回密碼頁面 FindPasswordView *findpasswordview = [[FindPasswordView alloc] initWithFrame:CGRectMake( 0 , 0 , 320 , 480 )]; findpasswordview.backgroundColor = [UIColor whiteColor]; // 設置找回密碼頁面背景顏色 // 設置找回密碼頁面tag值 findpasswordview.tag = 101 ; // 找回密碼頁面按鈕觸發事件 [findpasswordview.undo addTarget:self action:@selector(undo:) forControlEvents:UIControlEventTouchUpInside]; [findpasswordview.tofind addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside]; // 添加到視圖 [self.view addSubview:findpasswordview]; [findpasswordview release]; // 創建注冊頁面 RegisterView *registerview = [[RegisterView alloc] initWithFrame:CGRectMake( 0 , 0 , 320 , 480 )]; registerview.backgroundColor = [UIColor whiteColor]; // 設置注冊頁面背景顏色 // 設置注冊頁面tag值 registerview.tag = 102 ; // 注冊頁面按鈕觸發事件 [registerview.registButton addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside]; [registerview.undoButton addTarget:self action:@selector(regist:) forControlEvents:UIControlEventTouchUpInside]; // 注冊頁面添加到視圖 [self.view addSubview:registerview]; [registerview release]; // Do any additional setup after loading the view. } - ( void )didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
AppDelegate中代碼
// AppDelegate.h中代碼 #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (retain, nonatomic) UIWindow * window; @end // AppDelegate.m中代碼 #import " AppDelegate.h " #import " RootViewController.h " @implementation AppDelegate -( void )dealloc { [_window release]; [super dealloc]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary * )launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; // 給window創建根視圖控制器 RootViewController *rootVC = [[RootViewController alloc] init]; // 視圖控制器初始化 self.window.rootViewController = rootVC; // 設置窗口的根視圖控制器是rootVC [rootVC release]; // 控制器釋放 [self.window makeKeyAndVisible]; return YES; } - ( void )applicationWillResignActive:(UIApplication * )application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - ( void )applicationDidEnterBackground:(UIApplication * )application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - ( void )applicationWillEnterForeground:(UIApplication * )application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - ( void )applicationDidBecomeActive:(UIApplication * )application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - ( void )applicationWillTerminate:(UIApplication * )application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
效果圖
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
