IOS工作筆記(二),ios工作筆記

來源:互聯網
上載者:User

IOS工作筆記(二),ios工作筆記

1.懶載入(即消極式載入)只有被調用時才初始化,防止資源浪費,需要重寫對象 的get方法,且必須寫成成員變數形式,如_imageData。可以這麼寫,如:

 1 @property(nonatomic,strong) NSArray *imageData; 2  3 -(NSArray *)imageData{ //重寫imageData的get方法 4     if(_imageData == nil){ 5         //初始化資料 6         NSMutableDictionary *image1 = [NSMutableDictionary dictionary]; 7         image1[@"icon"] = @"hello";  8         image1[@"desc"] = @"這是一張圖片的說明"; 9 10         NSMutableDictionary *image2 = [NSMutableDictionary dictionary];11         image2[@"icon"] = @"hello2";12         image2[@"desc"] = @"這是一張圖片的說明2";13 14         //self.imageData = @[image1,image2];//原來的寫法15         _imageData = @[image1,image2]; //懶載入的寫法16 17     }18     return _imageData;19 }

上述get方法不能這樣寫

1 -(NSArray *) imageData{2     if(self.imageData == nil){3         //4     }5     return self.imageData;6 }

因為這樣寫會陷入死迴圈,self.imageData 調用的就是imageData的get方法,所以得寫成成員變數"_imageData"這種形式的。

 

2.當資料量較多時,可以把資料存放區在plist檔案中,讀取plist檔案可以這樣來,固定格式,3行。

//一個bundle就表示一個檔案夾,利用mainBundle可以訪問手機的任何資源NSBundle *bundle = [NSBundle mainBundle];//擷取plist檔案路徑,並且這種路徑是全路徑,而不單單是通過檔案名稱擷取NSString *path = [bundle pathForResource:@"imageData" ofType:"@"plist"];_imageData = [NSArray arrayWithContentOfFile:path];//一般含File的路徑必須是全路徑,而不是和imageNamed的那樣,只提供名稱就行。

 

3.出現這種錯誤:

undefined symbols for architecture arm64:
"_OBJC_CLASS_$_BMKMapView", referenced from:"………………

這是程式不支援64位命令,有兩種修改方法
①在target的build settings中的valid Architectures ,將arm64刪除,並將Build Active Architecture Only設為NO,即可。

②另一種是只需修改Architectures的Architectures改為

${ARCHS_STANDARD_32_BIT}

即可,原來的為

$(ARCHS_STANDARD)

4.關於UIButton的點擊事件,若將一個btn添加到view裡面

[self addSubView:btn];

若該button的座標位於view之外,那麼button就接受不到按鈕事件。其餘的類似

 

5.viewWithTag可以根據tag值很快地擷取到所需要的view,不過對tag的大小有要求,因為tag值較小的,如0——100為蘋果保留所用。

所以我們自訂時需要把tag設的很大才行,如100000.

 

6.userInteractionEnabled是UIView的屬性,可以設定視圖是否可以接受使用者的事件和訊息,是否可以跟使用者互動。若不想,設定為NO即可。

如當一個父視圖中包含兩個子視圖a,b。但b被a覆蓋了,這樣b就不能響應事件,這時若設定

a.userInteractionEnabled = NO;b.userInteractionEnabled = YES;

這樣b就可以接收到訊息事件了。

 

7.keyWindow是用來接受鍵盤以及非接觸類的資訊,而且每個程式中都只能有一個window是keywindow.

//定義keywindowUIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];

 

8.擷取UIButton上的文字,可以用

myButton.titleLabel.text

 

9.定義可變數組及初始化

 1 @property(strong, nonatomic) NSMutableArray *allMedicBtn; 2  3 -(NSMutableArray *) allMedicBtn{ 4     if(!_allMedicBtn){ 5       _allMedicBtn = [NSMutableArray array];//第20條筆記講述這樣寫的原因 6     } 7     return _allMedicBtn; //return self.allMedicBtn 8     //這裡_allMedicBtn相當於self.allMedicBtn,這其實調用的是allMedicBtn的get方法,因此這兩種寫法是等價的 9     // self.allMedicBtn 與 _allMedicBtn;10 }

 

10.[NSMutableArray array]和[[NSMutableArray alloc] init]的區別;

[NSMutableArray array]相當於[[[NSMutableArray alloc] init] autorelease],autorelease的對象有時會在不用的時候已經release了,後面想用時還得retain一次。

 

11.xcode的快速鍵

xocde沒有eclipse那樣的花括弧成對的提示符,只能雙擊任一花括弧,成對的花括弧就會以陰影形式出現。
展開、閉合單一方法,快速鍵是 command + option + 左右鍵,(左鍵閉合,右鍵展開)
要想對所有方法做這些操作,需要加shift,command + option + shift + 左右鍵

 

12.在普通的view中,一些方法可以載入initWithFrame中,如:

1 -(id)initWithFrame:(CGRect)frame{2     self = [super initWithFrame:frame];3     if (self) {4         //添加選取器5         [self setupChoiceView];6     }7     return self;8 }

但在tableViewCell中,這些方法最好寫在initWithStyle中,方便複用。如

1 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier2 {3     if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {4         //自訂方法5         [self setupFTEHitorySwitchBtn];6     }7     return self;8 }

 

13.ios中座標的定義,當把UIView添加到keyWindow後,view相對的座標原點就會發生變化,ipad橫屏程式中,預設進入首頁時的home鍵是位於左邊的,此時的座標原點應該位於右上方(倒過來看還是左上方)。

而假如不是加入到keyWindow中,那麼座標原點還是位於左上方。

 

而對於keyWindow的定義是在appDelegate中進行的,如

1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{2       self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];3       self.window.rootViewController = [[LoginViewController alloc]init];4       [self.window makeKeyAndVisible];5 6       return YES;7 }

對於這種問題,則可以這麼解決(這是view中添加view)

①在view的.h檔案中,添加新方法。如

1 @class HomeViewController; //為下邊的方法要使用的類提供介面2 @interface MedicineSelectView:UIView3 -(id)initWithFrame:(CGRect) frame andSuperViewController:(HomeViewController *) home;4 @end

②在view的.m檔案中添加自訂的方法,原來的方法不再需要。原來的為

1 -(id)initWithFrame:(CGRect)frame{2     self = [super initWithFrame:frame];3     if(self){4         [self setupAddMedicineBtn];5     }6     return self;7 }

此時,上述方法應改為

 1 -(id)initWithFrame:(CGRect) frame andSuperViewController:(HomeViewController *) home{ 2     self = [super initWithFrame:frame]; 3     if(self){ 4         self.home = home; //當然還需要在.m中聲明如下 5         /** 6         *@property (weak, nonatomic) UIViewController *home; 7         *因為self.home = home中等號左邊的home是指的property的home,等號右邊的home是方法中的參數 8         */ 9         [self setupAddMedicineBtn];10     }11     return self;12 }

③在view的.m檔案中添加該view時也會發生變化。原來是添加到keyWindow內,如

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];[keyWindow addSubView:self.allMedicineView];

改變後的方法是將view添加到controller所在的view

[self.home.view addSubview:self.allMedicineView];

④在要將該view添加的controller中,定義該view的大小位置時也需變化。
原來的是

MedicineSelectView *medicineView = [[MedicineSelectView alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];

改變後的是

MedicineSelectView *medicineView = [[MedicineSelectView alloc] initWithFrame:CGRectMake(0, 0, 300, 44) andSuperViewcontroller:self];//self指的是要添加該view的控制器

 

14.ipad解決鍵盤遮住文字框的問題,這種方法較簡單,是用來監聽文字框的。如有兩個文字框userName,passWord(都為UITextField),那麼可以這樣。

①先對兩個文字框添加監聽事件,這裡只以userName為例

1 [userName addTarget:self action:@selector(textFieldBeginEdit:) forControlEvents:UIControlEventEditingDidBegin];2 [userName addTarget:self action:@selector(textFieldEndEdit:) forControlEvents:UIControlEventEditingDidEnd];

②添加具體的移動方法

//開始編輯時,整體上移-(void)textFieldBeginEdit:(UITextField *)textField{    [self moveView:-230]; }//結束編輯後,移回原來位置-(void)textFieldEndEdit:(UITextField *)textField{    [self moveView:230];}//view移動的具體方法-(void) moveView:(float)move{    NSTimeInterval animationDuration = 0.3f;    CGRect frame = self.view.frame;    frame.origin.x += move; //view的X軸上移    self.view.frame = frame;    [UIView beginAnimations:@"2" context:nil];// 2是動畫的標識    [UIView setAnimationDuration:animationDuration];    self.view.frame = frame;    [UIView commitAnimations];}

③點擊其它地區和鍵盤右下角“隱藏鍵盤”那個按鈕時隱藏鍵盤,寫在viewDidLoad裡

- (void)viewDidLoad {        //點擊其他地區關閉鍵盤    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKeyBoard)];    gesture.numberOfTapsRequired = 1; //確認是單擊其他地區,而不是雙擊等其它操作    [self.view addGestureRecognizer:gesture];        //該方法特別重要    //點擊鍵盤右下角“隱藏鍵盤”那個按鈕,關閉鍵盤並且恢複view的位置    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyBoard) name:UIKeyboardWillHideNotification object:nil];}//隱藏鍵盤,並且view恢複初始位置-(void)hideKeyBoard{    [userName resignFirstResponder];    [passWord resignFirstResponder];    [self resumeView];}//恢複view的原來位置-(void)resumeView{    NSTimeInterval animationDuration=0.5f;    [UIView beginAnimations:@"3" context:nil];    [UIView setAnimationDuration:animationDuration];    [UIView commitAnimations];}

 

15.UITableViewCell的重用,比如表格有100行資料,每頁只能顯示10行,則當向下劃時,第11行出現,第1行消失時,為了節約記憶體,就要將第1行的cell複用到第11行。代碼如下:

+(instancetype) cellWithTableView:(UITableView *) tableView{    static NSString *ID = @"MedicineProcessCell";    MedicineProcessView *cell = [tableView dequeueReusableCellWithIdentifier:ID];    //表示定義一個cell,在tableView的可重用隊列中尋找有MedicineProcessCell標識的UITableViewCell,已進行重用    //如果隊列中有這樣的UITableView,則把它賦值給cell;若沒有,則返回nil給cell    if(!cell){        cell = [[MedicineProcessView alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];    }    return cell;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.