您的位置:首页 > 编程语言

iPhone代码片段收集(2)

2011-04-26 17:39 405 查看
1. 随机数:srandom(time(NULL)); //随机数种子

id d = random(); // 随机数 2. 视频播放: MPMoviePlayerController *moviePlayer;
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"]]];
//初始化视频播放器对象,并传入被播放文件的地址
moviePlayer.movieControlMode = MPMovieControlModeDefault;
[moviePlayer play];
//此处有内存溢出 3. 只需要在resource里面将你希望设置的图片更名为Default.png,这个图片就可以成为iPhone载入的缺省图片 4. iPhone的系统目录://得到Document目录:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

//得到temp临时目录:
NSString *tempPath = NSTemporaryDirectory();

//得到目录上的文件地址:
NSString *文件地址 = [目录地址 stringByAppendingPathComponent:@"文件名.扩展名"]; 5. 状态栏显示Indicator:[UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 6.app Icon显示数字: - (void)applicationDidEnterBackground:(UIApplication *)application{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:5];
} 7.sqlite保存地址:

代码 8.Application退出:exit(0);

9. AlertView,ActionSheet的cancelButton点击事件:

代码#pragma mark -
#pragma mark UITextFieldDelegate
//控制键盘跳转
- (BOOL)textFieldShouldReturn:(UITextField *)textField {

if (textField == _txtAccount) {
if ([_txtAccount.text length]==0) {
return NO;
}
[_txtPassword becomeFirstResponder];
} else if (textField == _txtPassword) {
[_txtPassword resignFirstResponder];
}

return YES;
}

//输入框背景更换
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField{

[textField setBackground:[UIImage imageNamed:@"ctext_field_02.png"]];

return YES;
}

-(void) textFieldDidEndEditing:(UITextField *)textField{
[textField setBackground:[UIImage imageNamed:@"ctext_field_01.png"]];
} 12.UITextField文本框前面空白宽度设置以及后面组合按钮设置:

代码 NSLog(@"subviews count=%d",[self.view.subviews count]);
if ([self.view.subviews count] > 0) {
for (UIView *curView in self.view.subviews) {
NSLog(@"view.subviews=%@", [NSString stringWithUTF8String:object_getClassName(curView)]);
}
} 14. 在drawRect里画文字: UIFont * f = [UIFont systemFontOfSize:20];
[[UIColor darkGrayColor] set];
NSString * text = @"hi \nKiloNet";
[text drawAtPoint:CGPointMake(center.x,center.y) withFont:f]; 15. NSArray查找是否存在对象时用indexOfObject,如果不存在则返回为NSNotFound. 16. NString与NSArray之间相互转换:array = [string componentsSeparatedByString:@","];
string = [[array valueForKey:@"description"] componentsJoinedByString:@","]; 17. TabController随意切换tab bar: [self.tabBarController setSelectedIndex:tabIndex]; 或者 self.tabBarController.selectedIndex = tabIndex; 或者实现下面的delegate来扑捉tab bar的事件:
- (void) pushController: (UIViewController*) controller
withTransition: (UIViewAnimationTransition) transition
{
[UIView beginAnimations:nil context:NULL];
[self pushViewController:controller animated:NO];
[UIView setAnimationDuration:.5];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationTransition:transition forView:self.view cache:YES];
[UIView commitAnimations];
}
或者:


代码

代码      20.计算字符串长度:CGFloat w = [title sizeWithFont:[UIFont fontWithName:@"Arial" size:18]].width;      21. iTunesLink should be your applications linkNSString *iTunesLink = @"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=xxxxxx&mt=8";[[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]];
22.时间转换NSString & NSDate: -(NSDate *)NSStringDateToNSDate:(NSString *)string {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [formatter dateFromString:string];
[formatter release];
return date;
} 
/span/span/code/pre/spandiv 22。 模拟器的文件位置/divspan style="font-family: Verdana,Helvetica,Arial,sans-serif; line-height: normal; font-size: 12px; border-collapse: collapse; color: #444444;"tbody style="word-wrap: break-word; line-height: normal;"/tbody
其中#username#表示当前用户名:
/Users/#username#/Library/Application Support/iPhone Simulator/User/Applications/
/spandiv /divdiv 23.span style="font-family: "Microsoft YaHei',Helvetica,Arial; line-height: 28px; color: #242020;">在使用UISearchBar时,将背景色设定为clearColor,或者将translucent设为YES,都不能使背景透明,经过一番研究,发现了一种超级简单和实用的方法:[]removeFromSuperviewUIImageView [:
:]*wallpaper [UIImageView alloc @]; // 不会缓存图片 25. iphone-常用的对视图图层(layer)的操作

对图层的操作:

(1.给图层添加背景图片:
myView.layer.contents = (id)[UIImage imageNamed:@"view_BG.png"].CGImage;

(2.将图层的边框设置为圆脚
myWebView.layer.cornerRadius = 8;
myWebView.layer.masksToBounds = YES;

(3.给图层添加一个有色边框
myWebView.layer.borderWidth = 5;
myWebView.layer.borderColor = [[UIColor colorWithRed:0.52 green:0.09 blue:0.07 alpha:1] CGColor];
26. UIPopoverController 使用 -(void) onSetting:(id) sender {

SplitBaseController *detail = [[SettingServerController alloc] init];

CGRect frame = [(UIView *)sender frame];
frame.origin.y = 0;

UIPopoverController *popwin = [[UIPopoverController alloc] initWithContentViewController:detail];
[popwin setPopoverContentSize:CGSizeMake(400, 300) animated:YES];
popwin.delegate = self;
[popwin presentPopoverFromRect: frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

[detail release];
} 27.在UINavigationBar中添加左箭头返回按钮
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  职场 移动开发 休闲