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