Quote:
Originally Posted by BrianSlick
Post the new version of your code. Don't make me assume that you replaced everything correctly, because I won't do that.
|
Code:
- (void)dealloc {
[self.userName release];
[self.password release];
[super dealloc];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if (indexPath.section == 0) {
NSString *cellIdentifier = @"LoginCellIdentifier";
cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 25)];
leftLabel.backgroundColor = [UIColor clearColor];
leftLabel.tag = 1;
[cell.contentView addSubview:leftLabel];
[leftLabel release];
UITextField *valueTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 400, 35)];
valueTextField.tag = 2;
valueTextField.delegate = self;
[cell.contentView addSubview:valueTextField];
[valueTextField release];
}
if (indexPath.row == 0) { // User name
UILabel *lblText = (UILabel *)[cell.contentView viewWithTag:1];
lblText.text = @"Username: ";
UITextField *userNameField = (UITextField *)[cell.contentView viewWithTag:2];
userNameField.placeholder = @"Enter your username here";
}
else { // Pass word
UILabel *lblText = (UILabel *)[cell.contentView viewWithTag:1];
lblText.text = @"Password: ";
UITextField *passwordField = (UITextField *)[cell.contentView viewWithTag:2];
passwordField.placeholder = @"Enter your password here";
passwordField.secureTextEntry = YES;
}
}
return cell;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self getValueForTextField:textField]; // Get the value for current field holding the keyboard.
Database *db = [[Database alloc] initDatabase];
UserInfoRec *userInfoRec = (UserInfoRec*)[db getUserInfo:self.userName andPassword:self.password];
if (userInfoRec != nil) {
[[UserLogin sharedInstance] setCurrentUserInfo:userInfoRec];
[textField resignFirstResponder];
}
[db release];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
// Get value for text field just finished editing.
[self getValueForTextField:textField];
}
- (void)getValueForTextField:(UITextField *)textField {
UITableViewCell *cell = (UITableViewCell *)[[textField superview] superview];
UITableView *table = (UITableView *)[cell superview];
NSIndexPath *textFieldIndexPath = [table indexPathForCell:cell];
NSLog(@"Row %d just finished editing with the value %@",textFieldIndexPath.row,textField.text);
if (textFieldIndexPath.row == 0) {
if (self.userName != nil) {
[self.userName release];
}
self.userName = [[NSString alloc] initWithString:textField.text];
}
else {
if (self.password != nil) {
[self.password release];
}
self.password = [[NSString alloc] initWithString:textField.text];
}
}
As requested, I've posted all the code using username/password fields.
Since the app crashes in placeholder string and as you can see that I've assigned some placeholder string to textfield there so do you see anything wrong in that code in cellForRowAtIndexPath?
PS: I'm a C++ guy and comparatively new to objective-C.
And, also please notice the second line of crash log below, just before crash.. It has (
null) as the output !!! I didn't type in anything in both text field so output text should have been be same for both rows.
(complete crash log below)
[Session started at 2011-03-15 01:16:09 +0530.]
2011-03-15 01:16:14.632 MobileLearning[1287:207] Row 0 just finished editing with the value
2011-03-15 01:16:15.247 MobileLearning[1287:207] Row 1 just finished editing with the value (null)
2011-03-15 01:16:15.250 MobileLearning[1287:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithString:]: nil argument'
*** Call stack at first throw:
(
0 CoreFoundation 0x00e4bbe9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00fa05c2 objc_exception_throw + 47
2 CoreFoundation 0x00e04628 +[NSException raise:format:arguments:] + 136
3 CoreFoundation 0x00e0459a +[NSException raise:format:] + 58
4 Foundation 0x0004f1a8 -[NSPlaceholderString initWithString:] + 105
5 MobileLearning 0x00003773 -[AccountLoginViewController getValueForTextField:] + 562
6 MobileLearning 0x0000353b -[AccountLoginViewController textFieldDidEndEditing:] + 43
7 UIKit 0x0035a723 -[UITextField _resignFirstResponder] + 388
8 UIKit 0x0038e29a -[UIResponder resignFirstResponder] + 455
9 UIKit 0x0035622f -[UITextField resignFirstResponder] + 79
10 UIKit 0x0038e457 -[UIResponder becomeFirstResponder] + 280
11 UIKit 0x00550e09 -[UITextInteractionAssistant setFirstResponderIfNecessary] + 208
12 UIKit 0x00553a8a -[UITextInteractionAssistant oneFingerTap:] + 1676
13 UIKit 0x0054a9c7 -[UIGestureRecognizer _updateGestureWithEvent:] + 727
14 UIKit 0x005469d6 -[UIGestureRecognizer _delayedUpdateGesture] + 47
15 UIKit 0x0054cfa5 _UIGestureRecognizerUpdateObserver + 584
16 UIKit 0x0054d18a _UIGestureRecognizerUpdateGesturesFromSendEvent + 51
17 UIKit 0x002e86b4 -[UIWindow _sendGesturesForEvent:] + 1292
18 UIKit 0x002e3f87 -[UIWindow sendEvent:] + 105
19 UIKit 0x002c737a -[UIApplication sendEvent:] + 447
20 UIKit 0x002cc732 _UIApplicationHandleEvent + 7576
21 GraphicsServices 0x016eea36 PurpleEventCallback + 1550
22 CoreFoundation 0x00e2d064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FU NCTION__ + 52
23 CoreFoundation 0x00d8d6f7 __CFRunLoopDoSource1 + 215
24 CoreFoundation 0x00d8a983 __CFRunLoopRun + 979
25 CoreFoundation 0x00d8a240 CFRunLoopRunSpecific + 208
26 CoreFoundation 0x00d8a161 CFRunLoopRunInMode + 97
27 GraphicsServices 0x016ed268 GSEventRunModal + 217
28 GraphicsServices 0x016ed32d GSEventRun + 115
29 UIKit 0x002d042e UIApplicationMain + 1160
30 MobileLearning 0x00001bcc main + 102
31 MobileLearning 0x00001b5d start + 53
)
terminate called after throwing an instance of 'NSException'