1.协议代理
A.h
#import <UIKit/UIKit.h>
@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 <UIKit/UIKit.h>
#import "ViewControllerA.h"
@interface ViewControllerB : UIViewController<ViewControllerAdelegate>
@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] initWithObjects:@"One",@"Two",@"Three",@"Four",nil]; //注意用nil结尾
增加元素: addObject
删除:removeObjectAtIndex:1
复制数组:
直接复制
NSMutableArray *MutableArray = [[NSMutableArray alloc] init];
NSArray *array = [NSArray arrayWithObjects:
@"a",@"b",@"c",nil];
MutableArray = [NSMutableArray arrayWithArray:array];
用循环:
NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSArray *oldArray = [NSArray arrayWithObjects:
@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
for(int i = 0; i < [oldArray count]; i++)
{
obj = [[oldArray objectAtIndex:i] copy];
[newArray addObject: obj];
}
快速枚举:
for(id obj in oldArray)
{
[newArray addObject: obj];
}
或者:
NSEnumerator *enumerator;
enumerator = [oldArray objectEnumerator]; //reverseObjectEnumerator 从后往前
id obj;
while(obj = [enumerator nextObject])
{
[newArray addObject: obj];
}
排序:
[newArray sortUsingSelector:@selector(compare:)];
5.NSDictionary
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];
NSString *string = [dictionary objectForKey:@"One"];
[dictionary setObject:@"One" forKey:@"1"];
//删除指定的字典[dictionary removeObjectForKey:@"3"];
6.文件夹操作:
/*******************************************************************************************
从目录搜索扩展名为jpg的文件
*******************************************************************************************/
//NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *home;
home = @"../Users/";
NSDirectoryEnumerator *direnum;
direnum = [fileManager enumeratorAtPath: home];
NSMutableArray *files = [[NSMutableArray alloc] init];
//枚举
NSString *filename;
while (filename = [direnum nextObject]) {
if([[filename pathExtension] hasSuffix:@"jpg"]){
[files addObject:filename];
}
}
//快速枚举
//for(NSString *filename in direnum)
//{
// if([[filename pathExtension] isEqualToString:@"jpg"]){
// [files addObject:filename];
// }
//}