iPhone開發(fā)知識總結(jié) 上篇
iPhone開發(fā)知識總結(jié) 上篇是本文要介紹的內(nèi)容,主要講述的是iphone開發(fā)應用中Atomic 和 nonatomic 屬性的理解,來看詳細內(nèi)容。
1、關(guān)于不同分辨率的屏幕顯示問題
iphone 3 和 4分辨率各不相同。加載圖像的時候只需要指定基本的文件名,例如:pic.png根據(jù)不同分辨率,如果在iphone 4 上會自動加載pic@2x.png(如果存在)。
2、關(guān)于Atomic 和 nonatomic 屬性的理解
atomic的訪問控制器只用在沒有垃圾回收的環(huán)境中。
使用atiomic能保證線程安全的,保證一個屬性的get/set在一個線程中必須完成之后才能有其他的線程訪問它。
- //@property(nonatomic, retain) UITextField *userName;
- //Generates roughly
- - (UITextField *) userName {
- return userName;
- }
- - (void) setUserName:(UITextField *)userName_ {
- [userName_ retain];
- [userName release];
- userName = userName_;
- }
Now, the atomic variant is a bit more complicates:
- //@property(retain) UITextField *userName;
- //Generates roughly
- - (UITextField *) userName {
- UITextField *retval = nil;
- @synchronized(self) {
- retval = [[userName retain] autorelease];
- }
- return retval;
- }
- - (void) setUserName:(UITextField *)userName_ {
- @synchronized(self) {
- [userName_ retain];
- [userName release];
- userName = userName_;
- }
Atomic版本加了一個鎖保證線程安全的,atomic保證userName方法獲得的不是一個釋放過的值。
3、使用歸檔程序復制對象(深復制)
- NSMutableArray *dataArray = [NSMutableArray arrayWithObjects:[NSMutableString stringWithString: @"one"], ....];
- NSMutableArray *dataArray2;
- NSData data = [NSKeyedArchiver archivedDataWithRootObject:dataArray];
- dataArray2 = [NSKeyedUnarchiver unarchiveObjectWithData: data];
4、在iphone開發(fā)中,設置navigationController中返回按鈕的標題,默認為前一個視圖中標題的title,
如果設置,在前一個視圖中寫下:
- UIBarButtonItem *temporaryBarButtonItem=[[UIBarButtonItem alloc] init];
- temporaryBarButtonItem.title=@"Back";
- self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
- [temporaryBarButtonItem release];
5、在table view 中加入手勢或者事件觸摸機制,需要實現(xiàn)方法
- -(BOOL)canBecomeFirstResponder {
- return YES;
- }
應該如下判斷用戶觸摸的是哪一個cell
- CGPoint pinchLocation = [pinchRecognizer locationInView:self.tableView];
- NSIndexPath *newPinchedIndexPath = [self.tableView indexPathForRowAtPoint:pinchLocation];
6、- (void)dismissModalViewControllerAnimated:(BOOL)animated
UIViewController的這個方法很有意思,一般parent view controller都有責任調(diào)用此方法來消除其通過presentModalViewController:animated: 方法展現(xiàn)的modal view controller。但是如果你在modal view controller中調(diào)用此方法,modal view controller會自動的把此消息轉(zhuǎn)發(fā)給parent view controller。
小結(jié):iPhone開發(fā)知識總結(jié) 上篇的內(nèi)容介紹完了,如果你對iPhone開發(fā)知識總結(jié) 下篇的相關(guān)內(nèi)容,***希望本文能對你有所幫助!
iphone開發(fā)感興趣的話,請參考