设为首页 加入收藏

TOP

object-c常用类(一)
2014-11-23 18:53:14 来源: 作者: 【 】 浏览:21
Tags:object-c 常用

1.协议代理

A.h

#import 
  
   
@class ViewControllerA;

@protocol ViewControllerAdelegate
-(void)changeBgColorFromCtrlA:(ViewControllerA *)aView withColor:(UIColor *)color;
@end

@interface ViewControllerA : UIViewController
@property( assign, nonatomic ) id< ViewControllerAdelegate > delegate;


-(IBAction)changeColorAction:(id)sender;
@end
  

A.m

#import "ViewControllerA.h"
#import "ViewControllerB.h"
@interface ViewControllerA ()

@end

@implementation ViewControllerA
@synthesize delegate;


//使用协议代理Delegate
-(IBAction)changeColorAction:(id)sender{
    [delegate changeBgColorFromCtrlA:self withColor:[UIColor grayColor]];
    [[self navigationController] popViewControllerAnimated:YES];
}

@end

最终实现在B

B.h

#import 
  
   
#import "ViewControllerA.h"
@interface ViewControllerB : UIViewController
   
     @end
   
  

B.m

#import "ViewControllerB.h"

@implementation ViewControllerB

//响应协议代理Delegate
-(void)changeBgColorFromCtrlA:(ViewControllerA *)aView withColor:(UIColor *)color{
    [self.view setBackgroundColor:color];
}
@end

2.通知中心(其他同上面的例子)

a.m

//通知中心NSNotificationCenter
-(IBAction)changeColorAction2:(id)sender{
    UIColor *color = [UIColor greenColor];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeColorKey" object:color];
    [[self navigationController] popViewControllerAnimated:YES];
}

b.m

#import "ViewControllerB.h"

@implementation ViewControllerB

- (void)viewDidLoad
{
    [super viewDidLoad];
    //注册
    [[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(changeColor:)
												 name:@"ChangeColorKey"
											   object:nil];
  }


//响应通知中心NSNotificationCenter
- (void)changeColor:(NSNotification *)notification{
    if([notification object] != nil)
    {
        UIColor *color = [notification object];
        [self.view setBackgroundColor:color];
    }
}
@end

3.NSString

NSString *string = @"abc";

比较:[a isEqualToString:b]

匹配开头结尾: [a hasPrefix:@"ab"] ; [a hasSuffix:@".txt"];

忽略大小写比较: [a caseInsensitiveCompare:b] == NSOrderedSame;

拼接: NSString *newS = [NSString stringWithFormat:@"%@%@",a,b ]; //已有的话用[a appendFormat :[NSString stringWithFormaat:@"hhhhhhh"] ];

分割:(字符串分割)

NSString *string = [[NSString alloc] initWithString:@"One,Two,Three,Four"];
NSArray *array = [string componentsSeparatedByString:@","];

(数组组成字符串)

//从数组合并元素到字符串- componentsJoinedByString:
NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
NSString *string = [array componentsJoinedByString:@","];

大写: [a uppercaseString];

小写: [a lowercaseString]

截取:

从头开始:[a substringToIndex:3];

到哪里: [a substringFromIndex:3];

一段: [a substringWithRange:NSMakeRange(0,4)];


文件扩展名: [path pathExtension]



/*----------------从文件读取字符串:initWithContentsOfFile方法----------------*/   
 
NSString *path = @"astring.text";
NSString *astring = [[NSString alloc] initWithContentsOfFile:path];
NSLog(@"astring:%@",astring);
[astring release];
 
 
/*----------------写字符串到文件:writeToFile方法----------------*/   
 
 
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
NSLog(@"astring:%@",astring);
NSString *path = @"astring.text";    
[astring writeToFile: path atomically: YES];
[astring release];    

4.NSSArray

NSArray *array = [[NSArray alloc] initWithObjec
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇[Objective-C]OC中自定义对象的归.. 下一篇object-c基本知识

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: