#import "EditCancelAlert.h"
-@implementation EditCancelAlert
+@implementation EditCancelAlert {
+ BOOL finished;
+ BOOL isOK;
+}
+
+- (id)initWithDefault {
+ return [self initWithTitle:@"Confirm"
+ message:@"Note is modified. Are you sure to discard changes?" delegate:self
+ cancelButtonTitle:@"Cancel"
+ otherButtonTitles:@"OK", nil];
+}
+
+- (BOOL)showAndWait {
+ [self show];
+ finished = NO;
+
+ // Wait in runloop till a button is clicked.
+ while (!finished) {
+ [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
+ beforeDate:[NSDate dateWithTimeIntervalSinceNow:300]];
+ }
+ return isOK;
+}
+
+- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
+ finished = YES;
+
+ if (buttonIndex == 0) {
+ // Cancel
+ isOK = NO;
+ } else if (buttonIndex == 1) {
+ isOK = YES;
+ }
+}
+
+
@end
#import "FileItem.h"
#import "PasswordManager.h"
-@interface BackgroundView : UIView {
-}
-@end
-
-@implementation BackgroundView
-- (id)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- self.backgroundColor = [UIColor clearColor];
- }
- return self;
-}
-
-- (void)drawRect:(CGRect)rect {
- UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:5.0];
- [path moveToPoint:CGPointMake(0, self.bounds.size.height/2.0)];
- [path addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height/2.0)];
- [[UIColor whiteColor] set];
- [path fill];
-}
-@end
-
-@interface MasterViewController () <UIAlertViewDelegate, UITableViewDelegate, UISplitViewControllerDelegate> {
+@interface MasterViewController () <UITableViewDelegate, UISplitViewControllerDelegate> {
NSMutableArray *_objects;
Storage *storage;
PasswordManager *passwordManager;
}
- (void)createNewFolder:(id)sender {
- NewFolderAlert *alert = [[NewFolderAlert alloc] initWithTitle:@"Folder name:"
- message:@"\n"
- delegate:self
- cancelButtonTitle:@"Cancel"
- otherButtonTitles:@"Done", nil];
- BackgroundView *back = [[BackgroundView alloc] initWithFrame:CGRectMake(20.0, 43.0, 245.0, 25.0)];
- [alert addSubview:back];
- UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
- [alert addSubview:textField];
-
- [alert show];
-// [textField becomeFirstResponder];
+ NewFolderAlert *alert = [[NewFolderAlert alloc] initWithDefault];
+ NSString *folderName = [alert showAndWait];
+ if (folderName) {
+ FileItem *item = [storage newFolder:folderName];
+ [self insertItem:item];
+ }
}
#pragma mark - Item operations
- (void)editViewControllerDidCancel:(EditViewController *)controller {
if (controller.isModify) {
- UIAlertView *alert = [[EditCancelAlert alloc] initWithTitle:@"Confirm"
- message:@"Note is modified. Are you sure to discard changes?" delegate:self
- cancelButtonTitle:@"OK"
- otherButtonTitles:@"Cancel", nil];
- [alert show];
+ EditCancelAlert *cancel = [[EditCancelAlert alloc] initWithDefault];
+ if ([cancel showAndWait]) {
+ [self dismissModalViewControllerAnimated:YES];
+ }
} else {
[self dismissModalViewControllerAnimated:YES];
}
[self insertItem:newItem];
}
-#pragma mark - AlertViewDelegate
-
--(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
- if ([alertView isKindOfClass:[EditCancelAlert class]]) {
- if (buttonIndex == 0) {
- [self dismissModalViewControllerAnimated:YES];
- }
- } else if ([alertView isKindOfClass:[NewFolderAlert class]]) {
- if (buttonIndex == 1) {
- //
- UITextField *field = [alertView.subviews lastObject];
- NSString *folderName = [field.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
- if (folderName.length > 0) {
- FileItem *item = [storage newFolder:folderName];
- [self insertItem:item];
- }
- }
- }
-}
@end
#import "NewFolderAlert.h"
-@implementation NewFolderAlert
+@implementation NewFolderAlert {
+ BOOL finished;
+}
+
+@synthesize folderName=_folderName;
+
+- (id)initWithDefault {
+ return [self initWithTitle:@"New folder"
+ message:@"Input folder name"
+ delegate:self
+ cancelButtonTitle:@"Cancel"
+ otherButtonTitles:@"Done", nil];
+}
+
+- (NSString *)showAndWait {
+ self.alertViewStyle = UIAlertViewStylePlainTextInput;
+ [self show];
+ finished = NO;
+
+ // Wait in runloop till a button is clicked.
+ while (!finished) {
+ [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
+ beforeDate:[NSDate dateWithTimeIntervalSinceNow:300]];
+ }
+ return self.folderName;
+}
+
+- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
+ finished = YES;
+
+ UITextField *field = [self textFieldAtIndex:0];
+ if (buttonIndex == 0) {
+ // Cancel
+ self.folderName = nil;
+ } else if (buttonIndex == 1) {
+ // OK
+ NSString *fname = [field.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
+ if (fname.length == 0) {
+ self.folderName = nil;
+ } else {
+ self.folderName = fname;
+ }
+ }
+ [field resignFirstResponder];
+}
@end