`
v5qqcom
  • 浏览: 1289945 次
文章分类
社区版块
存档分类
最新评论

Iphone代码片段

 
阅读更多

Iphone代码片段导航

Iphone开发代码片段1

Iphone开发代码片段2

Iphone开发代码片段3

1.给UITableViewController添加ToolBar。

self.navigationController.toolbarHidden = NO; //默认是隐藏的。

//添加MessageToolBar ,messageToolBar是IBOutlet的一个ToolBar。

self.toolbarItems = [[[NSMutableArray alloc] initWithArray:self.messageToolBar.items] autorelease];

self.navigationController.toolbar.barStyle = self.messageToolBar.barStyle;

2.后台运行一个方法,如果该方法需要修改UI,为了防止出错,应在主线程里修改UI。

[self performSelectorInBackground:@selector(updateInfo)];

在UpdateInfo里如果要修改UI ,

[self performSelectorOnMainThread:@selector(updateUIMethod) withObject:nil waitUntilDone:NO];

同时注意,后台程序的方法应该放在NSAutoRelease pool里的,如下所示:

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
xxxx
[pool release];

3.在A类里动态的设定B类或者C类的方法。

[self.actionTarget performSelector:self.actionMethod withObject:parameter];

actionTarget -> id类型的属性。设置B 类或者C类。

actionMethod -> Sel类型的属性。设置具体的方法名

parameter -> 参数

4.设置Navigation的提示信息和进度条设置

self.navigationItem.prompt : 提示信息
self.navigationItem.titleView :存放ProgressBar等其它提示信息的View

在进度条显示完了后,需要清空显示进度信息:

self.navigationItem.prompt = nil;
self.navigationItem.titleView = nil;

5.从资源文件xib里加载View的方法

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyView"
owner:self
options:nil];
MyView *view = [nib objectAtIndex:0];

6. UIAlterView 修改默认的Frame高度

在其委托里实现这个方法

- (void)willPresentAlertView:(UIAlertView *)alertView 

{

  alertView.frame = CGRectMake(5.f, 1.f, 100.f, 200.f);

}

参考:http://stackoverflow.com/questions/2763713/change-width-of-uialertview-in-ipad

7.获取iphone屏幕大小

CGRect screenBounds = [ [ UIScreen mainScreen ] bounds ];
CGRect screenRect= [ [ UIScreen mainScreen ] applicationframe ];

8. 修改TableView的样式,让UITableView显示Windows的背景图片。

self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
self.tableView.backgroundView = nil;

如果要修改UITableCell的事情backgroundColor需要再tableView:willDisplayCell:forRowAtIndexPath:里修改。

9.通过图片获取颜色。

[UIColor colorWithPatternImage:[UIImage imageNamed:@"imageName"]];

修改分割线颜色

self.tableView.separatorColor = [UIColor blackColor];

显示文本的地方设置透明色

cell.textLabel.opaque = NO;

这样整个cell就有立体感。

10.设置UITableView 的checkmark显示样式

修改cell的 accessoryView

cell.accessoryView = UIImageView

11. 修改TableView距离导航缆的高度。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{
return 10.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];;
}

12. 自定义TableViewCell的背景颜色和选择后的颜色。

方法一:将TableViewCell的backgroundView和SelectBackGroundView修改成指定的View就可以了。

方法二: 在Interface Builder里设置cell的image和SelectImage属性,但是要记得UItableView修改seperator的属性为None

13 颜色定义。

美工一般定义好颜色,然后让程序员去填充颜色,美工一般给的是RGB颜色,那么RGB颜色如果换成UIColor

[UIColor colorWithRed:31.0/255 green:204.0/255 blue:39.0/255 alpha:1.0];

Red,Green,Blue只接受0-1的参数,换算方法是除以255。

14. Xcode 4设置NSZombieEnabled

if you click on the scheme drop down bar -> edit scheme -> arguments tab and then add NSZombieEnabled in the Environment Variables column and YES in the value column

15.自动生成多语言化的StringTable

如果在代码里全部是通过 NSLocalizedString(@"中文", nil)来对应多语言,最后要整理一个list,手动一个一个粘贴太麻烦。

自动化生成方法:在命令行目录下进入项目根目录:执行genstrings -a $(find . -name "*.m"),就会自动生成一个文件对应。

参考网址 http://steelwheels.sourceforge.jp/Documents/genstring.html

http://iphone.longearth.net/2009/05/25/%E3%80%90iphone%E3%80%91localizablestrings%E3%82%92%E8%87%AA%E5%8B%95%E3%81%A7%E4%BD%9C%E3%82%8B-genstrings/

16.自定义bond字体

[UIFont fontWithName:@"Helvetica-Bold" size:16.0]

17无边框透明UITableViewCell

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

self.tableView.separatorColor = [UIColor clearColor];

self.tableView.backgroundColor = [UIColor clearColor];

self.tableView.opaque = NO;

self.tableView.backgroundView = nil;

--Cell修改--

self.backgroundView = [[[UIView alloc] init] autorelease];

self.backgroundView.backgroundColor = [UIColor clearColor];

self.selectedBackgroundView = [[[UIView alloc] init] autorelease];

self.selectedBackgroundView.backgroundColor = [UIColor clearColor];

18. 隐藏Tabbar

SampleViewController *obj = [[SampleViewController alloc] init];

[obj setHidesBottomBarWhenPushed:YES];

[self.navigationController pushViewController:obj animated:YES];

[obj release];
19.从UIView获取UImage

#import QuartzCore/QuartzCore.h

- (UIImage *)getImageFromView:(UIView *)orgView

{ UIGraphicsBeginImageContext(orgView.bounds.size);

[orgView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;

}

20. 添加手式识别后,会屏蔽掉touchend方法

21.获取手机号码,和IMEI

获取本地iphone手机号码

[[NSUserDefaults standardUserDefaults] valueForKey:@"SBFormattedPhoneNumber"];

获取手机的imei

#import "Message/NetworkController.h"

NetworkController *ntc=[[NetworkController sharedInstance] autorelease];

NSString *imeistring = [ntc IMEI];

imeistring就是获取的imei。 IMEI(International Mobile Equipment Identity)是国际移动设备身份码的缩写,国际移动装备辨识码,是由15位数字组成的"电子串号",它与每台手机一一对应,而且该码是全世界唯一的。

22 NLog的格式,经常忘记,做个笔记

%@ 对象
%d, %i 整数
%u 无符整形
%f 浮点/双字
%x, %X 二进制整数
%o 八进制整数
%zu size_t
%p 指针
%e 浮点/双字 (科学计算)
%g 浮点/双字
%s C 字符串
%.*s Pascal字符串
%c 字符
%C unichar
%lld 64位长整数(long long)
%llu 无符64位长整数
%Lf 64位双字

23.更改UISearchBar最下面黑色的边框

#define SEARCHBAR_BORDER_TAG 1337

- (void) viewDidLoad{
// Set a custom border on the bottom of the search bar, so it's not so harsh
UISearchBar *searchBar = self.searchDisplayController.searchBar;
UIView *bottomBorder = [[UIView alloc] initWithFrame:CGRectMake(0,searchBar.frame.size.height-1,searchBar.frame.size.width, 1)];
[bottomBorder setBackgroundColor:[UIColor colorWithWhite:200.0f/255.f alpha:1.0f]];
[bottomBorder setOpaque:YES];
[bottomBorder setTag:SEARCHBAR_BORDER_TAG];
[searchBar addSubview:bottomBorder];
[bottomBorder release];
}

24.设置键盘的默认形式。

比如UITextField 设置为默认数字,和只允许数组数字

//默认数字

textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation
//只允许输入数字

textField.keyboardType = UIKeyboardTypeNumberPad

25.UIButton设置文字左对齐

- emailBtn.contentHorizontalAlignment = UIontrolContentHorizontalAlignmentLeft;
- emailBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
- CGRect buttonRect = emailBtn.bounds;
UILabel *myLabel = [[UILabel alloc] initWithFrame: buttonRect];
myLabel = UITextAlignmentLeft;
[emailBtn addSubview:myLabel];

[myLabel release];

26. retain异常的时候重载这个方法设置断点查看和分析

- (id) retain

{
// Break here to see who is retaining me.
return [super retain];
}

27.去掉白色半圆

Plist添加

Icon already includes gloss effects 为YES

UIPrerenderedIcon 设置不起作用(Xcode4 .0.2)

28.tableView reloadRowsAtIndexPaths 如果不在可见区域,将不会重新加载。

29. 设置应用程序的statusbaryanse

再plist里设置Status bar styleOpaque black style

30. 设置控件的copy paste的本地化

- 设置Localization native development region =》 china

- 将项目的en.lproj 改成zh_CN.lproj

31. 允许应用程序通过itunes上传文件(ios3.2以上)

在info.plist里设置UIFileSharingEnabled => YES

32. 获取UICOLOR的rgb值

const CGFloat* components = CGColorGetComponents(SelectedColor.CGColor);
NSLog(@"Red: %f", components[0]);
NSLog(@"Green: %f", components[1]);
NSLog(@"Blue: %f", components[2]);

NSLog(@"Alpha: %f", CGColorGetAlpha(SelectedColor.CGColor));

33.获取2个时间之间的天,小时,分钟

+(NSString *)TimeRemainingUntilDate:(NSDate *)date {

NSTimeInterval interval = [date timeIntervalSinceNow];
NSString * timeRemaining = nil;
if (interval > 0) {
div_t d = div(interval, 86400);
int day = d.quot;
div_t h = div(d.rem, 3600);
int hour = h.quot;
div_t m = div(h.rem, 60);
int min = m.quot;
NSString * nbday = nil;
if(day > 1)
nbday = @"days";
else if(day == 1)
nbday = @"day";
else
nbday = @"";
NSString * nbhour = nil;
if(hour > 1)
nbhour = @"hours";
else if (hour == 1)
nbhour = @"hour";
else
nbhour = @"";
NSString * nbmin = nil;
if(min > 1)
nbmin = @"mins";
else
nbmin = @"min";
timeRemaining = [NSString stringWithFormat:@"%@%@ %@%@ %@%@",day ? [NSNumber numberWithInt:day] : @"",nbday,hour ? [NSNumber numberWithInt:hour] : @"",nbhour,min ? [NSNumber numberWithInt:min] : @"00",nbmin];
}
else
timeRemaining = @"Over";
return timeRemaining;
}

34.Icon specified in the Info.plist not found under the top level app wrapper

记住Icon 首字母是大写的,不是icon.png, 是Icon.png

35.

[iphone]Code Sign error: Provisioning profile XXXX can't be found

http://www.cnblogs.com/baryon/archive/2010/05/06/1728968.html

http://www.douban.com/note/131009422/

1.关闭你的项目,找到项目文件XXXX.xcodeproj,在文件上点击右键,选择“显示包内容”(Show Package Contents)。会新打开一个Finder。注:其实XXXX.xcodeproj就是一个文件夹,这里新打开的一个Finder里面的三个文件就是该XXXX.xcodeproj文件夹里面的文件。
2.在新打开的Finder中找到project.pbxproj,并且打开。在这之中找到你之前的证书的编码信息。我之前报的错误信息是
Code Sign error: Provisioning profile '37D44E7F-0339-4277-9A82-C146A944CD46',所以我用查找的方式找到了所有包括37D44E7F-0339-4277-9A82-C146A944CD46的行,并且删除。
3.保存,重新启动你的项目,再编译。就OK了。

36.获取手机唯一ID

UIDevice *device = [UIDevice currentDevice];//创建设备对象
NSString *deviceUID = [[NSString alloc] initWithString:[device uniqueIdentifier]];

NSLog(@"%@",deviceUID); // 输出设备id

分享到:
评论

相关推荐

    ios-watchkit-snippets:使用 WatchKit 制作 Apple Watch 应用程序的代码片段

    ios-watchkit-snippets ##Description 使用 WatchKit 制作 Apple Watch 应用程序的代码片段。 此 repo 中的每个文件夹代表一个不同的项目,如下所述。 ##语言迅捷###项目:HelloWatch 一个简单的“Hello World”...

    JDLIB:开源构建脚本和重要的代码片段

    开源构建脚本和重要的代码片段 一些重要的构建脚本,用于在具有 arm64 支持的 iOS 平台中构建开源 C 库。 LIBIDN 构建脚本 原始构建脚本是在 xmppframework 项目中找到的,后来我修改了脚本以使用 iOS7 SDK 和 arm...

    Alfred_4.0.9_1144.dmg 通过热键,关键字,文本扩展等功能提高您的效率

    创建您自己的代码片段,然后键入一个简短的缩写,以将其自动扩展为全文代码片段,从长远来看,您可以节省数小时的输入时间! 借助Alfred的Powerpack,可使用功能强大的工作流程来更高效地执行任务,并减少重复的...

    mailbrush:Node.js 库,用于生成带有语法突出显示的电子邮件友好片段

    MailBrush 允许您向代码片段添加语法突出显示,以便它们可以在您的电子邮件模板中使用。 在您的电子邮件模板中,不要像下面这样简单: { "key": "value", "key2": "value 2" } 您的代码段现在将如下所示: ...

    awesome-snippets-js:javascript的代码段

    真棒代码片段本页涵盖了javascript的高级代码段技术。 描述可以使用的代码段。 您可以随时使用“拉取请求”进行贡献。验证检查iOS function checkiOS ( ) { return / iPad | iPhone | iPod / . test ( navigator . ...

    JQUERY-跟CSS3卡通动态背景特效.zip

    代码片段:  wow bounceInDown" data-wow-delay=".8s"><i class="icon iphone"></i> App Store  wow bounceInDown" data-wow-delay="1s"><i class="icon an"></i> Android下载 ...

    SideNotes-1.1.1.zip 显示器侧面的笔记

    使用带有等宽字体的“代码”模式保存代码片段。创建快速的待办事项列表并将项目标记为已完成。任何你想要的。 入门 入门 我们已经准备了一个视频指南,其中涵盖了SideNotes的大多数功能。它面向刚开始使用该...

    TextExpander for Chrome-crx插件

    扩展所有应用程序中的代码片段,使您摆脱系统特定的模板和应用程序特定的自动更正。始终在所有设备上保持最新状态 *与您的团队分享力量与您的团队分享片段组。每个人都可以从团队的集体知识中受益。另外,更正过时的...

    css3商品购物清单.zip

    代码片段: 感谢您的惠顾!  以下是您的购物清单,请保存好此票据。      <span class="receipt__line__item">iPad mini 4(数量1)  ¥ 1699</span>      ...

    cortex-mobile-demo:Cortex的Android和iPhone演示应用程序

    目标是提供有用的代码片段,您可以在自己的应用程序中使用它们。 皮质 cortex 演示只能与或存储库结合使用。 它还需要额外的本机库,您需要将这些库放在 Android 的 /libs 文件夹中。 请联系 pim@ 或 ted@ sense-os...

    微信小程序实现树莓派(raspberry pi)小车控制

    这里简单记录一下主要代码片段。也是趟过了许多的坑,例如:微信小程序不支持完全全屏,微信小程序不能横屏展示。所以开发过程中也用了一些非常手段。可以说这只是一个很基本的demo,所以里面很多东西,比如摄像头...

    responsiviewer:在多屏幕查看器中打开浏览器屏幕。 以多种尺寸加载一页,以一种尺寸加载许多页面

    gitFlow 完成自述文件添加代码片段版权和许可信息作者名单提交错误、功能请求、提交补丁、加入邮件列表、获取公告或以其他形式加入用户或开发社区的说明“关闭所有屏幕”按钮切换菜单带到前面和发送到后面选项检查 $...

    NEJUG201501-MaterialDesign:我在 NEJUG 上的 Lightning Talk on Android Lollipop Material Design 笔记

    Android 棒棒糖材料设计 新英格兰 Java 用户组Lightning Talk 的注意事项 ... 将做一个过渡动画的快速演示,然后查看小代码片段。 API 感觉有点“粗糙”,文档不是很好,而且我遇到了一些似乎误导我的

    Freemake Video Converter影音转换器 v4.1.9.rar

    而且内建了dvd烧录功能及youtube上传功能,还可做简单的影片剪辑,相当方便而且实用,连初学者都可轻易上手,新增对 HTML5 的支持,包含 Ogg, Theora, WebM, H.264 四种格式,自动生成网页代码,方便发布 ...

    the-wall:Mootools 的 Javascript 插件

    The Wall - Mootools 的 ...片段代码 Javascript: #JS // Define The Wall var maxLength = 100; // Max Number images or array length var counter = 1; var wall = new Wall("wall", { "width":150, "height

    程序员专用刷题-fdac19.github.io:网页

    程序员专用刷题班级 通过 Zoom 远程参与的信息 从 PC、Mac、iPad、iPhone 或 Android 设备加入:请单击此 URL 开始或加入。...或者,转到并输入课程会话/会议 ...代码片段:在 GH 上,或者,如果有人贡

    工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究

    Android 一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导开发 IOS 由苹果公司开发的移动操作系统 Webkit 一个开源的浏览器引擎,在手机上的...

Global site tag (gtag.js) - Google Analytics