標籤:
搞編程做項目的,沒碰到bug、遇到問題,基本不可能。stackoverflow就是一個大型的開放的FAQ平台,你是問題製造者,也是答案提供者。本文列出至今stackoverflow上關於iOS的票數最高(最常見)的15個問題,僅為了大家能夠更方便、直接、快速的找到自己想要的答案,也許其中某個(些)問題就是你已經碰到或者即將碰到的。
(這裡的答案都是簡單描述,有需要的話可以進入問題原頁面查看詳細答案)
一、怎麼把UILabel的內容豎直方向靠上排列
我們都知道NSTextAlignment有五個值:
NSTextAlignmentLeft; // 水平居左
NSTextAlignmentCenter; // 水平置中
NSTextAlignmentRight; // 水平居右
NSTextAlignmentJustified; // 合理鋪滿 等同於居左
NSTextAlignmentNatural; // 預設 等同於居左
卻沒有想要的豎直方向居上或者居下:
NSTextAlignmentTop;
NSTestAlignmentBottom;
預設都是豎直置中的,怎麼實現居上或者居下?
答:
UILabel不能設定豎直方向的排列布局,但是可以通過sizeToFit改變label的frame來實現曲線救國。
Vertically align text to top within a UILabel
http://stackoverflow.com/questions/1054558/vertically-align-text-to-top-within-a-uilabel
二、atomic和nonatomic的區別是什麼
如下,atomic和nonatomic在聲明成員變數時有什麼作用:
@property(nonatomic, retain) UITextField *userName;
@property(atomic, retain) UITextField *userName;
@property(retain) UITextField *userName;
答:
有了atomic,合成的setter/getter(非自己重寫的)可以經常返回一個完整的值(來自getter或者setter),即便其他線程進行中setter活動,nonatomic沒有這個保證。但是atomic並不能保證安全執行緒,所以為了運行速度和使用者體驗,一般用nonatomic,系統預設atomic。
What’s the difference between the atomic and nonatomic attributes?
http://stackoverflow.com/questions/588866/whats-the-difference-between-the-atomic-and-nonatomic-attributes
三、怎麼讓UITextField在彈出鍵盤時向上移動
比如一些填寫資訊的頁面,有些UITextField位置比較靠下,鍵盤彈出後會把它遮住,導致看不見輸入的內容。
答:
通過監聽鍵盤的狀態移動UITextField所在的view。
How to make a UITextField move up when keyboard is present?
http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present
四、怎麼檢查iOS或者OS X裝置的網路連接狀態
答:
藉助於開源架構tonymillion/Reachability
How to check for an active Internet connection on iOS or OSX?
http://stackoverflow.com/questions/1083701/how-to-check-for-an-active-internet-connection-on-ios-or-osx
五、performSelector:方法將會因為不知道它的選取器而導致記憶體泄露
代碼:
[_controller performSelector:NSSelectorFromString(@"someMethod")];
ARC編譯器對上一行代碼給出了一個警告:
"performSelector may cause a leak because its selector is unknown".
答:
利用IMP和函數指標方法配合解決。
performSelector may cause a leak because its selector is unknown
http://stackoverflow.com/questions/7017281/performselector-may-cause-a-leak-because-its-selector-is-unknown
六、在Objective-C中怎麼檢查一個字串中是否還有另外一個字串
答:
iOS8或OS X Yosemite之後:
- (BOOL)containsString:(NSString *)str NS_AVAILABLE(10_10, 8_0);
之前:
NSString *string = @"hello bla bla";
if ([string rangeOfString:@"bla"].location == NSNotFound) {
NSLog(@"string does not contain bla");
} else {
NSLog(@"string contains bla!");
}
How do I check if a string contains another string in Objective-C?
http://stackoverflow.com/questions/2753956/how-do-i-check-if-a-string-contains-another-string-in-objective-c
七、如何對一個包含自訂對象的可變數組進行排序
答:
sortedArrayUsingComparator:
How to sort an NSMutableArray with custom objects in it?
http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it
八、用自動布局在tableView中給動態cell布局和自適應高度
答:
1、添加約束
2、確定cell的唯一的標識符
3、使用row的估計高度(iOS8)
4、添加高度緩衝(需要的話)
Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights
九、控制器之間的傳值
答:
屬性、代理、block…
Passing Data between View Controllers
http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers
十、怎麼用windows裝置開發iPhone軟體
註:這個問題七年了,至今未有答案被採納
How can I develop for iPhone using a Windows development machine?
http://stackoverflow.com/questions/22358/how-can-i-develop-for-iphone-using-a-windows-development-machine
十一、怎麼讓tableView的cell選中時的高亮狀態失效
答:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
How can I disable the UITableView selection highlighting?
http://stackoverflow.com/questions/190908/how-can-i-disable-the-uitableview-selection-highlighting
十二、怎麼在iOS7上改變狀態列的顏色
答:
1、在.plist檔案裡把UIViewControllerBasedStatusBarAppearance設為YES。
2、在viewDidLoad添加[self setNeedsStatusBarAppearanceUpdate]..
3、在控制裡添加如下代碼:
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
How to change Status Bar text color in iOS 7
http://stackoverflow.com/questions/17678881/how-to-change-status-bar-text-color-in-ios-7
十三、怎麼改變iOS app的名字
註:被採納的答案是08年的,已不準確,可以點進去看詳細答案。
How to change the name of an iOS app?
http://stackoverflow.com/questions/238980/how-to-change-the-name-of-an-ios-app
十四、Xcode7報錯“iOS發布認證丟失”
打包上傳AppStore時報如下錯誤:
Failed to locate or generate matching signing assets
Xcode attempted to locate or generate matching signing assets and failed to do so because of the following issues.
Missing iOS Distribution signing identity for ... Xcode can request one for you.
答:
首先、下載安裝WWDR中級認證,
其次、在鑰匙鏈訪問應用中選取系統鑰匙串…
Xcode 7 error: “Missing iOS Distribution signing identity for …”
http://stackoverflow.com/questions/32821189/xcode-7-error-missing-ios-distribution-signing-identity-for
十五、傳輸安全阻止明文HTTP
錯誤資訊:
Transport security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app‘s Info.plist file.
答:
在工程info下面的配置裡把Allow Arbitrary Loads設為YES。然後在Exception Domians下面添加要串連的主網域名稱。
Transport Security has Blocked a cleartext HTTP
http://stackoverflow.com/questions/31254725/transport-security-has-blocked-a-cleartext-http
stackoverflow上關於iOS的票數最多(最常見)的15個問題