搞编程做项目的,没碰到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