判断当前设备:

Ipad:

#define isPad(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)

Mac&Iphone&Ipod:

#if TARGET_OS_IPHONE

NSLog(@"iphone");

#elif TARGET_OS_MAC

NSLog(@"mac");

#elif

NSLog(@"other");

#endif

更改cell选中的背景

UIView*myview=[[UIViewalloc]init];

myview.frame=CGRectMake(0,0,320,47);

myview.backgroundColor=[UIColorcolorWithPatternImage:[UIImageimageNamed:@"0006.png"]];

cell.selectedBackgroundView=myview;

在数字键盘上添加button:

//定义一个消息中心

[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];//addObserver:注册一个观察员name:消息名称

-(void)keyboardWillShow:(NSNotification*)note{

//createcustombutton

UIButton*doneButton=[UIButtonbuttonWithType:UIButtonTypeCustom];

doneButton.frame=CGRectMake(0,163,106,53);

[doneButtonsetImage:[UIImageimageNamed:@"5.png"]forState:UIControlStateNormal];

[doneButtonaddTarget:selfaction:@selector(addRadixPoint)forControlEvents:UIControlEventTouchUpInside];

//locatekeyboardview

UIWindow*tempWindow=[[[UIApplicationsharedApplication]windows]objectAtIndex:1];//返回应用程序window

UIView*keyboard;

for(inti=0;i<[tempWindow.subviewscount];i++)//遍历window上的所有subview

{

keyboard=[tempWindow.subviewsobjectAtIndex:i];

//keyboardviewfound;addthecustombuttontoit

if([[keyboarddescription]hasPrefix:@"<UIKeyboard"]==YES)

[keyboardaddSubview:doneButton];

}

}

正则表达式使用:

被用于正则表达式的字串必须是可变长的,不然会出问题

将一个空间放在视图之上

[scrollViewinsertSubview:searchButtonaboveSubview:scrollView];

从本地加载图片

NSString*boundle=[[NSBundlemainBundle]resourcePath];

[web1loadHTMLString:[NSStringstringWithFormat:@"<imgsrc='0001.png'/>"]baseURL:[NSURLfileURLWithPath:boundle]];

从网页加载图片并让图片在规定长宽中缩小

[cell.imgloadHTMLString:[NSStringstringWithFormat:@"<html><body><imgsrc='%@'height='90px'width='90px'></body></html>",goodsInfo.GoodsImg]baseURL:nil];

将网页加载到webview上通过javascript获取里面的数据,如果只是发送了一个连接请求获取到源码以后可以用正则表达式进行获取数据

NSString*javaScript1=@"document.getElementsByName('.u').item(0).value";

NSString*javaScript2=@"document.getElementsByName('.challenge').item(0).value";

NSString*strResult1=[NSStringstringWithString:[theWebViewstringByEvaluatingJavaScriptFromString:javaScript1]];

NSString*strResult2=[NSStringstringWithString:[theWebViewstringByEvaluatingJavaScriptFromString:javaScript2]];

用NSString怎么把UTF8转换成unicode

utf8Str//

NSString*unicodeStr=[NSStringstringWithCString:[utf8StrUTF8String]encoding:NSUnicodeStringEncoding];

View自己调用自己的方法:

[selfperformSelector:@selector(loginToNext)withObject:nilafterDelay:2];//黄色段为方法名,和延迟几秒执行.

显示图像:

CGRectmyImageRect=CGRectMake(0.0f,0.0f,320.0f,109.0f);

UIImageView*myImage=[[UIImageViewalloc]initWithFrame:myImageRect];

[myImagesetImage:[UIImageimageNamed:@"myImage.png"]];

myImage.opaque=YES;//opaque是否透明

[self.viewaddSubview:myImage];

[myImagerelease];

WebView:

CGRectwebFrame=CGRectMake(0.0,0.0,320.0,460.0);

UIWebView*webView=[[UIWebViewalloc]initWithFrame:webFrame];

[webViewsetBackgroundColor:[UIColorwhiteColor]];

NSString*urlAddress=@"http://www.google.com";

NSURL*url=[NSURLURLWithString:urlAddress];

NSURLRequest*requestObj=[NSURLRequestrequestWithURL:url];

[webViewloadRequest:requestObj];

[selfaddSubview:webView];

[webViewrelease];

显示网络活动状态指示符

这是在iPhone左上部的状态栏显示的转动的图标指示有背景发生网络的活动。

UIApplication*app=[UIApplicationsharedApplication];

app.networkActivityIndicatorVisible=YES;

动画:一个接一个地显示一系列的图象

NSArray*myImages=[NSArrayarrayWithObjects:[UIImageimageNamed:@"myImage1.png"],[UIImageimageNamed:@"myImage2.png"],[UIImageimageNamed:@"myImage3.png"],[UIImageimageNamed:@"myImage4.gif"],nil];

UIImageView*myAnimatedView=[UIImageViewalloc];

[myAnimatedViewinitWithFrame:[selfbounds]];

myAnimatedView.animationImages=myImages;//animationImages属性返回一个存放动画图片的数组

myAnimatedView.animationDuration=0.25;//浏览整个图片一次所用的时间

myAnimatedView.animationRepeatCount=0;//0=loopsforever动画重复次数

[myAnimatedViewstartAnimating];

[selfaddSubview:myAnimatedView];

[myAnimatedViewrelease];

动画:显示了something在屏幕上移动。注:这种类型的动画是“开始后不处理”-你不能获取任何有关物体在动画中的信息(如当前的位置)。如果您需要此信息,您会手动使用定时器去调整动画的X和Y坐标

这个需要导入QuartzCore.framework

CABasicAnimation*theAnimation;

theAnimation=[CABasicAnimationanimationWithKeyPath:@"transform.translation.x"];

//CreatesandreturnsanCAPropertyAnimationinstanceforthespecifiedkeypath.

//parameter:thekeypathofthepropertytobeanimated

theAnimation.duration=1;

theAnimation.repeatCount=2;

theAnimation.autoreverses=YES;

theAnimation.fromValue=[NSNumbernumberWithFloat:0];

theAnimation.toValue=[NSNumbernumberWithFloat:-60];

[view.layeraddAnimation:theAnimationforKey:@"animateLayer"];

Draggableitems//拖动项目

Here'showtocreateasimpledraggableimage.//这是如何生成一个简单的拖动图象

1.CreateanewclassthatinheritsfromUIImageView

@interfacemyDraggableImage:UIImageView{}

2.Intheimplementationforthisnewclass,addthe2methods:

-(void)touchesBegan:(NSSet*)toucheswithEvent:(UIEvent*)event

{

//Retrievethetouchpoint检索接触点

CGPointpt=[[touchesanyObject]locationInView:self];

startLocation=pt;

[[selfsuperview]bringSubviewToFront:self];

}

-(void)touchesMoved:(NSSet*)toucheswithEvent:(UIEvent*)event

{

//Moverelativetotheoriginaltouchpoint相对以前的触摸点进行移动

CGPointpt=[[touchesanyObject]locationInView:self];

CGRectframe=[selfframe];

frame.origin.x+=pt.x-startLocation.x;

frame.origin.y+=pt.y-startLocation.y;

[selfsetFrame:frame];

}

3.Nowinstantiatethenewclassasyouwouldanyothernewimageandaddittoyourview

//实例这个新的类,放到你需要新的图片放到你的视图上

dragger=[[myDraggableImagealloc]initWithFrame:myDragRect];

[draggersetImage:[UIImageimageNamed:@"myImage.png"]];

[draggersetUserInteractionEnabled:YES];

线程:

1.Createthenewthread:

[NSThreaddetachNewThreadSelector:@selector(myMethod)toTarget:selfwithObject:nil];

2.Createthemethodthatiscalledbythenewthread:

-(void)myMethod

{

NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];

***codethatshouldberuninthenewthreadgoeshere***

[poolrelease];

}

//Whatifyouneedtodosomethingtothemainthreadfrominsideyournewthread(forexample,showaloading//symbol)?UseperformSelectorOnMainThread.

[selfperformSelectorOnMainThread:@selector(myMethod)withObject:nilwaitUntilDone:false];

Plistfiles

Application-specificplistfilescanbestoredintheResourcesfolderoftheappbundle.Whentheappfirstlaunches,itshouldcheckifthereisanexistingplistintheuser'sDocumentsfolder,andifnotitshouldcopytheplistfromtheappbundle.

//LookinDocumentsforanexistingplistfile

NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString*documentsDirectory=[pathsobjectAtIndex:0];

myPlistPath=[documentsDirectorystringByAppendingPathComponent:

[NSStringstringWithFormat:@"%@.plist",plistName]];

[myPlistPathretain];

//Ifit'snotthere,copyitfromthebundle

NSFileManager*fileManger=[NSFileManagerdefaultManager];

if(![fileMangerfileExistsAtPath:myPlistPath])

{

NSString*pathToSettingsInBundle=[[NSBundlemainBundle]pathForResource:plistNameofType:@"plist"];

}

//NowreadtheplistfilefromDocuments

NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString*documentsDirectoryPath=[pathsobjectAtIndex:0];

NSString*path=[documentsDirectoryPathstringByAppendingPathComponent:@"myApp.plist"];

NSMutableDictionary*plist=[NSDictionarydictionaryWithContentsOfFile:path];

//Nowreadandsetkey/values

myKey=(int)[[plistvalueForKey:@"myKey"]intValue];

myKey2=(bool)[[plistvalueForKey:@"myKey2"]boolValue];

[plistsetValue:myKeyforKey:@"myKey"];

[plistwriteToFile:pathatomically:YES];

Alerts

ShowasimplealertwithOKbutton.

UIAlertView*alert=[[UIAlertViewalloc]initWithTitle:nilmessage:

@"AnAlert!"delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil];

[alertshow];

[alertrelease];

Infobutton

IncreasethetouchableareaontheInfobutton,soit'seasiertopress.

CGRectnewInfoButtonRect=CGRectMake(infoButton.frame.origin.x-25,infoButton.frame.origin.y-25,infoButton.frame.size.width+50,infoButton.frame.size.height+50);

[infoButtonsetFrame:newInfoButtonRect];

DetectingSubviews

Youcanloopthroughsubviewsofanexistingview.Thisworksespeciallywellifyouusethe"tag"propertyonyourviews.

for(UIImageView*anImagein[self.viewsubviews])

{

if(anImage.tag==1)

{//dosomething}

}