TOMBO source code
Revision | 3687471fafdd7ed316b193c3b5997c4101d47f97 (tree) |
---|---|
Time | 2012-04-11 23:34:54 |
Author | Hirami <tomohisa.hirami@nift...> |
Commiter | Hirami |
AlertView related refactoring.
NewFolderAlert and EditCancelAlert
@@ -1,4 +1,9 @@ | ||
1 | 1 | #import <UIKit/UIKit.h> |
2 | 2 | |
3 | -@interface EditCancelAlert : UIAlertView | |
3 | +@interface EditCancelAlert : UIAlertView <UIAlertViewDelegate> | |
4 | + | |
5 | +- (id)initWithDefault; | |
6 | + | |
7 | +- (BOOL)showAndWait; | |
8 | + | |
4 | 9 | @end |
@@ -1,4 +1,39 @@ | ||
1 | 1 | #import "EditCancelAlert.h" |
2 | 2 | |
3 | -@implementation EditCancelAlert | |
3 | +@implementation EditCancelAlert { | |
4 | + BOOL finished; | |
5 | + BOOL isOK; | |
6 | +} | |
7 | + | |
8 | +- (id)initWithDefault { | |
9 | + return [self initWithTitle:@"Confirm" | |
10 | + message:@"Note is modified. Are you sure to discard changes?" delegate:self | |
11 | + cancelButtonTitle:@"Cancel" | |
12 | + otherButtonTitles:@"OK", nil]; | |
13 | +} | |
14 | + | |
15 | +- (BOOL)showAndWait { | |
16 | + [self show]; | |
17 | + finished = NO; | |
18 | + | |
19 | + // Wait in runloop till a button is clicked. | |
20 | + while (!finished) { | |
21 | + [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode | |
22 | + beforeDate:[NSDate dateWithTimeIntervalSinceNow:300]]; | |
23 | + } | |
24 | + return isOK; | |
25 | +} | |
26 | + | |
27 | +- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { | |
28 | + finished = YES; | |
29 | + | |
30 | + if (buttonIndex == 0) { | |
31 | + // Cancel | |
32 | + isOK = NO; | |
33 | + } else if (buttonIndex == 1) { | |
34 | + isOK = YES; | |
35 | + } | |
36 | +} | |
37 | + | |
38 | + | |
4 | 39 | @end |
@@ -8,29 +8,7 @@ | ||
8 | 8 | #import "FileItem.h" |
9 | 9 | #import "PasswordManager.h" |
10 | 10 | |
11 | -@interface BackgroundView : UIView { | |
12 | -} | |
13 | -@end | |
14 | - | |
15 | -@implementation BackgroundView | |
16 | -- (id)initWithFrame:(CGRect)frame { | |
17 | - self = [super initWithFrame:frame]; | |
18 | - if (self) { | |
19 | - self.backgroundColor = [UIColor clearColor]; | |
20 | - } | |
21 | - return self; | |
22 | -} | |
23 | - | |
24 | -- (void)drawRect:(CGRect)rect { | |
25 | - UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:5.0]; | |
26 | - [path moveToPoint:CGPointMake(0, self.bounds.size.height/2.0)]; | |
27 | - [path addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height/2.0)]; | |
28 | - [[UIColor whiteColor] set]; | |
29 | - [path fill]; | |
30 | -} | |
31 | -@end | |
32 | - | |
33 | -@interface MasterViewController () <UIAlertViewDelegate, UITableViewDelegate, UISplitViewControllerDelegate> { | |
11 | +@interface MasterViewController () <UITableViewDelegate, UISplitViewControllerDelegate> { | |
34 | 12 | NSMutableArray *_objects; |
35 | 13 | Storage *storage; |
36 | 14 | PasswordManager *passwordManager; |
@@ -110,18 +88,12 @@ | ||
110 | 88 | } |
111 | 89 | |
112 | 90 | - (void)createNewFolder:(id)sender { |
113 | - NewFolderAlert *alert = [[NewFolderAlert alloc] initWithTitle:@"Folder name:" | |
114 | - message:@"\n" | |
115 | - delegate:self | |
116 | - cancelButtonTitle:@"Cancel" | |
117 | - otherButtonTitles:@"Done", nil]; | |
118 | - BackgroundView *back = [[BackgroundView alloc] initWithFrame:CGRectMake(20.0, 43.0, 245.0, 25.0)]; | |
119 | - [alert addSubview:back]; | |
120 | - UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)]; | |
121 | - [alert addSubview:textField]; | |
122 | - | |
123 | - [alert show]; | |
124 | -// [textField becomeFirstResponder]; | |
91 | + NewFolderAlert *alert = [[NewFolderAlert alloc] initWithDefault]; | |
92 | + NSString *folderName = [alert showAndWait]; | |
93 | + if (folderName) { | |
94 | + FileItem *item = [storage newFolder:folderName]; | |
95 | + [self insertItem:item]; | |
96 | + } | |
125 | 97 | } |
126 | 98 | #pragma mark - Item operations |
127 | 99 |
@@ -361,11 +333,10 @@ | ||
361 | 333 | |
362 | 334 | - (void)editViewControllerDidCancel:(EditViewController *)controller { |
363 | 335 | if (controller.isModify) { |
364 | - UIAlertView *alert = [[EditCancelAlert alloc] initWithTitle:@"Confirm" | |
365 | - message:@"Note is modified. Are you sure to discard changes?" delegate:self | |
366 | - cancelButtonTitle:@"OK" | |
367 | - otherButtonTitles:@"Cancel", nil]; | |
368 | - [alert show]; | |
336 | + EditCancelAlert *cancel = [[EditCancelAlert alloc] initWithDefault]; | |
337 | + if ([cancel showAndWait]) { | |
338 | + [self dismissModalViewControllerAnimated:YES]; | |
339 | + } | |
369 | 340 | } else { |
370 | 341 | [self dismissModalViewControllerAnimated:YES]; |
371 | 342 | } |
@@ -378,23 +349,4 @@ | ||
378 | 349 | [self insertItem:newItem]; |
379 | 350 | } |
380 | 351 | |
381 | -#pragma mark - AlertViewDelegate | |
382 | - | |
383 | --(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { | |
384 | - if ([alertView isKindOfClass:[EditCancelAlert class]]) { | |
385 | - if (buttonIndex == 0) { | |
386 | - [self dismissModalViewControllerAnimated:YES]; | |
387 | - } | |
388 | - } else if ([alertView isKindOfClass:[NewFolderAlert class]]) { | |
389 | - if (buttonIndex == 1) { | |
390 | - // | |
391 | - UITextField *field = [alertView.subviews lastObject]; | |
392 | - NSString *folderName = [field.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; | |
393 | - if (folderName.length > 0) { | |
394 | - FileItem *item = [storage newFolder:folderName]; | |
395 | - [self insertItem:item]; | |
396 | - } | |
397 | - } | |
398 | - } | |
399 | -} | |
400 | 352 | @end |
@@ -1,5 +1,10 @@ | ||
1 | 1 | #import <UIKit/UIKit.h> |
2 | 2 | |
3 | -@interface NewFolderAlert : UIAlertView | |
3 | +@interface NewFolderAlert : UIAlertView <UIAlertViewDelegate> | |
4 | 4 | |
5 | +@property (strong,nonatomic) NSString *folderName; | |
6 | + | |
7 | +- (id)initWithDefault; | |
8 | + | |
9 | +- (NSString *)showAndWait; | |
5 | 10 | @end |
@@ -1,5 +1,49 @@ | ||
1 | 1 | #import "NewFolderAlert.h" |
2 | 2 | |
3 | -@implementation NewFolderAlert | |
3 | +@implementation NewFolderAlert { | |
4 | + BOOL finished; | |
5 | +} | |
6 | + | |
7 | +@synthesize folderName=_folderName; | |
8 | + | |
9 | +- (id)initWithDefault { | |
10 | + return [self initWithTitle:@"New folder" | |
11 | + message:@"Input folder name" | |
12 | + delegate:self | |
13 | + cancelButtonTitle:@"Cancel" | |
14 | + otherButtonTitles:@"Done", nil]; | |
15 | +} | |
16 | + | |
17 | +- (NSString *)showAndWait { | |
18 | + self.alertViewStyle = UIAlertViewStylePlainTextInput; | |
19 | + [self show]; | |
20 | + finished = NO; | |
21 | + | |
22 | + // Wait in runloop till a button is clicked. | |
23 | + while (!finished) { | |
24 | + [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode | |
25 | + beforeDate:[NSDate dateWithTimeIntervalSinceNow:300]]; | |
26 | + } | |
27 | + return self.folderName; | |
28 | +} | |
29 | + | |
30 | +- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { | |
31 | + finished = YES; | |
32 | + | |
33 | + UITextField *field = [self textFieldAtIndex:0]; | |
34 | + if (buttonIndex == 0) { | |
35 | + // Cancel | |
36 | + self.folderName = nil; | |
37 | + } else if (buttonIndex == 1) { | |
38 | + // OK | |
39 | + NSString *fname = [field.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; | |
40 | + if (fname.length == 0) { | |
41 | + self.folderName = nil; | |
42 | + } else { | |
43 | + self.folderName = fname; | |
44 | + } | |
45 | + } | |
46 | + [field resignFirstResponder]; | |
47 | +} | |
4 | 48 | |
5 | 49 | @end |