標籤: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鍵, 滑鼠從File‘s 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
第二篇結束!!!