您好,欢迎访问一九零五行业门户网

iOS 开发百问(6)

61、警告“addexplicit braces to avoid dangling else”
所谓“危险的else”是类似这样的代码:
i
f(a== 10) printf("ten"); else printf("not ten"); a = 100;
编译器认为你的else 子句导致语义不清,你到底是什么意思?是无论 a 是否等于10 , if 执行完之后都要将 a 赋值为100,还是只想在 else 子句(即 a 不等于10 的时候)中将 a 赋值为 100?
如果是前者,正确的写法应该是:
if(a== 10) { printf("ten"); }else{ printf("not ten"); } a= 100; 如果是后者,正确的写法应该是: if(a== 10) { printf("ten"); }else{ printf("not ten"); a = 100; }
当然,对于c/c++/java 编译器来说,这只是一个小问题,并不会导致无法编译。编译器实际上是倾向于前者的,它自动按第一种情况处理。但它会警告你这是一种不好的代码风格,你可以用#pragma clang diagnostic ignored "-wswitch" 宏忽略该警告,或者将编译选项 missingbraces and parentheses 设置为 no。
62、ipad模拟器不显示 home 键
从xcode 4.3 开始,为了获得更大的用户可用空间,ipad 模拟器不显示 home 键。 你可以通过菜单“ 硬件 > 首页”或者快捷键⇧⌘h 来代替 home 键。
63、novisible @interface for 'nsurl' declares the selector 'query'
ios6 中,该方法被抛弃,请用 nsurl+parameters 替代。
64、certificateidentity 'iphone distribution' appears more than once
这是证书重复的错误,需要将钥匙串里重复的证书删掉编译才能通过。但是,如果你重启xcode ,会发现之前删除的证书又回来了。但当重新启动xcode时,xcode里的证书会被导进钥匙串,所以仅仅是删除钥匙串中重复证书是无效的。
相信 许多同学对 xcode 的这个 bug 深恶痛绝了,但除了反复地(但是徒劳地)从钥匙串中删除证书,也没有别的办法了。其实,也不能光怪 xcode,而是跟”iphone 配置使用工具“也有一定的关系。
xcode中的这些“残留”证书不以常规的形式存在。如果你安装了“iphone 配置实用工具”,这些证书实际上存在于/users/km-cn/library/mobiledevice/applications/目录下的.app 文件中,这些.app 实际上是 “iphone配置实用工具”——“应用程序”中的所导入的 app。你可以用finder ——“显示包内容”来查看.app 。其中一个名叫“embedded.mobileprovision”的文件,就是“残留”的重复证书。你可以逐一删除这些 .app,也可以干脆把该目录下的所有.app 都删除(反正只要项目文件存在,你随时可以编译出这些 .app并导入到“iphone 配置实用工具”中)。最后,还要将 orgnizer 中的重复证书也删除,然后重启xcode。
65、application identifier 'com. ydtf.*' which doesn't match the current setting'com.ydtf.dlt'
如你所见,这两个application id 绝对是匹配的(*表示通配符)。但这个莫名的错误会导致你始终不能编译。这绝对是 xcode 的另一个 bug,先将 codesigning 修改为 don't code sign,build,然后再修改回正确的签名 build。
66、theidentity used to sign the executable is no longer valid.
由于前面的签名问题导致不能archive。解决方式见问题 65。
67、在ipad 中使用 presentmodalviewcontroller 设置弹出窗口的大小
testviewcontroller*testvc = [[testviewcontroller alloc] initwithnibname:@"testviewcontroller"bundle:nil]; testvc.modalpresentationstyle= uimodalpresentationformsheet; testvc.modaltransitionstyle= uimodaltransitionstylecrossdissolve; [selfpresentmodalviewcontroller:testvc animated:yes]; testvc.view.superview.frame= cgrectmake(0, 0, 649, 397);//it's important to do this afterpresentmodalviewcontroller testvc.view.superview.center = self.view.center;
注意://it'simportant to do this after presentmodalviewcontroller。即一定要在[selfpresentmodalviewcontroller:testvc animated:yes];之后设置frame的大小!
68、在ipad 中定制 actionsheet 的按钮和popover 箭头方向。
actionsheet在 ipad 上以popover的方式显示。默认不会显示cancelbutton(sdk用popover之外的区域代替cancelbutton,用户只要点击popover之外的区域就等同点击取消按钮)。如果你这样init一个actionsheet:
uiactionsheet* sheet=[[uiactionsheet alloc]initwithtitle:nil delegate:self cancelbuttontitle:@"cancel" destructivebuttontitle:@"ok" otherbuttontitles:nil];
则最终只有红色的destructivebutton 会显示。
如果你非要显示cancelbutton,则可以这样干:
uiactionsheet* sheet=[[uiactionsheet alloc]initwithtitle:nil delegate:self cancelbuttontitle:nil destructivebuttontitle:nil otherbuttontitles:@"cancel",@"ok",nil]; // sheet.cancelbuttonindex=0; sheet.destructivebuttonindex=1;
指定destructivebuttonindex之后,该按钮被显示为红色。
但千万不要指定cancelbuttonindex,因为在ipad上cancelbutton会被移除。
在ipad中,sdk没有提供可以修改 actionsheet 的箭头方向的api,系统自动判断箭头显示的方向。但我们可以利用showfromrect的第1个参数来改变箭头的方向:
cgrect r=sender.bounds; r.size.width=2*self.view.bounds.size.width; r.origin.x=-self.view.bounds.size.width+sender.bounds.size.width/2+sender.frame.origin.x; [sheet showfromrect:r inview:sender animated:yes];
这样就将原来的左箭头,换成了上箭头。
其实ios 在判断 actionsheet 弹出方向时的逻辑很简单,哪边有“足够”的空间,它就往哪边弹出。当我们利用showfromrect的第1个参数将3个方向都“堵死”后,它就只能老老实实地从我们想要的方向弹出了。
69、在 asinetworkqueue 中 setshowaccurateprogress=yes 不起作用
在 networkqueue.showaccurateprogress= yes之前加入 request.showaccurateprogress= yes ,否则showaccurateprogress 不会生效。示例代码:
equest.showaccurateprogress=yes; networkqueue.showaccurateprogress=yes; [networkqueue setsuspended:yes]; [networkqueue addoperation:request]; networkqueue.uploadprogressdelegate=self; [networkqueue go];
此外,由于 cfnework 中的一个 bug,对于小于128k的数据,无法跟踪其上传/下载的精确进度。
70、如何设置 uiwebview 背景色为透明?
在ib中设置 uiwebview 的 background 属性为 clear color 并不能使其背景透明。要达到这个目的,你需要使用以下两句:
[webview setbackgroundcolor:[uicolor clearcolor]];[webview setopaque:no];
以上就是ios 开发百问(6)的内容。
其它类似信息

推荐信息