Adding an object to NSMutableArray from UITextField - iphone

With this code, I get this error:
'* -[NSMutableArray insertObject:atIndex:]: attempt to insert nil
object at 0'
categories=[[NSMutableArray alloc] init ];
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:#"Category name"];
[dialog setMessage:#" "];
[dialog addButtonWithTitle:#"Cancel"];
[dialog addButtonWithTitle:#"OK"];
nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
[dialog show];
[dialog release];
[categories addObject:[nameField text]];
[nameField release];
[categoryTable reloadData];
When I run this in simulator, I get a crash before the alert view even pops up. Any ideas?

An exception will be raised in an NSArray if you try to add a nil object. Check for the condition first:
if ([nameField text]) [categories addObject:[nameField text]];
EDIT:
Also from your code, you need to implement the UIAlertViewDelegate protocol and attempt to add your object to your array there. For example:
- (void) showDialog {
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:#"Category name"];
[dialog setMessage:#" "];
[dialog addButtonWithTitle:#"Cancel"];
[dialog addButtonWithTitle:#"OK"];
nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
[dialog show];
[dialog release];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if ([nameField text]) [categories addObject:[nameField text]];
[categoryTable reloadData];
}
Assumes nameField and categories are iVars and you will need to release them when they are no longer needed. You should also check which button was pressed in the delegate method. HTH Dave

Related

how to access the secureTextEntry for further use?

Hi I'm using the below code which includes the alertView containing UserID and Password field.
these UserID and Password are text field, i made password field as secure, but when i later want to access the data entered in password field, its showing no value in password field..
My code is
self.loginPassword = [[ UIAlertView alloc] initWithTitle:#"Please Login" message:#"Enter Valid UserID and Password\n\n\n\n" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Cancel", nil];
//[self.loginPassword setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
self.userIDText = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 80.0, 260.0, 25.0)];
self.userIDText.text=#"";
[self.userIDText setBackgroundColor:[UIColor whiteColor]];
[self.userIDText setKeyboardAppearance:UIKeyboardAppearanceAlert];
[self.userIDText setAutocorrectionType:UITextAutocorrectionTypeNo];
[self.userIDText setTextAlignment:UITextAlignmentLeft];
self.passwordField = [[UITextField alloc] initWithFrame:CGRectMake(12.0,115.0, 260.0, 25.0)];
self.passwordField.text=#"";
[self.passwordField setBackgroundColor:[UIColor whiteColor]];
[self.passwordField setKeyboardAppearance:UIKeyboardAppearanceAlert];
[self.passwordField setAutocorrectionType:UITextAutocorrectionTypeNo];
[self.passwordField setTextAlignment:UITextAlignmentLeft];
[self.loginPassword addSubview:self.userIDText];
[self.loginPassword addSubview:self.passwordField];
self.passwordField.secureTextEntry = YES;
[self.loginPassword setTag:2];
[self.loginPassword show];
[self.loginPassword release];
please suggest me some solution..
Create and In-built UIAlertView
UIAlertView *alert = [[UIAlertView alloc] init];
// Define Alertview Type to Login&Password
[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
// Set Delegate to access Input Value
[alert setDelegate:self];
// show it
[alert show];
set Delegate in you interface (.h file) UIAlertViewDelegate
Implement This delegate method in your implementation (.m file)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Access your Username & Password from alertview and store them for further use.
NSString *username = [[alertView textFieldAtIndex:0] text];
NSString *password = [[alertView textFieldAtIndex:1] text];
NSLog(#"Username : %# , Password : %#",username, password);
}
This may help you.Just take a look at the code.
https://github.com/josecastillo/EGOTextFieldAlertView

UitextFields in UIAlertView selecting iOS

I have three UITextfields in UIAlertView, while tap on one UITextfield and try to tap on other its not selecting and creating a problem, also problem of resigning first responder, is it not good choice of using UITextfield in UIAlertView
- (IBAction)heightMethod:(id)sender
{
self.centimeterTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
centimeterTextField.placeholder = #" Centimeters";
self.centimeterTextField.delegate=self;
self.centimeterTextField.tag=3;
[ self.centimeterTextField setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview: self.centimeterTextField];
self.ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(40, 80.0, 80, 25.0)]; ptextfield.placeholder = #" Feet";
self.ptextfield.delegate=self;
self.ptextfield.tag=4;
[self.ptextfield setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview:self.ptextfield];
self.ptextfieldInches = [[UITextField alloc] initWithFrame:CGRectMake(140, 80.0, 80, 25.0)]; ptextfieldInches.placeholder = #" Inches";
self.ptextfieldInches.delegate=self;
self.ptextfieldInches.tag=5;
[ptextfieldInches setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview:ptextfieldInches];
[self.centimeterTextField setKeyboardType:UIKeyboardTypeDecimalPad];
[self.ptextfieldInches setKeyboardType:UIKeyboardTypeDecimalPad];
[self.ptextfield setKeyboardType:UIKeyboardTypeDecimalPad];
self.alertHeight.tag=1;
[self.alertHeight show];
}
- (void) presentSheet {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Enter Information"
message:#"Specify the Name and URL" delegate:self cancelButtonTitle:#"Cancel"otherButtonTitles:#"OK", nil];
[alert addTextFieldWithValue:#"" label:#"Enter Name"];
[alert addTextFieldWithValue:#"http://" label:#"Enter URL"];
UITextField *tf = [alert textFieldAtIndex:0];
tf.clearButtonMode = UITextFieldViewModeWhileEditing;
tf.keyboardType = UIKeyboardTypeAlphabet;
tf.keyboardAppearance = UIKeyboardAppearanceAlert;
tf.autocapitalizationType = UITextAutocapitalizationTypeWords;
tf.autocorrectionType = UITextAutocorrectionTypeNo;
// URL field
tf = [alert textFieldAtIndex:1];
tf.clearButtonMode = UITextFieldViewModeWhileEditing; tf.keyboardType = UIKeyboardTypeURL;
tf.keyboardAppearance = UIKeyboardAppearanceAlert; tf.autocapitalizationType = UITextAutocapitalizationTypeNone; tf.autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
}
You can now create a UIAlertView with a style.
UIAlertView now has a property - alertViewStyle that you can set to one of the enum values
1. UIAlertViewStyleDefault
2. UIAlertViewStyleSecureTextInput
3. UIAlertViewStylePlainTextInput
4. UIAlertViewStyleLoginAndPasswordInput
Once you've created the alert view, you set it style and display it. After dismissing the alert, you can access the contents of the fields using the textFieldAtIndex property of the alert view.
or else you can use this one also.,
IAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:#"Enter Name"];
[dialog setMessage:#" "];
[dialog addButtonWithTitle:#"Cancel"];
[dialog addButtonWithTitle:#"OK"];
UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 70.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];
[nameField release];
Hope this information helps. Thank you.

Is it possible to display UIAlertView over another UIAlertView in iOS5

I am getting password in First alertView and if the password is incorrect, then another alertView shows that the password is incorrect. Now the first alertView disappears. I wanna to display the second alertView over the first alertView.
This is not So complicated work i just done like this:-
UITextField *forgetPass;
-(IBAction)Password{// here is a Action for login password
//[txtEmail resignFirstResponder];
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:#"Enter your password"];
[dialog setMessage:#" "];
[dialog addButtonWithTitle:#"Cancel"];
[dialog addButtonWithTitle:#"OK"];
dialog.tag= 1;
forgetPass = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
forgetPass.delegate = self;
[forgetPass setSecureTextEntry:YES];
forgetPass.placeholder = #"password";
forgetPass.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
[forgetPass setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:forgetPass];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 0.0);
[dialog setTransform: moveUp];
[dialog show];
}
-(void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alert.tag==1)
{
if (buttonIndex == 1) { // OK pushed
if([forgetPass.text length]>0 )
{
}
else {
UIAlertView *alert2 = [[UIAlertView alloc]initWithTitle:#"Oops..!" message:#"please enter your current password" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
UITextField *forgetPass2 = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
forgetPass2.delegate = self;
[forgetPass2 setSecureTextEntry:YES];
forgetPass2.placeholder = #"password";
forgetPass2.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
[forgetPass2 setBackgroundColor:[UIColor whiteColor]];
[alert2 addSubview:forgetPass2];
forgetPass2.hidden=YES;
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 0.0);
[alert2 setTransform: moveUp];
alert2.tag=2;
[alert2 show];
[alert2 release];
}
}
if (buttonIndex == 0)
{
}
}
else if(alert.tag==2)
{
if (buttonIndex == 0)
{
[self Password];
}
}
}
i just create a demo for you might be its helps for please download this:-
http://www.sendspace.com/file/zptg8l

Alert View show me warning

When I use alertView using below code, it shows me the warning
warning: Semantic Issue: Method '-addTextFieldWithValue:label:' not found (return type defaults to 'id')
Here is the code:
UIAlertView *alSave=[[UIAlertView alloc]initWithTitle:#"Save as" message:#"Title the note and click Save" delegate:self cancelButtonTitle:#"save" otherButtonTitles:#"cancel", nil];
NSArray *arr=[noteObj.noteTitle componentsSeparatedByString:#" - "];
app.longClickId = [noteObj.noteId integerValue];
[alSave addTextFieldWithValue:[NSString stringWithFormat:#"%#",[arr objectAtIndex:0]] label:#"Note Name"];
// show me warning at this place
textField = [alSave textFieldAtIndex:0];
textField.keyboardType = UIKeyboardTypeAlphabet;
textField.keyboardAppearance = UIKeyboardAppearanceAlert;
textField.autocorrectionType = UITextAutocorrectionTypeNo; // correction automatically
[alSave show];
if (app.NotePopOver!= nil) {
[app.NotePopOver dismissPopoverAnimated:YES];
}
[alSave release];
If you use a private method (of which addTextFieldWithValue: is one), then Apple will most likely reject your app. You can achieve the same result with the following snippet, courtesy of this answer which credits a no longer working link:
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Your title here!" message:#"this gets covered" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView addSubview:myTextField];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 130.0);
[myAlertView setTransform:myTransform];
[myAlertView show];
[myAlertView release];
That method is undocumented. You will have to create your own text field and then add it to the alert view.

Multiple keyboards popping up on iPhone alert dialog?

I am setting up an alert with a text field in it so players can enter their name for a high score. The game is oriented in landscape mode, but when I call to show the alert, the alert pops up in portrait mode while two keyboards are displayed, one in landscape and one(that's the size of the landscape one) in portrait mode. Here's the code I'm using to setup the alert dialog:
UIAlertView* dialog = [[[UIAlertView alloc] init] retain];
[dialog setDelegate:self];
[dialog setTitle:#"Enter Name"];
[dialog setMessage:#" "];
[dialog addButtonWithTitle:#"Cancel"];
[dialog addButtonWithTitle:#"OK"];
[dialog addTextFieldWithValue:#"name" label:#""];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 100.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];
How do I make the alert show in landscape orientation and prevent it from showing two keyboards?
Thanks,
Ben
For those who may care, here's a working solution, which works properly in landscape mode:
UIAlertView* dialog = [[[UIAlertView alloc] init] retain];
[dialog setDelegate:self];
[dialog setTitle:#"Enter Name"];
[dialog setMessage:#" "];
[dialog addButtonWithTitle:#"Cancel"];
[dialog addButtonWithTitle:#"OK"];
nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 100.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];
[nameField release];
Make sure you've created UITextField * nameField; in your .h file, then you can get at the text the user typed in by doing:
inputText = [nameField text];
in the - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex method.