stackoverflow上關于iOS的票數最多(最常見)的15個問題

圖片來源@百度圖片

搞編程做項目的,沒碰到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

二、atomicnonatomic的區別是什么

如下,atomicnonatomic在聲明成員變量時有什么作用:

@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?

三、怎么讓UITextField在彈出鍵盤時向上移動

比如一些填寫信息的頁面,有些UITextField位置比較靠下,鍵盤彈出后會把它遮住,導致看不見輸入的內容。

答:

通過監聽鍵盤的狀態移動UITextField所在的view。

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?

五、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

六、在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?

七、如何對一個包含自定義對象的可變數組進行排序

答:
sortedArrayUsingComparator:

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

九、控制器之間的傳值

答:

屬性、代理、block...

Passing Data between View Controllers

十、怎么用windows設備開發iPhone軟件

注:這個問題七年了,至今未有答案被采納。

How can I develop for iPhone using a Windows development machine?

十一、怎么讓tableView的cell選中時的高亮狀態失效

答:
cell.selectionStyle = UITableViewCellSelectionStyleNone;

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

十三、怎么改變iOS app的名字

注:被采納的答案是08年的,已不準確,可以點進去看詳細答案。

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

錯誤信息:

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

寫到這里

寫完了發現有些被采納答案已經不適用于現在,但是里面一些討論的內容,以及其他一些答案,還是很有價值,因此堅持把本文寫完。

歡迎拍磚和轉載(請標明作者和出處,謝謝??),如果覺得對你有用可以點個贊哦,O(∩_∩)O~

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容