1. 随机数:
id d = random(); // 随机数
2. 视频播放:
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: @" Movie " ofType: @" m4v " ]]];
// 初始化视频播放器对象,并传入被播放文件的地址
moviePlayer.movieControlMode = MPMovieControlModeDefault;
[moviePlayer play];
// 此处有内存溢出
3. 启动界面显示:
iPhone软件启动后的第一屏图片是非常重要的往往就是loading载入中的意思。设置它说来也简单,但是却无比重要
只需要在resource里面将你希望设置的图片更名为Default.png,这个图片就可以成为iPhone载入的缺省图片
4. iPhone的系统目录:
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex: 0 ];
// 得到temp临时目录:
NSString * tempPath = NSTemporaryDirectory();
// 得到目录上的文件地址:
NSString * 文件地址 = [目录地址 stringByAppendingPathComponent: @" 文件名.扩展名 " ];
5. 状态栏显示Indicator:
6.app Icon显示数字:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 5 ];
}
7.sqlite保存地址:
NSString * thePath = [paths objectAtIndex: 0 ];
NSString * filePath = [thePath stringByAppendingPathComponent: @" kilonet1.sqlite " ];
NSString * dbPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent: @" kilonet2.sqlite " ];
8.Application退出:exit(0);
9. AlertView,ActionSheet的cancelButton点击事件:
NSLog( @" cancel actionSheet........ " );
// 当用户按下cancel按钮
if ( buttonIndex == [actionSheet cancelButtonIndex]) {
exit( 0 );
}
// // 当用户按下destructive按钮
// if( buttonIndex == [actionSheet destructiveButtonIndex]) {
// // DoSomething here.
// }
}
- ( void )alertView:(UIAlertView * )alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog( @" cancel alertView........ " );
if (buttonIndex == [alertView cancelButtonIndex]) {
exit( 0 );
}
}
10.给Window设置全局的背景图片:
11. UITextField文本框显示及对键盘的控制:
#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文本框前面空白宽度设置以及后面组合按钮设置:
_txtAccount.rightView = _btnDropDown;
_txtAccount.rightViewMode = UITextFieldViewModeAlways;
// 给文本输入框前面加入空白
CGRect frame = [_txtAccount frame];
frame.size.width = 5 ;
UIView * leftview = [[UIView alloc] initWithFrame:frame];
_txtAccount.leftViewMode = UITextFieldViewModeAlways;
_txtAccount.leftView = leftview;
13. UIScrollView 设置滑动不超出本身范围:
[ fcScrollView setBounces : NO ];
14. 遍历View里面所有的Subview:
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之间相互转换:
string = [[array valueForKey: @" description " ] componentsJoinedByString: @" , " ];
17. TabController随意切换tab bar:
[self.tabBarController setSelectedIndex:tabIndex];
或者 self.tabBarController.selectedIndex = tabIndex;
或者实现下面的delegate来扑捉tab bar的事件:
代码
-
(BOOL) tabBarController:(UITabBarController
*
)tabBarController shouldSelectViewController:(UIViewController
*
)viewController {
if
([viewController.tabBarItem.title isEqualToString: NSLocalizedString(
@"
Logout
"
,nil)]) {
[self showLogout];
return
NO;
}
return
YES;
}
18. 自定义View之间切换动画:
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];
}
或者:
transition.duration = kAnimationDuration;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromTop;
transitioning = YES;
transition. delegate = self;
[self.navigationController.view.layer addAnimation:transition forKey:nil];
self.navigationController.navigationBarHidden = NO;
[self.navigationController pushViewController:tableViewController animated:YES];
19. UIWebView加载时白色显示问题解决以及字体统一设置:
uiWebView.opaque = NO ;
代码
20.计算字符串长度:
CGFloat w = [title sizeWithFont:[ UIFont fontWithName: @"Arial" size: 18 ]]. width ;
21. iTunesLink should be your applications link
NSString *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;
}
NSString * year = [ myDate descriptionWithCalendarFormat :@ "%Y" timeZone : nil locale : nil ];
or
NSDateFormatter
*
formatter
=
[[
NSDateFormatter
alloc
]
init
];
[
formatter setDateFormat
:@
"yyyy"
];
//Optionally for time zone converstions
[
formatter setTimeZone
:[
NSTimeZone
timeZoneWithName
:@
"..."
]];
NSString
*
stringFromDate
=
[
formatter stringFromDate
:
myNSDateInstance
];
22。 模拟器的文件位置
其中#username#表示当前用户名: /Users/#username#/Library/Application Support/iPhone Simulator/User/Applications/ |
23.在使用UISearchBar时,将背景色设定为clearColor,或者将translucent设为YES,都不能使背景透明,经过一番研究,发现了一种超级简单和实用的方法:
1
|
[
[ searchbar.subviews objectAtIndex
: 0
] removeFromSuperview
] ;
|
背景完全消除了,只剩下搜索框本身了。
24. 图像与缓存 :
UIImageView * wallpaper = [ [ UIImageView alloc] initWithImage:
[ UIImage imageNamed: @ "icon.png" ] ] ; // 会缓存图片
UIImageView * wallpaper = [ [ UIImageView alloc] initWithImage:
[ UIImage imageWithContentsOfFile: @ "icon.png" ] ] ; // 不会缓存图片
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中添加左箭头返回按钮
在iPhone 里面最讨厌的控件之一就是 UINavigationBar 了。这个控件样式修改不方便,连添加按钮也特别麻烦。下面的例子是如何手动添加带箭头的按钮:
UINavigationItem *item = [navBar.items objectAtIndex:0];
UINavigationItem *back = [[UINavigationItem alloc] initWithTitle:@"Back"];
NSArray *items = [[NSArray alloc] initWithObjects:back,item,nil];
[navBar setItems:items];
- (BOOL)navigationBar:(UINavigationBar *)navigationBar
shouldPopItem:(UINavigationItem *)item{
// 在此处添加点击back 按钮之后的操作代码
return NO;
}