横竖屏:
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
--------
java里的toString()相当于NSObject的-(NSString*)description;
------
addSubView时没置顶:
//CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, -20.0);
//[self.navigationController.view setTransform:myTransform];
-----------
notification机制:
注册:[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(someFunction)
name:@"notificationIdentifier"
object:nil];
发送:[[NSNotificationCenter defaultCenter] postNotificationName:@"notificationIdentifier" object:nil]];
-------
NSObject的Observer机制
可以对任何一个NSObject的某个属性注册一个Observer:
[someNSObject addObserver:self
forKeyPath:@"someProperty"
options:(NSKeyValueObservingOptionNew)
context:NULL];
当someNSObject的someProperty值发生变化时,会自动调用当前类的
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context{}
其中改变后的新值会放在change里面。也可以用NSKeyValueObservingOptionOld使改变前的值放在change里。两者或运算则新值、旧值都放change里
-------
遍历Documents目录:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *directoryPath = [paths objectAtIndex:0];
NSArray *fileNames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
NSMutableArray *filePaths = [NSMutableArray arrayWithCapacity:[fileNames count]];
for (NSString * fileName in fileNames) {
}
后台下载:
downloadRequest = [[ASIHTTPRequest requestWithURL:task.url] retain];
[downloadRequest setShouldContinueWhenAppEntersBackground:YES];
[downloadRequest setShouldAttemptPersistentConnection:YES];
[downloadRequest setDownloadProgressDelegate:self];
[downloadRequest setNumberOfTimesToRetryOnTimeout:2];
[downloadRequest setAllowResumeForFileDownloads:YES];
NSString *savePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
stringByAppendingPathComponent:task.title];
[downloadRequest setTemporaryFileDownloadPath:[savePath stringByAppendingPathExtension:@"download"]];
[downloadRequest setDownloadDestinationPath:savePath];
[downloadRequest setDelegate:self];
[downloadRequest startAsynchronous];
SynthesizeSingleton
#define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \
\
static classname *shared##classname = nil; \
\
+ (classname *)shared##classname \
{ \
@synchronized(self) \
{ \
if (shared##classname == nil) \
{ \
shared##classname = [[self alloc] init]; \
} \
} \
\
return shared##classname; \
} \
\
+ (id)allocWithZone:(NSZone *)zone \
{ \
@synchronized(self) \
{ \
if (shared##classname == nil) \
{ \
shared##classname = [super allocWithZone:zone]; \
return shared##classname; \
} \
} \
\
return nil; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return self; \
} \
\
- (id)retain \
{ \
return self; \
} \
\
- (NSUInteger)retainCount \
{ \
return NSUIntegerMax; \
} \
\
- (oneway void)release \
{ \
} \
\
- (id)autorelease \
{ \
return self; \
}