iPhone MediaPlayerController
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
iPhone 在UIWebView中的电子阅读器滑动和隐藏的底部导航
#define HORIZ_SWIPE_DRAG_MIN 100
CGPoint mystartTouchPosition;
BOOL isProcessingListMove;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint newTouchPosition = [touch locationInView:self.view];
if(mystartTouchPosition.x != newTouchPosition.x || mystartTouchPosition.y != newTouchPosition.y) {
isProcessingListMove = NO;
}
mystartTouchPosition = [touch locationInView:self.view];
[super touchesBegan:touches withEvent:event];
[[self.wv.subviews objectAtIndex:0] touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = touches.anyObject;
CGPoint currentTouchPosition = [touch locationInView:self.view];
// If the swipe tracks correctly.
double diffx = mystartTouchPosition.x - currentTouchPosition.x + 0.1; // adding 0.1 to avoid division by zero
double diffy = mystartTouchPosition.y - currentTouchPosition.y + 0.1; // adding 0.1 to avoid division by zero
if(abs(diffx / diffy) > 1 && abs(diffx) > HORIZ_SWIPE_DRAG_MIN)
{
// It appears to be a swipe.
if(isProcessingListMove) {
// ignore move, we're currently processing the swipe
return;
}
if (mystartTouchPosition.x < currentTouchPosition.x) {
isProcessingListMove = YES;
[self moveToPreviousItem];
return;
}
else {
isProcessingListMove = YES;
[self moveToNextItem];
return;
}
}
else if(abs(diffy / diffx) > 1)
{
isProcessingListMove = YES;
[[self.wv.subviews objectAtIndex:0] touchesMoved:touches withEvent:event];
[super touchesMoved:touches withEvent:event];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
isProcessingListMove = NO;
[super touchesEnded:touches withEvent:event];
[[self.wv.subviews objectAtIndex:0] touchesEnded:touches withEvent:event];
}
iPhone 把ç照着‰‡åº“é‡嗅鞘细胞“A>¾ç‰‡å¤??å¶å°Documentsæ-‡ä»¶å¤¹
directoryPath = [NSString stringWithFormat:@"%@",NSDocumentDirectory()];
NSString *SaveToDirectoryPath =
[directoryPath
stringByAppendingPathComponent: willSaveToDirectory];
[[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@/%@",SaveToDirectoryPath,DownloadFileName] contents:recvData attributes:nil];
iPhone CoreData实ä¾<A†天极??å|ä¹ï¼1)
Core Data是个好东西,在数æ®å‚¨å˜æ“作上速度快,容易æ“作,是一ç§ç±»ä¼¼å…³ç³»æ•°æ®åº“的东西。但是有些ä¸é‚£ä¹ˆå¥½å¦ï¼Œé‚£åˆ°åº•Core Data是怎么æ“作的呢?怎么用呢?怎么æ¥ç¼–程呢?我们一起æ¥å¦ä¹ å§ï¼ŒæŽ¥ä¸‹æ¥ä½¿ç”¨è‹¹æžœæ供的实例程åºLocationsæ¥ä½œåˆ†æžï¼š
>程åºä»‹ç»ï¼š
å³ä¾§æ˜¯æ”¹ç¨‹åºçš„截图,基本上æ¥è¯´å°±æ˜¯é€šè¿‡ä½¿ç”¨Core Locationæ¥å¾—到当时的ä½ç½®ï¼Œç„¶åŽåœ¨ç‚¹å‡»â€œ+â€çš„时候记录下当时的ç»çº¬åº¦ã€‚通过UITableViewController的功能æ¥æ·»åŠ ï¼Œç¼–è¾‘ï¼Œåˆ é™¤ç‰åŠŸèƒ½ã€‚整体程åºä½¿ç”¨Core Dataæ¥å‚¨å˜æ•°æ®ï¼Œå®žä½“(Entity)为一个ä½ç½®ï¼ŒåŒ…括以下å‚数:1,时间(收集数æ®çš„时间)2,纬度,3,ç»åº¦
首先我们看看该程åºçš„AppDelegate.h
@interface LocationsAppDelegate : NSObject {
UIWindow *window;
UINavigationController *navigationController; //导航æ
//以下定义了Core Data的三个决定性组建,ç‰åŽé¢m文件里é¢å†å¤šä»‹ç»
NSPersistentStoreCoordinator *persistentStoreCoordinator;
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UINavigationController *navigationController;
- (IBAction)saveAction:sender; //这个没找到作用...æ ¹æœ¬å°±æ²¡ç”¨åˆ°IB
//还记得å§ï¼Œnonatomicæ˜¯å› ä¸ºè¿™ä¸ªç¨‹åºæ˜¯å•çº¿ç¨‹
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator
*persistentStoreCoordinator;
@property (nonatomic, readonly) NSString *applicationDocumentsDirectory;
//程åºæ–‡æ¡£ç›®å½•æ˜¯ä¸»è¦ä¸ºäº†ç»™NSPersistentStoreCoordinator找个å˜SQLite文件的地方
@end
从上é¢çš„我们能看出æ¥ï¼Œè¯¥ç¨‹åºæ˜¯é€šè¿‡ä¸€ä¸ªæ ¹Core Dataæ•°æ®ç®¡ç†æ¥ç®¡ç†æ•´ä¸ªç¨‹åºçš„CoreDataæ•°æ®çš„,接下æ¥çœ‹m文件也会对æ¤ä½œæ›´å¤šçš„ç†è§£ã€‚
#import "LocationsAppDelegate.h"
#import "RootViewController.h" //该程åºä½¿ç”¨äº†ä¸€ä¸ªæ ¹è§†å›¾æŽ§åˆ¶å™¨ï¼Œæ‰€ä»¥å¯¼å…¥äº†è¿™ä¸ªç±»
@implementation LocationsAppDelegate
@synthesize window;
@synthesize navigationController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
//åˆå§‹åŒ–æ ¹è§†å›¾æŽ§åˆ¶å™¨ï¼Œå®ƒæ˜¯ä¸€ä¸ªåˆ—è¡¨è§†å›¾æŽ§åˆ¶å™¨
RootViewController *rootViewController = [[RootViewController alloc]
initWithStyle:UITableViewStylePlain];
//通过下é¢çš„自定义getter得到CoreData的“被管ç†å¯¹è±¡å†…容器â€
NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
// 如果getter出错,在这里æ¥æŽ’é”™
}
rootViewController.managedObjectContext = context;
//rootViewController有一个本地“被管ç†å¯¹è±¡å†…容器â€ï¼Œåœ¨è¿™é‡Œèµ‹å€¼è¿‡åŽ»
UINavigationController *aNavigationController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];
self.navigationController = aNavigationController;
//åˆå§‹åŒ–导航æ ï¼Œæ ¹è§†å›¾ä¸ºrootViewController,并指定该导航æ 为程åºå¯¼èˆªæ
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
//由于导航æ retainäº†æ ¹è§†å›¾ï¼Œæ‰€ä»¥åœ¨è¿™é‡Œå¯ä»¥release掉它了
[rootViewController release];
//由于selfå·²ç»retain了导航æ ,所以导航æ å¯ä»¥release
[aNavigationController release];
}
//applicationWillTerminate: 在程åºç»“æŸå‰ï¼ŒCore Data会ä¿å˜ä»»ä½•å¯¹å…¶çš„更改
- (void)applicationWillTerminate:(UIApplication *)application {
NSError *error;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Handle the error.
}
}
}
//在得到点击事件åŽï¼Œä¿å˜æ›´æ”¹
- (IBAction)saveAction:(id)sender {
NSError *error;
if (![[self managedObjectContext] save:&error]) {
// Handle error
}
}
//自定义的managedObjectContextçš„getter, 它其实是真æ£åœ¨ä½¿ç”¨çš„时候的被æ“作对象
- (NSManagedObjectContext *) managedObjectContext {
//如果已ç»æœ‰è¿™ä¸ªå¯¹è±¡ï¼Œå°±ç›´æŽ¥è¿”回,å¦åˆ™ç»§ç»
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
//这里å¯ä»¥çœ‹åˆ°ï¼Œâ€œå†…容管ç†å™¨â€å’Œâ€œæ•°æ®ä¸€è‡´æ€§å˜å‚¨å™¨â€çš„关系,
//managedObjectContext需è¦å¾—到这个“数æ®ä¸€è‡´æ€§å˜å‚¨å™¨â€
}
return managedObjectContext;
}
//自定义的CoreDataæ•°æ®æ¨¡æ¿çš„getter,数æ®æ¨¡æ¿å…¶å®žå°±æ˜¯ä¸€ä¸ªæ述实体与实体的关系
//,类似于关系型数æ®åº“的关系æ述文件
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
//从本地所有xcdatamodel文件得到这个CoreDataæ•°æ®æ¨¡æ¿
return managedObjectModel;
}
//自定义“数æ®ä¸€è‡´æ€§å˜å‚¨å™¨â€ persistentStoreCoordinatorçš„getter
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
//定义一个本地地å€åˆ°NSURL,用æ¥å˜å‚¨é‚£ä¸ªSQLite文件
NSURL *storeUrl = [NSURL fileURLWithPath:
[[self applicationDocumentsDirectory]
stringByAppendingPathComponent: @"Locations.sqlite"]];
NSError *error;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel: [self managedObjectModel]];
//从这里å¯ä»¥çœ‹å‡ºï¼Œå…¶å®žpersistentStoreCoordinator需è¦çš„ä¸è¿‡æ˜¯ä¸€ä¸ª
//å˜å‚¨æ•°æ®çš„ä½ç½®ï¼Œå®ƒæ˜¯è´Ÿè´£ç®¡ç†CoreData如何储å˜æ•°æ®çš„
if (![persistentStoreCoordinator
addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeUrl
options:nil
error:&error]) {
// Handle the error.
}
return persistentStoreCoordinator;
}
//返回该程åºçš„档案目录,用æ¥ç®€å•ä½¿ç”¨
- (NSString *)applicationDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
//我原æ¥çš„帖åä¸ä»‹ç»è¿‡ï¼Œæ–¹æ³•å…¶å®žæ˜¯ä¸€æ¨¡ä¸€æ ·çš„
}
- (void)dealloc {
//释放内å˜
[managedObjectContext release];
[managedObjectModel release];
[persistentStoreCoordinator release];
[navigationController release];
[window release];
[super dealloc];
}
@end
从上é¢çš„程åºä¸»ä»£ç†æ–‡ä»¶å¯ä»¥çœ‹å‡ºï¼ŒCoreData的简å•ä½¿ç”¨ä¸è¿‡æ˜¯é€šè¿‡ä¸‰ä¸ªç»„建。
NSManagedObjectModelæ¥æ述实体与实体的关系,也就是类似于表和表的关系。
NSManagedObjectContextæ¥å¾—到被储å˜å†…容的文件管ç†å™¨ï¼Œå¯¹æ•°æ®ä½œç›´æŽ¥æ“作
NSPersistentStoreCoordinatoræ¥ç®¡ç†æ•°æ®çš„储å˜ä½ç½®ï¼Œå‚¨å˜æ–¹æ³•(SQLite)
iPhone CoreData实ä¾<A†天极??å|ä¹ï¼2)
在我们分æžäº†ç¨‹åºä¸»ä»£ç†æ–‡ä»¶ï¼ˆAppDelegate)之åŽï¼Œæˆ‘们先æ¥çœ‹çœ‹ä¸€å¯¹è‡ªåŠ¨ç”Ÿæˆçš„文件Event.h/.m
@interface Event : NSManagedObject {}
@property (nonatomic, retain) NSDate *creationDate;
@property (nonatomic, retain) NSNumber *latitude;
@property (nonatomic, retain) NSNumber *longitude;
@end
#import "Event.h"
@implementation Event
@dynamic creationDate;
@dynamic latitude;
@dynamic longitude;
@end
从上é¢æˆ‘们能看出æ¥ï¼Œä¸€ä¸ªå®žä½“Event也就会被生æˆä¸€ä¸ªNSManagedObject(被管ç†å¯¹è±¡ï¼‰ï¼Œç„¶åŽä»»ä½•accessorå’Œgetter 都是被动æ€ç”Ÿæˆçš„,我们其实完全ä¸ç”¨æ“任何的心,åªéœ€è¦åœ¨xcdatamodel文件里é¢é…ç½®åŽï¼Œç‚¹å‡»æ·»åŠ æ–‡ä»¶ï¼Œæ·»åŠ NSManagedObject文件,就会看到自动感知的类对象,然åŽç”Ÿæˆå°±å¯ä»¥äº†ã€‚
下é¢æ˜¯æ ¹è§†å›¾æŽ§åˆ¶å™¨ï¼Œæ˜¯ä¸€ä¸ªåˆ—表视图(UITableViewController)
#import <CoreLocation/CoreLocation.h>
@interface RootViewController : UITableViewController <CLLocationManagerDelegate> {
//看到是UITableViewControllerçš„å类,由于需è¦ä½¿ç”¨Core Location,
//所以在åŽé¢å±¥è¡Œå…¶protocal
NSMutableArray *eventsArray;
NSManagedObjectContext *managedObjectContext;
//这个被管ç†å¯¹è±¡å†…容器就是我们真æ£å¯¹Core Dataæ•°æ®çš„æ“作对象
CLLocationManager *locationManager; //用æ¥å¾—到地ç†ä½ç½®çš„Core Location对象
UIBarButtonItem *addButton; //å³ä¸Šè§’çš„æ·»åŠ é”®
}
@property (nonatomic, retain) NSMutableArray *eventsArray;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) UIBarButtonItem *addButton;
- (void)addEvent;
@end
#import "RootViewController.h"
#import "LocationsAppDelegate.h"
#import "Event.h"
@implementation RootViewController
@synthesize eventsArray, managedObjectContext, addButton, locationManager;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Locations"; //è®¾ç½®åˆ—è¡¨è§†å›¾çš„æ ‡é¢˜
self.navigationItem.leftBarButtonItem = self.editButtonItem; //导航æ 左边的编辑按钮
addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addEvent)]; //åˆå§‹åŒ–æ·»åŠ æŒ‰é’®ï¼Œ
addButton.enabled = NO; //在Core Locationåˆå§‹åŒ–之å‰å°†å…¶å…³é—
self.navigationItem.rightBarButtonItem = addButton;
//æŠŠè¿™ä¸ªæ·»åŠ æŒ‰é’®æ·»åŠ åˆ°å¯¼èˆªæ å³ä¾§
// å¯åŠ¨CLocation
[[self locationManager] startUpdatingLocation];
//åˆå§‹åŒ–一个“获å–请求â€åˆ°æˆ‘们的实体“Eventâ€
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event"
inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
// 将时间以建立时间排åºï¼Œæœ€æ–°çš„在最上
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];
// 执行“获å–â€æ“作,得到一个“å¯å˜æ•°ç»„â€çš„æ‹·è´
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext
executeFetchRequest:request
error:&error] mutableCopy];
if (mutableFetchResults == nil) {
//如果结果为空,在这作错误å“应
}
// 将得到的本地数组赋值到本类的全局数组,然åŽæ¸…ç†æ— 用的对象
[self setEventsArray:mutableFetchResults];
[mutableFetchResults release];
[request release];
}
- (void)viewDidUnload {
// 当视图被å¸è½½åŽï¼Œå°†ä»¥ä¸‹æŒ‡é’ˆç½®ç©º
self.eventsArray = nil;
self.locationManager = nil;
self.addButton = nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// åªæœ‰ä¸€ä¸ªç« 节
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// 在数组里é¢æœ‰å¤šå°‘个对象,在列表视图就有多少行
return [eventsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ä¸€ä¸ªâ€œæ—¥æœŸæ ¼å¼åŒ–器â€ï¼ˆå‡‘åˆæ˜Žç™½å°±å¥½...)用æ¥ä»¥ç‰¹å®šçš„æ ¼å¼åˆ›å»ºå¾—到的日期
static NSDateFormatter *dateFormatter = nil;
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
}
static NSNumberFormatter *numberFormatter = nil;
if (numberFormatter == nil) {
numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:3];
}
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
UITableViewCellStyleSubtitle;
}
// 从数组ä¸å¾—到这个Event对象
Event *event = (Event *)[eventsArray objectAtIndex:indexPath.row];
cell.textLabel.text = [dateFormatter stringFromDate:[event creationDate]];
NSString *string = [NSString stringWithFormat:@"%@, %@",
[numberFormatter stringFromNumber:[event latitude]],
[numberFormatter stringFromNumber:[event longitude]]];
cell.detailTextLabel.text = string;
return cell;
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// åˆ é™¤è¢«æƒ¯ä¾‹å¯¹è±¡åœ¨ç´¢å¼•è·¯å¾„ï¼ˆindex path)
NSManagedObject *eventToDelete = [eventsArray objectAtIndex:indexPath.row];
[managedObjectContext deleteObject:eventToDelete];
// 更新数组和列表视图
[eventsArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
// æ交更改到Core Data
NSError *error;
if (![managedObjectContext save:&error]) {
// Handle the error.
}
}
}
- (void)addEvent {
//如果得ä¸åˆ°ä½ç½®ï¼Œå°±è¿”回.
CLLocation *location = [locationManager location];
if (!location) {
return; }
//建立一个Event实体对象
Event *event = (Event *)[NSEntityDescription
insertNewObjectForEntityForName:@"Event"
inManagedObjectContext:managedObjectContext];
//得到ç»çº¬åº¦ï¼Œç„¶åŽèµ‹å€¼åˆ°event对象去
CLLocationCoordinate2D coordinate = [location coordinate];
[event setLatitude:[NSNumber numberWithDouble:coordinate.latitude]];
[event setLongitude:[NSNumber numberWithDouble:coordinate.longitude]];
// 实例里é¢å¹¶æ²¡æœ‰ä½¿ç”¨CLçš„æ—¶é—´æ ‡ç¾ï¼Œå› 为在模拟器ä¸ä¼šæ˜¯ä¸€æ ·çš„
// [event setCreationDate:[location timestamp]];
[event setCreationDate:[NSDate date]];
//所以现在使用的是现在的时间,而ä¸æ˜¯å¾—到ä½ç½®çš„时候的时间
// ä¿å˜æ›´æ”¹
NSError *error;
if (![managedObjectContext save:&error]) {
// Handle the error.
}
// 将新Event放到最上é¢ï¼Œæ‰€ä»¥æ·»åŠ 到0çš„ä½ç½®
// 然åŽæ»šåŠ¨åˆ—表视图到最上é¢ï¼Œå¦‚果没有那么多的数æ®æ˜¯çœ‹ä¸å‡ºæ¥åŒºåˆ«çš„
[eventsArray insertObject:event atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
- (CLLocationManager *)locationManager {
//自定义的CLocationçš„getter,方便åˆå§‹åŒ–
if (locationManager != nil) {
return locationManager;
}
//åˆå§‹åŒ–CL对象,然åŽè®¾ç½®ç²¾å‡†åº¦ï¼Œç„¶åŽå°†ä»£ç†å¯¹è±¡è®¾ä¸ºæœ¬åœ°
locationManager = [[CLLocationManager alloc] init];
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager setDelegate:self];
return locationManager;
}
//CLocation的一个代ç†æ–¹æ³•ï¼Œå¦‚æžœæˆåŠŸå°±å¼€å¯å³ä¾§æ·»åŠ 按钮
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
addButton.enabled = YES;
}
//CLocation的一个代ç†æ–¹æ³•ï¼Œå¦‚果失败了就关é—(disable)å³ä¾§æ·»åŠ 按钮
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
addButton.enabled = NO;
}
- (void)dealloc {
//释放对象
[managedObjectContext release];
[eventsArray release];
[locationManager release];
[addButton release];
[super dealloc];
}
@end
从上é¢çš„æºä»£ç ,我们å¯ä»¥çœ‹å‡ºï¼Œ
1,在这里数æ®å¹¶ä¸æ˜¯æ¯æ¬¡éƒ½ç”±NSManagedContext对象得到,而是由一个数组得出。
2,数组是一个å¯å˜æ•°ç»„,由第一次载入的视图的时候从NSManagedContextä¸å¾—到
3,从NSManagedContext对象ä¸å¾—到数æ®éœ€è¦ä½¿ç”¨NSFetchRequestæ¥åˆå§‹åŒ–一个“获å–â€
4,æ¯æ¬¡èŽ·å¾—æ–°çš„æ•°æ®çš„时候,åŒæ—¶ä¿å˜åˆ°æ•°ç»„å’ŒNSManagedContextä¸ï¼Œæ·»åŠ åŽéœ€è¦å¯¹æ›´æ”¹è¿›è¡Œæ交
iPhone iPhoneå®~æ-¹SDK:å|,何ES ??è - ?? UINavigationBar的
我在写电å书 Tread 的时候 , 想实现自动éšè— UINavigationBar, 以达到 iPhone 上é¢æµè§ˆ Photo 的效果 . 当时 NavigationBar 在 UINavigationController 里é¢æ˜¯ private çš„ , æ— æ³•æŠŠå¥¹å˜æˆé€æ˜Ž . 最åŽåªå¥½æŠŠå®ƒ
  
在写电å书Tread的时候,想实现自动éšè—UINavigationBar,以达到iPhone上é¢æµè§ˆPhoto的效果.
当时NavigationBar在UINavigationController里é¢æ˜¯privateçš„,æ— æ³•æŠŠå¥¹å˜æˆé€æ˜Ž.
最åŽåªå¥½æŠŠå®ƒ"推出"view 外é¢,æ¥è¾¾åˆ°éšè—的目的.
在Beta1一直到Beta4,æ˜¯è¿™æ ·å†™çš„
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
CGRect viewFrame = self.navigationController.view.bounds;
//CGPoint touchPoint1 = self.navigationController.view.center;
if (viewIsUp) {
viewFrame.origin.y -= 25;
viewFrame.size.height -= 50;
viewIsUp = NO;
} else {
viewFrame.origin.y += 25;
viewFrame.size.height += 50;
viewIsUp = YES;
}
self.navigationController.view.bounds =viewFrame;
[UIView commitAnimations];
刚å‘布的Beta5,这个功能已ç»åŠ 在API里é¢äº†
现在å˜æˆ
if (viewIsUp) {
[self.navigationController setNavigationBarHidden:NO animated:YES];
viewIsUp = NO;
} else {
[self.navigationController setNavigationBarHidden:YES animated:YES];
viewIsUp = YES;
}
iPhone 设备更改方向时滚动视图问题
#define SCROLLVIEW_CONTENT_HEIGHT_PORTRAIT 800
#define SCROLLVIEW_CONTENT_WIDTH_PORTRAIT 320
#define SCROLLVIEW_CONTENT_HEIGHT_LANDSCAPE 670
#define SCROLLVIEW_CONTENT_WIDTH_LANDSCAPE 480
........
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:)
name: UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:)
name: UIKeyboardDidHideNotification object:nil];
m_scrollView.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH_PORTRAIT, SCROLLVIEW_CONTENT_HEIGHT_PORTRAIT);
displayKeyboard = NO;
}
- (void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)keyboardDidShow: (NSNotification *)notif
{
if (displayKeyboard) {
return;
}
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
m_scrollView.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH_LANDSCAPE, 140);
}
else
{
m_scrollView.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH_PORTRAIT, 240);
}
CGRect textFieldRect = [m_activeField frame];
textFieldRect.origin.y += 10;
[m_scrollView scrollRectToVisible:textFieldRect animated:YES];
displayKeyboard = YES;
}
- (void) keyboardDidHide: (NSNotification *)notif
{
if (!displayKeyboard) {
return;
}
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
m_scrollView.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH_LANDSCAPE, 300);
m_scrollView.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH_LANDSCAPE, SCROLLVIEW_CONTENT_HEIGHT_LANDSCAPE);
}
else
{
m_scrollView.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH_PORTRAIT, 460);
m_scrollView.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH_PORTRAIT, SCROLLVIEW_CONTENT_HEIGHT_PORTRAIT);
}
displayKeyboard = NO;
}
iPhone present / dissmiss模态视图
//declare the protocol
@protocol ModalViewControllerDelegate<NSObject>
- (void) didFinishModalView: (ModalViewController*) aModalViewController;
@end
//declare a delegate property
@interface ModalViewController : UIViewController {
}
@property (nonatomic, assign) id<ModalViewControllerDelegate> delegate;
@end
//
[delegate didFinishModalView: self];
////////////////////
//make the view controller confort to the protocol
<ModalViewControllerDelegate>
//initiate modal view and present it
ModalViewController *myModalViewController = [[MyViewController alloc] initWithNibName: @"ModalViewController" bundle: [NSBundle mainBundle]];
myModalViewController.delegate = self;
[self presentModalViewController: myModalViewController animated: YES];
//dismiss the modal view
- (void) didFinishModalView: (ModalViewControllerDelegate*) aModalViewControllerDelegate {
[self dismissModalViewControllerAnimated: YES];
}
iPhone 快速枚举数组
NSArray *array = [NSArray arrayWithObjects:
@"One", @"Two", @"Three", @"Four", nil];
for (NSString *element in array) {
NSLog(@"element: %@", element);
}
iPhone 删除表视图行
[tableView setEditing:YES];
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
int chapter = [[[book bookmarks] objectAtIndex: indexPath.row] intValue];
[book removeBookmark: chapter];
[tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationFade];
}