iPhone開發知識總結 上篇

來源:互聯網
上載者:User

iPhone開發知識總結 上篇是本文要介紹的內容,主要講述的是iphone開發應用中Atomic nonatomic 屬性的理解,來看詳細內容。

1、關於不同解析度的螢幕顯示問題 
 
iphone 3 和 4解析度各不相同。載入映像的時候只需要指定基本的檔案名稱,例如:pic.png根據不同解析度,如果在iphone 4 上會自動載入pic@2x.png如果存在)。

2、關於Atomic 和 nonatomic 屬性的理解

atomic的存取控制器只用在沒有記憶體回收的環境中。

使用atiomic能保證安全執行緒的,保證一個屬性的get/set在一個線程中必須完成之後才能有其他的線程訪問它。

 
  1. //@property(nonatomic, retain) UITextField *userName;  
  2. //Generates roughly   
  3. - (UITextField *) userName {  
  4.     return userName;  
  5.   }   
  6. - (void) setUserName:(UITextField *)userName_ {  
  7.     [userName_ retain];      
  8.     [userName release];     
  9.     userName = userName_;  
  10.  }  

Now, the atomic variant is a bit more complicates:

 
  1. //@property(retain) UITextField *userName;  
  2. //Generates roughly   
  3. - (UITextField *) userName {  
  4.     UITextField *retval = nil;      
  5.   @synchronized(self) {  
  6.           retval = [[userName retain] autorelease];  
  7.    }     
  8.     return retval;  
  9.  }   
  10. - (void) setUserName:(UITextField *)userName_ {    
  11.      @synchronized(self) {       
  12.      [userName_ retain];        
  13.      [userName release];       
  14.       userName = userName_;   
  15. }  
  16.   

Atomic版本加了一個鎖保證安全執行緒的,atomic保證userName方法獲得的不是一個釋放過的值。

3、使用歸檔程式複製對象深複製)

 
  1. NSMutableArray *dataArray = [NSMutableArray arrayWithObjects:[NSMutableString stringWithString: @"one"], ....];  
  2. NSMutableArray *dataArray2;  
  3.  
  4. NSData data = [NSKeyedArchiver archivedDataWithRootObject:dataArray];  
  5. dataArray2 = [NSKeyedUnarchiver unarchiveObjectWithData: data]; 

4、在iphone開發中,設定navigationController中返回按鈕的標題,預設為前一個視圖中標題的title,

如果設定,在前一個視圖中寫下:

 
  1. UIBarButtonItem *temporaryBarButtonItem=[[UIBarButtonItem alloc] init];  
  2. temporaryBarButtonItem.title=@"Back";  
  3. self.navigationItem.backBarButtonItem = temporaryBarButtonItem;  
  4. [temporaryBarButtonItem release]; 

5、在table view 中加入手勢或者事件觸摸機制,需要實現方法

 
  1. -(BOOL)canBecomeFirstResponder {  
  2. return YES;  

應該如下判斷使用者觸摸的是哪一個cell

 
  1. CGPoint pinchLocation = [pinchRecognizer locationInView:self.tableView];  
  2.  
  3. NSIndexPath *newPinchedIndexPath = [self.tableView indexPathForRowAtPoint:pinchLocation]; 

6、- (void)dismissModalViewControllerAnimated:(BOOL)animated

UIViewController的這個方法很有意思,一般parent view controller都有責任調用此方法來消除其通過presentModalViewController:animated: 方法展現的modal view controller。但是如果你在modal view controller中調用此方法,modal view controller會自動的把此訊息轉寄給parent view controller。

小結:iPhone開發知識總結 上篇的內容介紹完了,如果你對 iphone開發感興趣的話,請參考 iPhone開發知識總結 下篇的相關內容,最後希望本文能對你有所協助!

相關文章

聯繫我們

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