ios成長之每日一遍(day 2)

來源:互聯網
上載者:User

標籤:android   style   class   blog   c   code   

接著下來簡單說說Label(相當於android的textview)和button的使用, 由雩都是與上篇的AppDelegate一致, 所以這一篇就說說ViewController與xib的使用唄。

BIDViewController.h

#import <UIKit/UIKit.h>@interface BIDViewController : UIViewController    // 類的開始@property (weak, nonatomic) IBOutlet UILabel *statusLabel;    
// a. @property是定義屬性的關鍵字; b. weak與strong關鍵字的區別, strong表示對象沒有被釋放則一直持有對象, 而weak指向的地址一旦被釋放,這些指標都將被賦值為nilc. atomic、nonatomic、assign、copy、retain關鍵字的區別, atomic是預設的設定,提供多安全執行緒, 但是會影響效率; nonatomic線程不安全, 提高效能; assign用於基本的資料類型; retain用於NSObject和其子類; copy複製對象到新的地址; ps:copy 其實是建立了一個相同的對象,而 retain 不是:比如一個NSString 對象,地址為 0×1111,內容為 @”STR” , copy 到另外一個 NSString 之後,地址為 0×2222 ,內容相同,新的對象 retain 為 1 ,舊有對象沒有變化; retain 到另外一個 NSString 之後,地址相同(建立一個指標,指標拷貝),內容當然相同,這個對象的 retain 值 +1。也就是說, retain 是指標拷貝, copy 是內容拷貝。
IBOutlet只是一個標記, 用於表示已在xib定義實現, xib串連時按住ctrl鍵, 滑鼠從Files Owner拖動到在代碼中標有IBOutlet的空間上, 然後在彈出框選擇- (IBAction)buttonPressed:(UIButton *)sender;    // 定義操作觸發的函數, IBAction也是一個標識, 標記這是觸發函數, xib中在需要觸發函數的控鍵上按住ctrl鍵, 滑鼠從該控鍵拖動到File‘s Owner並選擇相應的函數即可@end    // 類的結束

 

 BIDViewController.m

#import "BIDViewController.h"@implementation BIDViewController@synthesize statusLabel;    // 與property一般成對出現- (IBAction)buttonPressed:(UIButton *)sender {    NSString *title = [sender titleForState:UIControlStateNormal];   // 返回控鍵指定狀態的title     NSString *plainText = [NSString stringWithFormat:@"%@ button pressed.", title];    // %@表示這個參數時物件類型    /***  讓部分文字變色方法  **/    NSMutableAttributedString *styledText = [[NSMutableAttributedString alloc]                                             initWithString:plainText];    // 這文法有點兒像json哇    NSDictionary *attributes = @{    NSFontAttributeName : [UIFont boldSystemFontOfSize:statusLabel.font.pointSize]    };    NSRange nameRange = [plainText rangeOfString:title];        [styledText setAttributes:attributes                        range:nameRange];    statusLabel.attributedText = styledText;}@end


第二篇結束!!!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.