您的位置:首页 > 移动开发 > IOS开发

ios开发中常见问题 (一)

2014-09-19 15:25 288 查看
//开起导步线程

    dispatch_queue_t myQueue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(myQueue, ^{

        //下载图片

        NSString *picURL_=@"http://f.hiphotos.baidu.com/album/w%3D2048/sign=a3a3545dac6eddc426e7b3fb0de3b7fd/0b55b319ebc4b745eefecdaecefc1e178a82151b.jpg";

        NSData *data_=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:picURL_]];

        //回主线程

        dispatch_async(dispatch_get_main_queue(), ^{

            if (data_) {

                self.imageView.image=[UIImage imageWithData:data_];

                

            }

        });

    });

——————————————————————————————————————

 TableViewCell背景设置


-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

  cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"58rowWrap.png"]]; //cell的背景图
  cell.textLabel.backgroundColor
= [UIColor clearColor];
}

 TableView背景设置

 

UIImageView *tableBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"XXX.png"]];
[yourTable setBackgroundView:tableBg];
——

_POnlineTableView.backgroundColor = [ UIColor colorWithPatternImage:[UIImage imageNamed:@"statistics-bk.png"] ];

——————————————————————————————————————

@property (retain, nonatomic) NSArray *questionTitleTrip;

在头文件中这样写,默认会生成一个_questionTitleTrip变量。

所以,在实现文件中最好这样写。

@synthesize questionTitleTrip = _questionTitleTrip

——————————————————————————————————————

判断两个字符串是否相等:

 [mystring compare:@"string"]==NSOrderedSame

NSString * tmpStr=@"how to stop”;if([selected isEqualToString:tmpStr])

—————————————————————————————————

iOS中的arc4random方法:

通过arc4random() 获取0到x-1之间的整数的代码如下:

int value = arc4random() % x;

获取1到x之间的整数的代码如下:

int value = (arc4random() % x) + 1;

最后如果想生成一个浮点数,可以在项目中定义如下宏:

#define ARC4RANDOM_MAX      0x100000000

然后就可以使用arc4random() 来获取0到100之间浮点数了(精度是rand()的两倍),代码如下:

double val = floorf(((double)arc4random() / ARC4RANDOM_MAX) * 100.0f);

——————————————————

为UIImageView添加点击事件

UIImageView并不像UIButton一样,点点鼠标就可以关联点击事件,也不像Android里有onClickListener,这里需要借助于UITapGestureRecognizer类。从类名上就可以看出,这个类就是用于处理tap(单击)事件的。

(bbc和voaspecial是UIImageView对象)

    [bbc setUserInteractionEnabled:YES];

    [voaspecial setUserInteractionEnabled:YES];

    [bbc addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickCategory:)]];

    [voaspecial addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickCategory:)]];

经测试,多个UIImageView不能共用一个UITapGestureRecognizer对象,之前调用的会没效果。

根据点击的view判断来自谁的点击。

-(void)clickCategory:(UITapGestureRecognizer *)gestureRecognizer

{

    NSLog(@"click");

    NSLog(@"%hhd",[gestureRecognizer isMemberOfClass:[UITapGestureRecognizer class]]);

 

    UIView *viewClicked=[gestureRecognizer view];

    if (viewClicked==bbc) {

        NSLog(@"bbc");

    }else if(viewClicked==voaspecial)

    {

        NSLog(@"voaspecial");

    }

 

}  

—————————————————————————————————

判断点击的是哪个Button按钮

 - (void)ChoseFunc:(id)sender

 {

 UIButton *btn = (UIButton *)sender;

 NSLog(@"btn.titleLabel.text===%@",btn.titleLabel.text);

 }

—————————————————————————————————

页面之间的跳转:

从一个Controller跳转到另一个Controller时,一般有以下2种:

1、利用UINavigationController,调用pushViewController,进行跳转;这种采用压栈和出栈的方式,进行Controller的管理。调用popViewControllerAnimated方法可以返回。

    PickImageViewController *ickImageViewController = [[PickImageViewController alloc] init];

    [self.navigationController pushViewController: ickImageViewController animated:true];

    [ickImageViewController release];

2、利用UIViewController自身的presentModalViewController,进行跳转;调用dismissModalViewControllerAnimated方法可以返回。

    PickImageViewController *ickImageViewController = [[PickImageViewController alloc] init];

    [self presentModalViewController:ickImageViewController animated:YES];

//返回
[self dismissModalViewControllerAnimated:YES];

—————————————————————————————————

NSString字符串截取三种方法 :

 1.定义一个字符串a, 截取a 的某一个项目组,复制给b, b必须是int型

  NSString *a = @"1.2.30";

  int  b= [[a substringWithRange:NSMakeRange(4,2)] intValue];

  NSLog(@"a:%@  \n",a  );

  NSLog(@"b:%d",b  );

a:1.2.30  

b:30

解析如下:substringWithRange: 专门截取字符串的一块肉

           NSMakeRange(4,2)    从第4个字符开端截取,长度为2个字符,(字符串都是从第0个字符开端数的哦~!)

       b = [a intValue]; 将 a 转换为 整数型

       b = [a floatValue];   将 a 转换为 小数型

       b = [a boolValue]; 将 a 转换为 布尔型(true /  false)

       b = [a integerValue]; 将 a 转换为 整数型

       b = [a longLongValue]; 将 a 转换为 长整型

2。 字符串截取到第n位  (substringToIndex: n)(第n 位不算再内)

- (void)viewDidLoad

{

    NSString  *a = @"i like long dress";

    NSString *b = [a substringToIndex:4];

    NSLog(@"\n b: %@",b);

}    

b: i li

3。字符串从第n 位开端截取,直到最后 (substringFromIndex:n)(包含第 n 位)

- (void)viewDidLoad

{

    NSString  *a = @"i like long dress";

    NSString *b = [a substringFromIndex:4];

    NSLog(@"\n b: %@",b);

}

b: ke long dress

—————————————————————————————————

ios开发中,全局变量设置和调用方法如下:

在AppDelegate.h文件中设置全局变量:

@interface ***AppDelegate{

NSString *indexStr;

}

@property (nonatomic, retain) NSString  *indexStr;

@end

在AppDelegate.m文件中实现全局变量:

@synthesize indexStr;

假如在 CallBack页面调用,在CallBack.m中包含AppDelegate.h文件,并定义一个代理实例,如下

#import "AppDelegate.h"

在 - (void)****{

   AppDelegate *indexDelegate = [[UIApplication sharedApplication] delegate];

  indexDelegate.indexStr = @"gggg ";

}

---------------------------------------------------------------------------------------------------------

UIActionSheet在iOS7.0中按钮不响应点击事件:

把[actionSheet showInView:self.view];

改成[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 常见问题