项目中的技巧经验汇总

1、 若将一个view作为子视图添加到window中,则当设备的方向变换时,该视图不会随之变化,也就是所不会响应设备的方向变化事件。

如:“图片放大视图”,“报纸选择视图”...

要使其响应方向变化事件,最好是将该视图添加到一个视图中而不是window中。

2、通过Interface Builder创建的视图默认的背景色是白色的,

要使得该视图透明,可以将其背景色改为clearcolor。

3、再viewcontroller的viewdidload方法中,self.view一直是nil的。

所以在viewdidload中使用[uiactionsheet showinview:self.view];就会使程序崩溃。

4、获取设备当前的放置方向,只要获取当前应用的状态条方向即可,如:

[self SetSubviewsFrame:[[UIApplication sharedApplicationstatusBarOrientation]];

 

5、UIButton设置按钮图片的方式为:

[iButton setImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];

 

而不能像下面这种方式:iButton.imageView.image = [UIImage imageNamed:@"a.png"];

 

6、对一个视图使用动画时,若该视图包含子视图(如按钮,图片等内容),要使得动画进行时子视图不变形,可添加如下代码:

[self setClipsToBounds:YES];  //这里self是view.

 

7、属于工程resource中的内容都是只读的,不能修改。
 比如在我的项目中使用读取resource下的plist文件的内容,能获取到内容;
 但将内容写进去就会失败,读出来仍然是原来的内容。

解决方法:

如果要写入plist,文件路径的获取就不能用

“ NSString *plistpath=[[NSBundle mainBundle]pathForResource:@"member" ofType:@"plist"];”(我原本这么写的,得到的路径是resource文件夹下的plist文件)。

 

正确的做法是将要保存的plist文件放在一个sandbox沙盒里,文件路径这样
“NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* sandBoxpath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,@"member.plist"];

我这样写入后,并且再读出,就成功了。

 

 

8、

  1)如果对性能要求高,慎用Interface Build;

  2)copy是创建,retain是引用;

  3)加载到NSArray/NSMutableArray里的对象,不需要负责release;

  4)UIApplicationDelegate负责应用程序的生命周期,而UIViewController负责View的生命周期;

 

 

9、 如果表格是在UIViewController里并且table的delegate是ViewController, 那么table必须是viewcontroller的view,而不是能是viewController 的view的一个子视图。

否则在表格编辑的时候就不会触发willBeginEditingRowAtIndexPath,以及 didEndEditingRowAtIndexPath等方法。

  相关网页{http://www.flyblog.info/catprogramming/196.html}

 

 

10、用[NSMutableDictionary dictionary];方法初始化的 NSMutableDictionary类型对象,不需要release;也不需要用removeallobjects清空;

在再次用 [NSMutableDictionary dictionary]方法来初始化前,它会自动调用dealloc方法的。

 

11、在有tabbar的应用中使用nsnotification时,只有当前选中tab的viewcontroller才会接到通知,而其它tab是不会收到的。

 

12、在代码中有用到动画的地方要注意,由于动画的运行是有一个时间间隔的,故其内部实现机制其实是新开一个线程在执行的。所以在函数中用到动画的地方,一般动画之后的代码会先运行,等这个函数的所有代码执行完之后才开始运行动画。

  这样就会造成界面的迟缓问题(如在做报纸选择器时让其动画隐藏,然后再开始下载报纸,而程序运行的顺序却是先下载报纸,之后才动画隐藏报纸选择器视图。)

  或动画不执行的问题(如想让一个对象缓慢移动到某个位置,之后再变换其图片;而程序运行结果却是视图直接到达该位置并变换了图片,没有执行动画)。

  这些问题的解决方法是:要给动画添加一个动画执行完之后处理方法,在该方法中进行动画之后的处理。

如:

[UIView setAnimationDidStopSelector:@selector(hiddenPanelStopped)];

当然不要忘了给该动画设置代理:

[UIView setAnimationDelegate:self];

 

一般的动画处理代码如下:

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:kDefaultAnimationInterval];

[UIView setAnimationCurve:UIViewAnimationCurveLinear];

[UIView setAnimationDelegate:self];

[UIView setAnimationDidStopSelector:@selector(hiddenPanelStopped)];

[self setFrame:hideRect];

[UIView commitAnimations];

 

13、一个视图中的子视图的tag可以设成相同,只要能在代码中识别出来就行。

  如在报纸选择器中,其实可以将每个报纸按钮和new图标的tag设置成相同的,

  这样,在代码中使用时,只要添加下该tag所指向的具体为哪个视图即可,如使用

[tempView isKindOfClass:[UIButton class]];

  

  另一种做法是,将new图标和报纸按钮作为一组添加到一个视图中,再将这个视图添加到报纸选择器中。

 

14、tablecell高亮时可以考虑将其中字体的颜色换掉(如从黑色变为白色);

 

15、将应用屏幕限制在横屏的方法:

  在应用进入时,先将应用设置为横屏模式:

[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;

  在屏幕旋转时,只响应横屏的变化:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

  

16、获取字符串占用的尺寸:

CGSize detailSize = [@"你的字符串" sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(200, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];


17、将视图滚动到可见的实现:

// Scroll the |UIScrollView|s to display |view| on the screen.

- (void)scrollToVisible:(UIView *)view {

  UIView *parent = [view superview];

  while (parent) {

    if ([parent isKindOfClass:[UIScrollView class]]) {

      UIScrollView *scrollView = (UIScrollView *)parent;

      CGRect rect = [view convertRect:[view boundstoView:scrollView];

      [scrollView scrollRectToVisible:rect animated:YES];

    }

    parent = [parent superview];

  }

}

18、代码触发点击事件:

复制代码
// Sends touch event to specified view. This method should be executed on main
// thread.
- (void)performTouch:(UIView *)view {
// Make the view visible.
[self scrollToVisible:view];

// Create touch event for the view. Note [touch view] has the topmost element
// in the window. Tap event should be sent to the topmost element, not to the
// specified view.
UITouch *touch = [[[UITouch alloc] initInView:view] autorelease];

// The view should get focused.
if ([[touch view] canBecomeFirstResponder]) {
[[touch view] becomeFirstResponder];
}

// Send touchesBegan and touchesEnded messages.
NSSet *touches = [NSSet setWithObject:touch];
UIEvent
*event = [[[NSClassFromString(@"UITouchesEvent") alloc]
initWithTouch:touch] autorelease];
[[touch view] touchesBegan:touches withEvent:
event];
[touch setPhase:UITouchPhaseEnded];
[[touch view] touchesEnded:touches withEvent:
event];
}
复制代码

  

19、实现屏幕截图的代码:

        UIGraphicsBeginImageContext(self.view.frame.size);
	CGContextRef context = UIGraphicsGetCurrentContext();
	[self.view.layer renderInContext:context];
	UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
	UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
	UIGraphicsEndImageContext();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值