小的知识点笔记

来源:互联网 发布:触摸屏怎样编程 编辑:程序博客网 时间:2024/05/15 23:44

 

获取ios版本,硬件信息

UIDevice-Hardware.h  UIDevice-Hardware.m

 

UTF8--gb2312转换库 

libiconv.dylib 

 

 @selector

选择方法时后面加不加冒号?
在使用@selector时,方法应不应该加冒号,方法有参数加,没有不用加


 

delegate (就是一个指针)

delegate往往是assign方式的属性而不是retain方式的属性,赋值不会增加引用计数,就是为了防止delegation两端产生不必要的循环引用。

 

添加和删除导航栏按钮

添加: 

UIBarButtonItem *regButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"旋转" style:UIBarButtonItemStyleDone target:self action:@selector(rotateVideo)];
     self.navigationItem.rightBarButtonItem = regButtonItem;
     [regButtonItem release]; 

删除

self.navigationItem.rightBarButtonItem = nil;

 

//左箭头返回按钮

    self.navigationItem.title = @"tiltle";    
    
    UIButton* backButton = [UIButton buttonWithType: 101]; // left-pointing shape!
   
    [backButton addTarget: self action: @selector(cancel) forControlEvents: UIControlEventTouchUpInside];
   
    [backButton setTitle: @"返回" forState:UIControlStateNormal];
    
   UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

 

 self.navigationItem.leftBarButtonItem = backItem;
    [backItem release];

 

 

 

头文件包含

一般尽量将头文件包含到.m文件中,不然可能导致其他一些问题

有时候在头文件中有定义某个类ABC的对象,可以加个声明 @class ABC;  ,类似C语言中 声明某个ABC  是个结构体 : typedef struct ABC

有的时候写个库,你的头文件包含了其他文件,别人使用的使用也需要这个头文件就比较麻烦,如果放到实现文件里,就不需要了


 

Block

void (^vFun)()=^(void){
        NSLog(@"void function");
    };
    vFun();//void function
  //void:返回类型
  //(^vFun):vFun代表函数名
  //(xx):传入参数的类型,为空代表木有参数
  //^(xxx):block格式,xxx代表传入参数
   
    int (^iFun)(int) = ^(int i){
        return i;
    };
    NSLog(@"%d",iFun(12345));//12345
   
    NSString *str = @"World";
    NSString* (^sFun)(NSString *, int)= ^(NSString *s1, int i){
        return [s1 stringByAppendingString:str];
    };
    str = @"Max";
    NSLog(@"%@",sFun(@"Hello ",2));//Hello World 

对于sFun,创建block对象时,此对象只会捕获当前引用值。以后引用值怎么变,只会使用之前的值。

 

内存管理:

  Block对象可以发送retain等消息,但Block分配于栈,函数运行结束,系统会自动释放。

  让计数器+1,可以使用copy消息。

  保留Block对象的作用:1.在Block对象的函数外能执行此Block对象。
                         2.保存Block对象引用的对象。

  使用__block修饰符,系统不再保留Block对象内,指针所指向的对象。

  如上sFun()方法,在str方法前添加__block,sFun显示:Hello Max  

__block NSString *str = @"World";

  Block对象内使用实例变量,self retainCount+1.
  可以使用局部变量。

//@property (nonatomic, strong)NSString *name;- (void)method{    //NSString *s = name;//name为实例变量.    void (^aBolck)()=^(void){        NSLog(@"Hello, %@", name);    };    void (^aCopy)()=[aBolck copy];    aCopy();    NSLog(@"%d", [self retainCount]);//不使用局部变量,值为2}

 

 

iphone程序的开始画面设置

* iPhone软件启动后的第一屏图片是非常重要的往往就是loading载入中的意思。设置它说来也简单,但是却无比重要

只需要在resource里面将你希望设置的图片更名为Default.png,这个图片就可以成为iPhone载入的缺省图片

屏幕快照 2009-07-25 上午10.50.19

 

 

//是某个控件缓慢移动位置

  [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.7];    
           
    CGRect frameButton = testButton.frame;//estButton为界面上某个按钮的 IBOutlet
    
    frameButton.origin.y -= 20;
   
    testButton.frame = frameButton;        
    
    [UIView commitAnimations];

 

利用上段代码,可以实现多个控件放在一起,根据手势缓慢移动显示出来,也可以缓慢移动消失。
   

 

   

 

 

视图翻页效果

CGContextRef context = UIGraphicsGetCurrentContext(); 

[UIView beginAnimations:nil context:context];

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 

[UIView setAnimationDuration:1.0];

// Apply the animation to the backdrop

[UIView setAnimationTransition: 

    UIViewAnimationTransitionCurlUp

    forView:myView cache:YES];

// Exchange the two foreground views 

[myView exchangeSubviewAtIndex:0

    withSubviewAtIndex:1]; 

[UIView commitAnimations];

 

//设备是否静音  ,经测试不管用

-(BOOL)silenced 
{
     #if TARGET_IPHONE_SIMULATOR
         // return NO in simulator. Code causes crashes for some reason.
         return NO;
     #endif
 
    CFStringRef state;
    UInt32 propertySize = sizeof(CFStringRef);
    AudioSessionInitialize(NULL, NULL, NULL, NULL);
    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize;, &state;);
    if(CFStringGetLength(state) > 0)
            return NO;
    else
            return YES;
}

 

//限制长度

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range 
                            replacementString:(NSString *)string 
{
    //Check that response should be a maximum 5 characters
    if ([string length] > 0) {
        return [textField.text length] < 5;
    }
    return YES;
}

利用 Core Foundation 生成 UUID

方法1、

[[NSProcessInfo processInfo] globallyUniqueString];

方法2、

+ (NSString*)UUIDString { 
   CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault); 
    CFStringRef stringRef = CFUUIDCreateString(kCFAllocatorDefault, uuidRef); 
    CFRelease(uuidRef); 
     return [(id)stringRef autorelease]; 
}