IOS-學習記錄【01】
阿新 • • 發佈:2017-08-02
cancel screen color avi 需要 leg tco order isp
很久都沒寫博客了,從今天開始會記錄一些學習的心得/過程。現在對學習iOS的興趣也越來越濃。微信小程序的開發已經告一段落,目前會對產品進行代碼改進和功能優化。
第一個iOS應該從今天開始誕生,這兩天剛開始接觸一些iOS的語法,OC對象的概念還沒有完全熟悉,感覺很全新。
// // ViewController.m // more // // Created by vjia on 2017/8/1. // Copyright ? 2017年 vjia. All rights reserved. // #import "ViewController.h" #import "UIKit/UIkit.h" @interface ViewController (){ UITextField *_txtUserName;//聲明控件變量 UITextField *_txtPassword; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self LoadLoginForm];//在當前對象中加載 } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)LoadLoginForm{//實例化加載控件 UILabel *lbUserName=[[UILabel alloc] initWithFrame:CGRectMake(20, 160, 100, 40)];//繪制文本框大小 lbUserName.text=@"用戶名:";//設置文本框文字 [self.view addSubview:lbUserName]; _txtUserName=[[UITextField alloc] initWithFrame:CGRectMake(80, 160, 280, 40)];//繪制文本框大小 [_txtUserName setBorderStyle:UITextBorderStyleRoundedRect];//設置邊框樣式 _txtUserName.layer.borderColor=[[UIColor grayColor] CGColor];//設置邊框的顏色 _txtUserName.layer.borderWidth=0.7f;//設置邊框寬度 _txtUserName.layer.cornerRadius=6.0f;//設置圓角邊框大小 _txtUserName.placeholder=@"請輸入用戶名"; _txtUserName.clearButtonMode=UITextFieldViewModeAlways;//文本框右側總是顯示刪除角標 [self.view addSubview:_txtUserName];//將控件加入當前的view中 UILabel *lbPassWord=[[UILabel alloc] initWithFrame:CGRectMake(20, 220, 100,40)]; lbPassWord.text=@"密碼:"; [self.view addSubview:lbPassWord]; _txtPassword=[[UITextField alloc] initWithFrame:CGRectMake(80, 220, 280, 40)]; [_txtPassword setBorderStyle:UITextBorderStyleRoundedRect]; _txtPassword.layer.borderColor=[[UIColor grayColor] CGColor]; _txtPassword.layer.borderWidth=0.7f; _txtPassword.layer.cornerRadius=6.0f; _txtPassword.placeholder=@"請輸入密碼"; _txtPassword.clearButtonMode=UITextFieldViewModeAlways; [self.view addSubview:_txtPassword]; UILabel *lbForget=[[UILabel alloc] initWithFrame:CGRectMake(20, 280, 100, 50)]; lbForget.text=@"忘記密碼?"; [lbForget setTextColor:[UIColor blueColor]]; [self.view addSubview:lbForget]; UILabel *lbReg=[[UILabel alloc]initWithFrame:CGRectMake(320, 280, 100, 50)]; lbReg.text=@"註 冊"; [lbReg setTextColor:[UIColor redColor]];//設置字體的顏色 [self.view addSubview:lbReg]; CGRect rect=[[UIScreen mainScreen] bounds];//獲取主屏幕對象 CGSize size=rect.size;//屏幕大小 CGFloat width=size.width;//寬度 UIButton *btnLogin=[UIButton buttonWithType:UIButtonTypeCustom];//如果按鈕需要自定義背景圖片 按鈕必須設置成自定義模式 否則會看見藍色的圖標 btnLogin.frame=CGRectMake(0,400, width, 80+10); //[btnLogin setBackgroundColor:[UIColor redColor]];//設置背景顏色為紅色 [btnLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [btnLogin setTitle:@"登 錄" forState:UIControlStateNormal]; [btnLogin.self addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside]; [btnLogin setImage:[UIImage imageNamed:@"SourceBundle.bundle/login"] forState:UIControlStateNormal];//引用資源文件中的圖片作為背景圖 [btnLogin setImageEdgeInsets:UIEdgeInsetsMake(0, 25, 20, 25)]; [self.view addSubview:btnLogin]; UINavigationBar *navigationbar=[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, width, 50+10)];//繪制導航 [self.view addSubview:navigationbar];//加入到當前的view中 UINavigationItem *navitem=[[UINavigationItem alloc] initWithTitle:@"登錄"];//創建導航內容並設置標題 UIBarButtonItem *barbutton=[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(login)];//在導航欄上創建一個按鈕 文字為返回 加入觸發事件的方法 navitem.leftBarButtonItem=barbutton;//按鈕顯示在導航欄的左側 [navigationbar pushNavigationItem:navitem animated:NO];//將導航欄顯示出來 } -(void)login{//登錄方法 NSString *message=[NSString stringWithFormat:@"戶名 %@ 密碼 %@",_txtUserName.text,_txtPassword.text];//聲明一個string類型的變量 並接收文本框的輸入的內容 UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"溫馨提示" message:message delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil];//初始化彈窗 [alert show];//彈出模態窗體提示 } @end
相關的說明已經在註釋裏面寫了。感覺這種方式剛開始學可能編寫代碼會相對繁瑣,後面應該會很好理解的。還有一種方式是拖控件來實現,這種會相對簡單些。
小白一個,僅記錄學習的心得與分析學習的過程。
IOS-學習記錄【01】