一些微小但是又不得不知道的问题(持续更新)
2013-02-21 22:23
288 查看
1.隐藏UIWindow显示电池电量的状态栏
[[UIApplication sharedApplication] setStatusBarHidden:TRUE];
改变navbar的颜色
self.navigationController.navigationBar.tintColor
= [UIColor darkGrayColor];
2.委托里 @required的必须实现
3.textField.contentVerticalAlignment
= UIControlContentVerticalAlignmentCenter;//文字内容垂直居中,此为UIControl的属性
4.textField.placeholder
= @"请输入文字";
//设置默认提示文字
5.全局变量设置 在APPDelegate.h ,AppDelegate.m 中定义你的全局变量名:例如(FMDatabase *db;)
调用的时候 (1)extern FMDatabase *db; (2) AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate]; appDelegate.你的变量
6.xcode 中为单独文件设置不支持ARC的方法 (项目本身启用了ARC,单需要用到FMDatabase,而FMDatabase不支持ARC)
修改方法: 选择项目---》Build Phases ----》Compile Sources --->选择需要的文件---》在右边Compile Flags 输入 -fno-objc-arc 即可。
7.IOS获取系统时间:
NSString *date;
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"YYYY-mm-dd hh:mm:ss"];
date = [formatter stringFromDate:[NSDate date]];
//date 显示为: 2011-12-02 12:12:12
想查找几天前的时间可以用这个方法:NSDate *date = [[NSDate alloc]init]; date = [date dateByAddingTimeInterval:-5*3600*24];
8.IOS 禁用多按钮同时点下的效果 [view setExclusiveTouch:YES];
9.IOS button上的title默认是英文的,要修改成中文的 在.plist文件中如下设置:
Localization native development region -China
Localized resources can be mixed -YES
10.在AppDelegate里面引用了UINavigationController,可以设置ToolBar显示。若想隐藏ToolBar,则要在Appelegate里面设置 [self.navigationController setToolBarHidden:YES animated:YES];
11.#import 和@class的区别 :import会包含这个类的所有信息,@class只是包含这个类的名字,至于如何定义的咱不用考虑。另外如果有循环依赖关系,如:A–>B, B–>A这样的相互依赖关系,如果使用#import来相互包含,那么就会出现编译错误,如果使用@class在两个类的头文件中相互声明,则不会有编译错误出现。
12.NSString和NSURL的相互转化:
NSString *str = @"www.baidu.com";
NSURL *url = [NSURL URLWithString:str]; //string--->url
NSString *str1 = [url absoluteString]; //url--->string
13.获取时间间隔:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-mm-dd HH:mm"];
NSDate *date1 = [dateFormatter dateFormString:@"2011-2-5 1:00"];
NSDate *date2 = [dateFormatter dateFormString:@"2011-2-6 2:00"];
NSTimeInterval time = [date2 timeIntervalSinceDate:date1];
int days = ((int)time)/(3600*24);
int hours = ((int)time)%(3600*24)/3600;
NSString *dateCountent = [[NSString alloc]initWithFormat:@"%i天,%i小时",days,hours];
14.退出程序 exit();
15.创建UIButton
(1). UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake (x, y, Width,
Height)];
(2). UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
设置UIButton标题 [button setTitle:title forState:UIControlStateNormal];
设置UIButton标题颜色 [button setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];
设置UIButton背景图片 [button setBackgroundImage:newImage
forState:UIControlStateNormal];
设置UIButton背景颜色 button.backgroundColor = [UIColor
clearColor];
16.UIButton状态:
UIControlStateNormal // 正常状态
UIControlStateHighlighted // 高亮状态
UIControlStateDisabled // 禁用状态
UIControlStateSelected // 选中状态
UIControlStateApplication //
UIControlStateReserved // 保留状态
UIButton类型:
UIButtonTypeCustom //自定义类型
UIButtonTypeRoundedRect
//圆角类型
UIButtonTypeDetailDisclosure
//细节展示按钮
UIButtonTypeInfoLight
//浅色背景的信息按钮
UIButtonTypeInfoDark
//暗色背景的信息按钮
UIButtonTypeContactAdd
// 添加按钮
17.退回输入键盘:
- (BOOL) textFieldShouldReturn:(id)textField{
[textField resignFirstResponder];
}
textField 为空时让按钮不可用,既防止误操作又显得应用很注重细节。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
NSMutableString *newValue = [[self.TextField.text mutableCopy] autorelease];
[newValue replaceCharactersInRange:range withString:string];
if ([newValue length]== 0) {
self.navigationItem.rightBarButtonItem.enabled = NO;
}
else {
self.navigationItem.rightBarButtonItem.enabled = YES;
}
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
self.navigationItem.rightBarButtonItem.enabled = NO;
return YES;
}
18.CGRect,CGPoint & CGSize
CGRect frame = CGRectMake (origin.x, origin.y, size.width, size.height);矩形
NSStringFromCGRect(someCG) 把CGRect结构转变为格式化字符串;
CGRectFromString(aString) 由字符串恢复出矩形;
CGRectInset(aRect) 创建较小或较大的矩形(中心点相同),+较小 -较大
CGRectIntersectsRect(rect1, rect2) 判断两矩形是否交叉,是否重叠
CGRectZero 高度和宽度为零的/位于(0,0)的矩形常量
CGPoint aPoint = CGPointMake(x, y); CGSize aSize = CGSizeMake(width, height);
设置透明度 [myView setAlpha:value]; (0.0 < value < 1.0)
设置背景色 [myView setBackgroundColor:[UIColor redColor]];
(blackColor;darkGrayColor;lightGrayColor;whiteColor;grayColor; redColor; greenColor; blueColor; cyanColor;yellowColor;magentaColor;
orangeColor;purpleColor;brownColor; clearColor; )
自定义颜色:
UIColor *newColor = [[UIColor alloc] initWithRed:(float) green:(float) blue:(float) alpha:(float)]; 0.0~1.0
19. 320X480 480X320 状态栏高 20 像素高 导航栏 工具栏 44像素高
隐藏状态栏:
[[UIApplication shareApplication] setStatusBarHidden: YES animated:NO]
横屏:
[[UIApplication shareApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight].
orientation == UIInterfaceOrientationLandscapeLeft
window=[[UIWindow alloc] initWithFrame:[UIScreen mainScreen] bounds];全屏
自动适应父视图大小:
aView.autoresizingSubviews = YES;
aView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
20.定义按钮
UIButton *scaleUpButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[scaleUpButton setTitle:@"放 大" forState:UIControlStateNormal];
scaleUpButton.frame = CGRectMake(40, 420, 100, 40);
[scaleUpButton addTarget:self action:@selector(scaleUp) forControlEvents:UIControlEventTouchUpInside];
设置视图背景图片
UIImageView *aView;
[aView setImage:[UIImage imageNamed:@”name.png”]];
view1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image1.png"]];
UISlider *slider = (UISlider *) sender;
NSString *newText = [[NSString alloc] initWithFormat:@”%d”, (int)(slider.value + 0.5f)];
label.text = newText;
活动表单 <UIActionSheetDelegate>
- (IBActive) someButtonPressed:(id) sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@”Are you sure?”
delegate:self
cancelButtonTitle:@”No way!”
destructiveButtonTitle:@”Yes, I’m Sure!”
otherButtonTitles:nil];
[actionSheet showInView:self.view];
[actionSheet release];
}
警告视图 <UIAlertViewDelegate>
- (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex
{
if(buttonIndex != [actionSheet cancelButtonIndex])
{
NSString *message = [[NSString alloc] initWithFormat:@”You can
breathe easy, everything went OK.”];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@”Something was done”
message:message
delegate:self
cancelButtonTitle:@”OK”
otherButtonTitles:nil];
[alert show];
[alert release];
[message release];
}
}
动画效果
-(void)doChange:(id)sender
{
if(view2 == nil)
{
[self loadSec];
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:([view1 superview]?UIViewAnimationTransitionFlipFromLeft:UIViewAnimationTransitionFlipFromRight)forView:self.view cache:YES];
if([view1 superview]!= nil)
{
[view1 removeFromSuperview];
[self.view addSubview:view2];
}else {
[view2 removeFromSuperview];
[self.view addSubview:view1];
}
[UIView commitAnimations];
}
[[UIApplication sharedApplication] setStatusBarHidden:TRUE];
改变navbar的颜色
self.navigationController.navigationBar.tintColor
= [UIColor darkGrayColor];
2.委托里 @required的必须实现
3.textField.contentVerticalAlignment
= UIControlContentVerticalAlignmentCenter;//文字内容垂直居中,此为UIControl的属性
4.textField.placeholder
= @"请输入文字";
//设置默认提示文字
5.全局变量设置 在APPDelegate.h ,AppDelegate.m 中定义你的全局变量名:例如(FMDatabase *db;)
调用的时候 (1)extern FMDatabase *db; (2) AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate]; appDelegate.你的变量
6.xcode 中为单独文件设置不支持ARC的方法 (项目本身启用了ARC,单需要用到FMDatabase,而FMDatabase不支持ARC)
修改方法: 选择项目---》Build Phases ----》Compile Sources --->选择需要的文件---》在右边Compile Flags 输入 -fno-objc-arc 即可。
7.IOS获取系统时间:
NSString *date;
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"YYYY-mm-dd hh:mm:ss"];
date = [formatter stringFromDate:[NSDate date]];
//date 显示为: 2011-12-02 12:12:12
想查找几天前的时间可以用这个方法:NSDate *date = [[NSDate alloc]init]; date = [date dateByAddingTimeInterval:-5*3600*24];
8.IOS 禁用多按钮同时点下的效果 [view setExclusiveTouch:YES];
9.IOS button上的title默认是英文的,要修改成中文的 在.plist文件中如下设置:
Localization native development region -China
Localized resources can be mixed -YES
10.在AppDelegate里面引用了UINavigationController,可以设置ToolBar显示。若想隐藏ToolBar,则要在Appelegate里面设置 [self.navigationController setToolBarHidden:YES animated:YES];
11.#import 和@class的区别 :import会包含这个类的所有信息,@class只是包含这个类的名字,至于如何定义的咱不用考虑。另外如果有循环依赖关系,如:A–>B, B–>A这样的相互依赖关系,如果使用#import来相互包含,那么就会出现编译错误,如果使用@class在两个类的头文件中相互声明,则不会有编译错误出现。
12.NSString和NSURL的相互转化:
NSString *str = @"www.baidu.com";
NSURL *url = [NSURL URLWithString:str]; //string--->url
NSString *str1 = [url absoluteString]; //url--->string
13.获取时间间隔:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-mm-dd HH:mm"];
NSDate *date1 = [dateFormatter dateFormString:@"2011-2-5 1:00"];
NSDate *date2 = [dateFormatter dateFormString:@"2011-2-6 2:00"];
NSTimeInterval time = [date2 timeIntervalSinceDate:date1];
int days = ((int)time)/(3600*24);
int hours = ((int)time)%(3600*24)/3600;
NSString *dateCountent = [[NSString alloc]initWithFormat:@"%i天,%i小时",days,hours];
14.退出程序 exit();
15.创建UIButton
(1). UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake (x, y, Width,
Height)];
(2). UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
设置UIButton标题 [button setTitle:title forState:UIControlStateNormal];
设置UIButton标题颜色 [button setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];
设置UIButton背景图片 [button setBackgroundImage:newImage
forState:UIControlStateNormal];
设置UIButton背景颜色 button.backgroundColor = [UIColor
clearColor];
16.UIButton状态:
UIControlStateNormal // 正常状态
UIControlStateHighlighted // 高亮状态
UIControlStateDisabled // 禁用状态
UIControlStateSelected // 选中状态
UIControlStateApplication //
UIControlStateReserved // 保留状态
UIButton类型:
UIButtonTypeCustom //自定义类型
UIButtonTypeRoundedRect
//圆角类型
UIButtonTypeDetailDisclosure
//细节展示按钮
UIButtonTypeInfoLight
//浅色背景的信息按钮
UIButtonTypeInfoDark
//暗色背景的信息按钮
UIButtonTypeContactAdd
// 添加按钮
17.退回输入键盘:
- (BOOL) textFieldShouldReturn:(id)textField{
[textField resignFirstResponder];
}
textField 为空时让按钮不可用,既防止误操作又显得应用很注重细节。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
NSMutableString *newValue = [[self.TextField.text mutableCopy] autorelease];
[newValue replaceCharactersInRange:range withString:string];
if ([newValue length]== 0) {
self.navigationItem.rightBarButtonItem.enabled = NO;
}
else {
self.navigationItem.rightBarButtonItem.enabled = YES;
}
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
self.navigationItem.rightBarButtonItem.enabled = NO;
return YES;
}
18.CGRect,CGPoint & CGSize
CGRect frame = CGRectMake (origin.x, origin.y, size.width, size.height);矩形
NSStringFromCGRect(someCG) 把CGRect结构转变为格式化字符串;
CGRectFromString(aString) 由字符串恢复出矩形;
CGRectInset(aRect) 创建较小或较大的矩形(中心点相同),+较小 -较大
CGRectIntersectsRect(rect1, rect2) 判断两矩形是否交叉,是否重叠
CGRectZero 高度和宽度为零的/位于(0,0)的矩形常量
CGPoint aPoint = CGPointMake(x, y); CGSize aSize = CGSizeMake(width, height);
设置透明度 [myView setAlpha:value]; (0.0 < value < 1.0)
设置背景色 [myView setBackgroundColor:[UIColor redColor]];
(blackColor;darkGrayColor;lightGrayColor;whiteColor;grayColor; redColor; greenColor; blueColor; cyanColor;yellowColor;magentaColor;
orangeColor;purpleColor;brownColor; clearColor; )
自定义颜色:
UIColor *newColor = [[UIColor alloc] initWithRed:(float) green:(float) blue:(float) alpha:(float)]; 0.0~1.0
19. 320X480 480X320 状态栏高 20 像素高 导航栏 工具栏 44像素高
隐藏状态栏:
[[UIApplication shareApplication] setStatusBarHidden: YES animated:NO]
横屏:
[[UIApplication shareApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight].
orientation == UIInterfaceOrientationLandscapeLeft
window=[[UIWindow alloc] initWithFrame:[UIScreen mainScreen] bounds];全屏
自动适应父视图大小:
aView.autoresizingSubviews = YES;
aView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
20.定义按钮
UIButton *scaleUpButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[scaleUpButton setTitle:@"放 大" forState:UIControlStateNormal];
scaleUpButton.frame = CGRectMake(40, 420, 100, 40);
[scaleUpButton addTarget:self action:@selector(scaleUp) forControlEvents:UIControlEventTouchUpInside];
设置视图背景图片
UIImageView *aView;
[aView setImage:[UIImage imageNamed:@”name.png”]];
view1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image1.png"]];
UISlider *slider = (UISlider *) sender;
NSString *newText = [[NSString alloc] initWithFormat:@”%d”, (int)(slider.value + 0.5f)];
label.text = newText;
活动表单 <UIActionSheetDelegate>
- (IBActive) someButtonPressed:(id) sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@”Are you sure?”
delegate:self
cancelButtonTitle:@”No way!”
destructiveButtonTitle:@”Yes, I’m Sure!”
otherButtonTitles:nil];
[actionSheet showInView:self.view];
[actionSheet release];
}
警告视图 <UIAlertViewDelegate>
- (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex
{
if(buttonIndex != [actionSheet cancelButtonIndex])
{
NSString *message = [[NSString alloc] initWithFormat:@”You can
breathe easy, everything went OK.”];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@”Something was done”
message:message
delegate:self
cancelButtonTitle:@”OK”
otherButtonTitles:nil];
[alert show];
[alert release];
[message release];
}
}
动画效果
-(void)doChange:(id)sender
{
if(view2 == nil)
{
[self loadSec];
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:([view1 superview]?UIViewAnimationTransitionFlipFromLeft:UIViewAnimationTransitionFlipFromRight)forView:self.view cache:YES];
if([view1 superview]!= nil)
{
[view1 removeFromSuperview];
[self.view addSubview:view2];
}else {
[view2 removeFromSuperview];
[self.view addSubview:view1];
}
[UIView commitAnimations];
}
相关文章推荐
- JavaScript中一些你不一定知道的问题(持续更新中。。。。)
- docker遇到的一些小问题(持续更新ing)
- selenium遇到的一些问题,持续更新
- caffe训练中遇到的一些问题(持续更新......)
- 关于Mybatis使用的一些问题汇总(持续更新)
- 项目适配iOS9遇到的一些问题及解决办法(持续更新)
- 一些开发中遇到的小问题小记(持续更新中)
- UITableView与UIScrollView的一些问题(持续更新)
- 一些小问题的解决办法(持续补充更新)
- iOS 持续更新 开发中的一些小问题和技巧1
- java 基础问题的一些处理,记录一下,持续更新呗
- 【持续更新】刷题时遇到的一些问题
- myeclipse中编写小java项目遇到的一些问题(持续更新)
- Android开发jni遇到的一些问题,持续更新
- UITableView与UIScrollView的一些问题(持续更新)
- cocos2d-x jsbinding出现的一些问题列表(持续更新)
- JNI开发遇到的一些问题记录(持续记录更新)
- 使用Vista的一些问题[持续更新]
- window 开发java 项目的 一些问题。持续更新,
- 使用Retrofit过程中碰到的一些问题(持续更新。。。。)