When renaming title, file name is updated.
But update view is yet.
#import <UIKit/UIKit.h>
#import "FileItem.h"
+#import "Storage.h"
@interface DetailViewController : UIViewController <UISplitViewControllerDelegate>
@property (strong, nonatomic) FileItem *detailItem;
+@property (strong, nonatomic) Storage *storage;
@property (weak, nonatomic) IBOutlet UITextView *detailText;
@implementation DetailViewController
@synthesize detailItem = _detailItem;
+@synthesize storage;
@synthesize detailText = _detailText;
@synthesize masterPopoverController = _masterPopoverController;
// Release any retained subviews of the main view.
}
+- (void)viewWillDisappear:(BOOL)animated {
+ // Leaving detail view
+ NSString *note = self.detailText.text;
+ [storage save:note item: self.detailItem];
+
+ [super viewWillDisappear: animated];
+}
+
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
@class DetailViewController;
-@interface MasterViewController : UITableViewController
+@interface MasterViewController : UITableViewController <UINavigationControllerDelegate>
@property (strong, nonatomic) DetailViewController *detailViewController;
self.navigationItem.rightBarButtonItem = addButton;
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
+ self.navigationController.delegate = self;
+
imgFolder = nil;
imgDocument = nil;
if (!storage) {
} else {
[controller setDetailItem:item];
+ [controller setStorage: storage];
}
}
}
}
+#pragma mark UINavigationControllerDelegate
+
+/*
+ * Called when view is changed
+ */
+- (void)navigationController:(UINavigationController *)navigationController
+ willShowViewController:(UIViewController *)viewController
+ animated:(BOOL)animated {
+
+ // I want to catch when returning from detail view to master view.
+ // At current time, this means switch to master view.
+ // But if there are another subviews, it should to be add another condition.
+ if (![viewController isKindOfClass:[MasterViewController class]]) return;
+
+}
@end
Abstraction of file and directory.
*/
#import <Foundation/Foundation.h>
+#import "FileItem.h"
@interface Storage : NSObject
* Is current directory Top?
*/
-(BOOL)isTopDir;
+
+-(void)save:(NSString *)note item:(FileItem *)item;
@end
-(BOOL)isTopDir {
return [currentDirectory isEqualToString:@"/"];
}
+
+// save note
+-(void)save:(NSString *)note item:(FileItem *)item {
+ // Decide new title.
+ NSRange r;
+ r.location = 0;
+ r.length = 0;
+ NSRange titleRange = [note lineRangeForRange:r];
+ NSString *title = [note substringWithRange:titleRange];
+ if ([title characterAtIndex:(title.length - 1)] == '\n') {
+ title = [title substringToIndex:(title.length - 1)];
+ }
+ if (title.length == 0) {
+ title = @"New document";
+ }
+
+ // If title is changed, rename one.
+ NSString *path;
+ if (![title isEqualToString: item.name]) {
+ // Title is changed. Rename one.
+ NSString *toPath = [[item.path stringByDeletingLastPathComponent] stringByAppendingFormat:@"/%@.%@", title, [item.path pathExtension]];
+ NSError *error = nil;
+ path = toPath;
+ [fileManager moveItemAtPath:item.path toPath:toPath error:&error];
+ } else {
+ path = item.path;
+ }
+
+ // Save note.
+ NSError *error = nil;
+ [note writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
+}
+
@end