-
Notifications
You must be signed in to change notification settings - Fork 4
/
newNoteViewController.m
181 lines (126 loc) · 4.64 KB
/
newNoteViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
// newNoteViewController.m
// Notes2
//
// Created by Vivek Pandya on 17/10/13.
// Copyright (c) 2013 Vivek Pandya. All rights reserved.
//
#import "AppDelegate.h"
#import "newNoteViewController.h"
#import "Notes+Create.h"
@interface newNoteViewController ()
@end
@implementation newNoteViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.currentNoteID = nil;
AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
self.managedObjectContext = delegate.managedObjectContext;
// [self getUIManagedObj];
// Do any additional setup after loading the view.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChanged:)
name:UITextViewTextDidChangeNotification
object:self.noteTextView];
CGRect accessFrame = CGRectMake(0.0, 0.0, 40.0, 30.0);
UIView *inputAccessoryView = [[UIView alloc] initWithFrame:accessFrame];
UIButton *compButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
compButton.frame = CGRectMake(0,0, 40, 30);
[compButton setBackgroundColor:[UIColor blackColor]];
[compButton setTitle: @"Done" forState:UIControlStateNormal];
[compButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[compButton addTarget:self action:@selector(dismissKey) forControlEvents:UIControlEventTouchUpInside];
[inputAccessoryView addSubview:compButton];
self.noteTextView.inputAccessoryView = inputAccessoryView;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)donePressed:(id)sender {
[self.noteTextView resignFirstResponder];
NSUInteger len = self.noteTextView.text.length ;
int lenInt = (int)len;
// NSLog(@"len-->%d\n",lenInt);
if (lenInt != 0) {
if (lenInt < 15) {
self.noteTitle.title = self.noteTextView.text;
}
else{
NSString *noteTitle = [self.noteTextView.text substringToIndex:15];
self.noteTitle.title = noteTitle;
}
if (self.currentNoteID != nil) {
NSLog(self.currentNoteID);
}
else{
NSLog(@"else");
Notes *note = [Notes noteWithContent:self.noteTextView.text inManagedObjectContext:self.managedObjectContext];
self.currentNoteID = note.noteID;
}
}
else {
NSLog(@"No Problem here\n");
}
}
-(void)textFieldChanged:(NSNotification*)notification
{
UITextView *txt = (UITextView *)[notification object];
NSLog(@"inside notification");
if([txt.text length] == 0){
[self.navigationItem.rightBarButtonItem setEnabled:NO];
self.navigationItem.title = txt.text;
}
if ([txt.text length] >0 ) {
[self.navigationItem.rightBarButtonItem setEnabled:YES];
NSString *title;
if ([txt.text length]> 15) {
title = [txt.text substringToIndex:15];
}
else
{
title = txt.text;
}
self.navigationItem.title = title;
}
}
-(void)dismissKey{
[self.noteTextView resignFirstResponder];
}
/*-(void)getUIManagedObj
{
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"Demo Document"];
UIManagedDocument *mdocument = [[UIManagedDocument alloc]initWithFileURL:url];
if (![[NSFileManager defaultManager]fileExistsAtPath:[url path]]) {
[mdocument saveToURL:url
forSaveOperation:UIDocumentSaveForCreating
completionHandler:^(BOOL success) {
if (success) {
}
}];
self.managedObjectContext = mdocument.managedObjectContext;
}
else if (mdocument.documentState == UIDocumentStateClosed)
{
[mdocument openWithCompletionHandler:^(BOOL success) {
if(success)
{
}
}];
self.managedObjectContext = mdocument.managedObjectContext;
}
else{
self.managedObjectContext = mdocument.managedObjectContext;
}
}*/
@end