iOS开发的一些小技巧

来源:互联网 发布:浙江华通云数据上市 编辑:程序博客网 时间:2024/05/19 00:08
  • 1.任意设置Cell选中状态的背景色:
UIView *bgView = [[UIView alloc] init];bgView.backgroundColor = [UIColor orangeColor];self.selectedBackgroundView = bgView;

该方法设置的是纯色, 也可以使用任何图片,把selectedBackgroundView设成UIImageView。

  • 2.方法flashScrollIndicators:这个很有用,闪一下滚动条,暗示是否有可滚动的内容。可以在ViewDidAppear或[table reload]之后调用。
  • 3.点击Cell中的按钮时,如何取所在的Cell
-(void)OnTouchBtnInCell:(UIButton *)btn{CGPoint point = btn.center;point = [table convertPoint:point fromView:btn.superview];NSIndexPath* indexpath = [table indexPathForRowAtPoint:point];UITableViewCell *cell = [table cellForRowAtIndexPath:indexpath];...//也可以通过一路取btn的父窗口取到cell,但如果cell下通过好几层subview才到btn,就要取好几次 superview,所以我用上面的方法,比较通用。这种方法也适用于其它控件。}
  • UIColor colorWithRed:green:blue:alpha:这个方法的参数必须用浮点型。
    假如使用Xcode自带的取颜色的工具,取到的RGB值分别为:25,25,25,
    传给上述方法的参数应为25/255.0或25.0/255。如果用整型25/255,经过取整,小数部分没有了,显示出来的颜色和取到的是不一样的。可以定义一个宏:
    define RGB(A,B,C) [UIColor colorWithRed:A/255.0 green:B/255.0 blue:C/255.0 alpha:1.0]
    然后用RGB(25,25,25)就可以了
  • 禁止textField和textView的复制粘贴菜单
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{if ([UIMenuController sharedMenuController]) {[UIMenuController sharedMenuController].menuVisible = NO;}return NO;}
  • 如何进入软件在app store 的页面:
    先用iTunes Link Maker找到软件在访问地址,格式为itms-apps://ax.itunes.apple.com/…,然后
#define ITUNESLINK @"itms-apps://ax.itunes.apple.com/..."NSURL *url = [NSURL URLWithString:ITUNESLINK];if([[UIApplication sharedApplication] canOpenURL:url]){[[UIApplication sharedApplication] openURL:url];}

到app store。
iTunes Link Maker地址:http://itunes.apple.com/linkmaker

  • someview显示一断时间后自动消失
    [self performSelector:@selector(dismissView:) withObject:someview afterDelay:2];
    这么写比用NSTimer代码少,不过哪种都行的,这里只是提供一种不同的方法
  • 使提示窗口在任何界面都能显示:
    [self.navigationController.view addSubview:(自定义的提示窗口)]
    或用UIAlertView
  • 禁止程序运行时自动锁屏
    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
  • 判断一个字符串是否包含另一个字符串
    [str1 rangeOfString:str2].length != 0 ? @”包含” : @”不包含”
0 0