1. 程式人生 > >Do not just be a pragrammer

Do not just be a pragrammer

LTView

自定義檢視的步驟

  1. 建立一個繼承自UIView的類
  2. 重寫新類的初始化方法
  3. 把想封裝的檢視新增封裝到 新類裡面(初始化到新類中)
  4. 為了方便外部進行賦值取值 把新增的檢視寫成屬性(別忘了釋放記憶體)
  5. 測試一下

自定義檢視的好處

提高工作效率 大大提高程式碼的複用性

具體實現

自定義Label-TextField檢視

建立LTView類繼承自LTView

// 將要建立的控制元件用屬性寫在.h檔案中
@property (strong, nonatomic) UILabel *label;
@property (strong, nonatomic) UITextField *textField;

// 在.m檔案中重寫初始化方法 在初始化LTView的同時新增控制元件
- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // 獲取動態高度 CGFloat height = frame.size.height; // 獲取動態寬度 CGFloat width = frame.size.width; // 根據上面的寬度 來新增label self.label = [[UILabel alloc] initWithFrame:CGRectMake(0
, 0, width / 3, height)]; self.label.textAlignment = NSTextAlignmentCenter; self.label.backgroundColor = [UIColor blueColor]; // 新增到自己身上 [self addSubview:self.label]; // 釋放空間 [_label release]; // 動態獲取值構建frame可以適應任何尺寸的螢幕,並且當需求改變時,只需更改第一個控制元件,其餘控制元件位置隨之而改 // 初始化一個TextField
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(width / 3 + 20, 0, width / 3 * 2 - 20, height)]; // 設定背景色 self.textField.backgroundColor = [UIColor greenColor]; // 新增到檢視上 [self addSubview:self.textField]; // 釋放空間 [_textField release]; } return self; }

建立登陸介面LoginView利用自定義檢視進行構建

// 首先引入標頭檔案LTView.h
// 在標頭檔案中將需要的控制元件寫成屬性
@property (nonatomic, retain) LTView *userNameLTView;
@property (nonatomic, retain) LTView *passWordLTView;
@property (nonatomic, retain) UIButton *loginButton;
@property (nonatomic, retain) UIButton *registButton;
@property (nonatomic, retain) UIButton *findPasswordButton;

// 在.m檔案中實現新增控制元件
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
    // 初始化輸入賬戶名的l - T
        self.userNameLTView = [[LTView alloc] initWithFrame:CGRectMake((kScreenWidth - 300) / 2, 100, 300, 50)];
        self.userNameLTView.backgroundColor = [UIColor redColor];

        [self addSubview:self.userNameLTView];
        [self.userNameLTView release];
     // 初始化輸入密碼的l - T   
        self.passWordLTView = [[LTView alloc] initWithFrame:CGRectMake(self.userNameLTView.frame.origin.x, self.userNameLTView.frame.origin.y + self.userNameLTView.frame.size.height + kRowHeight, self.userNameLTView.frame.size.width, self.userNameLTView.frame.size.height)];
        self.passWordLTView.backgroundColor = [UIColor redColor];
        [self addSubview:self.passWordLTView];
        [self.passWordLTView release];

        // 迴圈button
        for (int i = 0; i < 3 ; i++) {
            UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
            button.frame = CGRectMake(50 + i * 100,300 , 80, 50);
            button.backgroundColor = [UIColor cyanColor];

            // 新增標籤 方便取出button 與屬性的button相對應
            button.tag = i + 100;
            [self addSubview:button];
        }
        // 屬性與迴圈建立的button進行關聯
        self.loginButton = (UIButton *)[self viewWithTag:100];
        self.findPasswordButton = (UIButton *)[self viewWithTag:101];
        self.registButton = (UIButton *)[self viewWithTag:102];

將屬性與迴圈建立的控制元件一一對應

// 除了利用tag值 還可以取出父檢視上的子檢視陣列,利用下標進行一一對應
NSArray *array = self.subviews;
self.loginButton = array[2];
[self.loginButton setTitle:@"登陸" forState:(UIControlStateNormal)];

在window上初始化並顯示

LoginView *login = [[LoginView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [login.loginButton setTitle:@"登陸" forState:(UIControlStateNormal)];
    [login.registButton setTitle:@"註冊" forState:(UIControlStateNormal)];
    [login.findPasswordButton setTitle:@"找回密碼" forState:(UIControlStateNormal)];
    [login.loginButton setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    [login.registButton setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    [login.findPasswordButton setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    [self.window addSubview:login];
    [login release];

重寫controller的loadView⽅法

建立⾃定義檢視物件,並指定為controller的view

 LoginView *logview = [[LoginView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    logview.tag = 1000;
    // 幫系統給self.view賦值
    self.view = logview;
    [logview release];

相關推薦

Do not just be a pragrammer

LTView 自定義檢視的步驟 建立一個繼承自UIView的類 重寫新類的初始化方法 把想封裝的檢視新增封裝到 新類裡面(初始化到新類中) 為了方便外部進行賦值取值 把新增的檢視寫成屬性(別忘了釋放記憶體) 測試一下 自定義檢視的好處 提高工

origin does not to be a git repository 問題解決

最近上傳程式碼到GitHub的時候,當我輸入 git push -u origin master的時候,它提示: origin does not to be a git repository 解決辦法:   1.重新輸入一次:git remote add origin [ema

solving Rubik's Cube could just be a really smart poltergeist | AITopics

This robotic Rubik's Cube is the product of a Japanese creator who's documented many of his creative projects on his YouTube channel, Human Controller. Yes

git上傳檔案時出現origin does not to be a git repository

最近上傳程式碼到GitHub的時候,當我輸入 git push -u origin master的時候,它提示: origin does not to be a git repository 重新輸入一次:git remote add origin [email prote

【墮落獸人的專欄】A good developer should know that development is not just programming; a great developer should know that development i

A good developer should know that development is not just programming; a great developer should know...

why not just do this stupid thing, this selfish thing… jump off a cliff into water of indeterminate depth" | AITopics

"It is with extraordinary sadness we can confirm the death of our friend and colleague, Anthony Bourdain," CNN said in a statement Friday morning. Christia

執行git命令時出現fatal: 'origin' does not appear to be a git repository錯誤

遠程 from pos pull reader could not span style fat 在執行git pull origin master時出現:   fatal: ‘origin‘ does not appear to be a git repository  

[ERROR ] Error parsing configuration file: /etc/salt/minion - conf should be a document, not <type 'str'>.

文件的 RR salt etc master str AS con nio 錯誤信息 [ERROR ] Error parsing configuration file: /etc/salt/minion - conf should be a document, not

The server cannot or will not process the request due to something that is perceived to be a client

HTTP Status 400 – Bad Request Type Status Report Description The server cannot or will not process the request due to something that i

Is The First True eGovernment A Goner? I Hope Not, Just.

Is The First True eGovernment A Goner? I Hope Not, Just.When we researched impacts of the ROCA vulnerability, the Estonian government limited the impact wi

python出現float() argument must be a string or a number, not 'map'的錯誤

報錯:TypeError: float() argument must be a string or a number, not 'map' 這時候,首先你肯定是用了map()這個函式,不管你用map這個函式把資料轉變成了什麼格式,不管是int還是float還是其它的,最後

python Bug記錄-int() argument must be a string, a bytes-like object or a number, not 'dict'

程式碼如下: error_msg = "" if request.method == "POST": new_dayincome = request.POST.get("dayincome_time", None) total_bath =

I'm not just a Coder...

     谷歌Android大學生挑戰賽比賽結果公佈已過去了一個半月,還記得當時自己滿懷著期待開啟挑戰賽官網檢視比賽結果,我把頁面重新整理了一遍又一遍,說實話,看到結果後,我不敢相信自己的眼睛,獲獎作品列表裡面竟然沒有我的作品,連個最低的優秀獎都沒評上。我絞盡腦汁都想不明白

Ensure that you have installed a JDK (not just a JRE) and configured your JAVA_HOME system variable

       出現錯誤的原因是:           gradle的版本過低導致的       解決

【davidsu33的專欄】To be or not to be, It's a problem!!!

Twisted 基於python開發的跨平臺的網路庫,可以說只要是伺服器涉及到的,都可以用。包含http、ftp、mail、ssh、xmpp、irc也包含了底層的通訊庫,包括twisted.basic中的基於位元組或則基於行的通訊。twisted最大的閃光點在於全面,而

jetty訪問jsp頁面出現PWC6345: There is an error in invoking javac. A full JDK (not just JRE) is required

應用場景: 專案架構:  Emmbed Jetty + Spring + JSP+Maven。 專案通過maven-shade-plugin外掛打為jar包,通過命令:“java -jar xxx.jar” 方式執行。 注意:伺服器端安裝JRE環境,而非JDK。如果安裝JD

to be or not to be, that is a question...

很少釋出負能量的東西,沒地方寫,就放這裡吧。 時間過得夠快的,本科畢業一年了,研究生入學也一年了,今天心情不太好,想總結一下自己在這一年都幹了什麼。 為時一年的雁棲湖集中教學馬上就要結束了,我在努力地回想,除了每天都在寫程式碼,程式碼量確實上去了,但是這一年來好像也沒幹

statsmodels.tsa.arima_model預測時報錯TypeError: int() argument must be a string, a bytes-like object or a number, not 'Timestamp'

進行 時報 參數 csv 別人 間隔 get req ice 在 python 中用 statsmodels創建 ARIMA 模型進行預測時間序列: import pandas as pd import statsmodels.api as sm df = pd.read

庖丁解牛 dic home should not be a file, but a directory!

最近一個專案,用到了paoding分詞器。 在開發階段我將詞庫放到了src目錄,配置檔案使用了classpath:dic,目的是為了增加可移植性。 發現問題:在啟動伺服器的時候丟擲 net.paod

To be or not to be,that's a question!

1. 背景:          SIP提供給客戶端伺服器收到來自客戶端請求的IP地址,這個源IP地址被放在”received”引數中傳送,它 放 於響應的頂端頭欄位中。對NAT穿越有很大作用。     但有很多情況下,僅一個ip地址資訊還不夠,還有需要埠資訊。於是有了第二步