iOS特别技能点总结2

iOS 通知

1.在数据发生变化的界面进行通知注册和发送

 NSArray *saveImageArray = [[NSArray alloc] initWithObjects:@"123", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"postData" object:saveImageArray];

2.在通知接收界面进行通知监测和作出反应

[[NSNotificationCenter defaultCenter] addObserver:self
                                      selector:@selector(PostImage:)//接收消息方法
                                          name:@"postData"//消息识别名称
                                        object:nil];
//实现方法
-(void)PostImage:(NSArray *)array {

    self.messageSign.hidden = NO;
    NSLog(@"array ===  %@", array);
    //接收传送过来的消息
}

3.在接收通知的界面消失的时候进行–通知观察者的移除

 //移除observer   
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"postData" object:nil];

界面间的跳转 – (storyboard方式搭建界面)

1.界面间有 segue 的界面跳转

[self performSegueWithIdentifier:@"segue的Identifier" sender:nil]; // 添加在需要跳转的地方

// 有 segue 的跳转都会调用到的方法
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

   // 传送的数据
   ViewController *lVC = [segue destinationViewController];
   VC.team_id = self.team_id1;
}

2.界面间没有连线或者在不同的 storyboard 界面跳转

// 获取要跳转的 Storyboard
UIStoryboard *scoreStoryboard = [UIStoryboard storyboardWithName:@"Personal" bundle:nil];

// 需要在目标界面设置 Storyboard ID
PayViewController *payVC = [scoreStoryboard instantiateViewControllerWithIdentifier:@"PayViewController"];
payVC.diamondCount = 1;

[self.navigationController pushViewController:payVC animated:YES];
  • 错误 :UIImage stringByDeletingPathExtension]: unrecognized selector sent to instance 0xbe79910

    原因 :你所使用的对象已经是 UIImage ,不能再分割成其他对象

    解决方案: 寻找使用了这个对象的地方,并修改。

单独对一些文件设置 ARC 或者设置 非ARC

1.如果项目建立时未使用ARC,想将其改为ARC,可以在building setting中修改

在Building Setting中搜索auto reference

将Objective-C Automatic Reference Counting一行设置为YES即可

2.因为之前没有ARC机制,好多比较好的类库都是使用的非ARC,或是有些大牛还是不喜欢用ARC,封装的类也是非ARC的,想要在自己的ARC项目中使用这些非ARC类库,只需要简单的设置一下就可以了。

在TARGETS-Bulid Phares-Compile Sources中找到非ARC的文件,双击,在弹出的框中添加

   -fno-objc-arc

3.UIImage转化成NSData

NSData *data;
if (UIImagePNGRepresentation(image) == nil) {

      data = UIImageJPEGRepresentation(image, 1);
    } else {

      data = UIImagePNGRepresentation(image);
}