OSDN Git Service

AlertView related refactoring.
authorHirami <tomohisa.hirami@nifty.com>
Wed, 11 Apr 2012 14:34:54 +0000 (23:34 +0900)
committerHirami <tomohisa.hirami@nifty.com>
Wed, 11 Apr 2012 14:34:54 +0000 (23:34 +0900)
NewFolderAlert and EditCancelAlert

iOS/Tombo/Tombo/EditCancelAlert.h
iOS/Tombo/Tombo/EditCancelAlert.m
iOS/Tombo/Tombo/MasterViewController.m
iOS/Tombo/Tombo/NewFolderAlert.h
iOS/Tombo/Tombo/NewFolderAlert.m

index c197799..8ac61f5 100644 (file)
@@ -1,4 +1,9 @@
 #import <UIKit/UIKit.h>
 
-@interface EditCancelAlert : UIAlertView
+@interface EditCancelAlert : UIAlertView <UIAlertViewDelegate>
+
+- (id)initWithDefault;
+
+- (BOOL)showAndWait;
+
 @end
index 8cb0468..1b94ac2 100644 (file)
@@ -1,4 +1,39 @@
 #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
index 21f278e..bc7fdf6 100644 (file)
@@ -8,29 +8,7 @@
 #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
index ff81409..668e6ab 100644 (file)
@@ -1,5 +1,10 @@
 #import <UIKit/UIKit.h>
 
-@interface NewFolderAlert : UIAlertView
+@interface NewFolderAlert : UIAlertView <UIAlertViewDelegate>
 
+@property (strong,nonatomic) NSString *folderName;
+
+- (id)initWithDefault;
+
+- (NSString *)showAndWait;
 @end
index 8994bdf..e6619b8 100644 (file)
@@ -1,5 +1,49 @@
 #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