UIAlertView-Show red if data not entered - iphone

I have a save button, which on click shows an alert box.I want the user to enter some data in the box and if he/she does not enter any data and press ok I want to make the alert box text field red i.e indicating its mandatory.For the moment I am showing another alert box.Please help me,Thanks.
following is the code.
-(void) saveCalculaterData{
UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:REFERENCE_ID
message:#"\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
textField = [[UITextField alloc] initWithFrame:CGRectMake(12, 50, 260, 25)];
textField.tag=REFERENCE_FIELD;
[textField setBackgroundColor:[UIColor whiteColor]];
[textField setPlaceholder:ADD_REFERENCE_ID];
[prompt addSubview:textField];
[prompt show];
[textField becomeFirstResponder];
textField.delegate=self;
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex > 0) {
NSString *textValue = textField.text;
if ([textField.text length] <= 0)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"No Reference"
message: #"msg"
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil,nil];
[alert show];
}
else{
CalculatorData *calcData = [self getCalcData];
calcData.dataKey = textValue;
if([database saveCalculatorData:(CalculatorData *)calcData tableName:[database Table]]){
[Utils showAlert:RECORD_SAVED];
}
}
}
}

What you can do is just find the instance of the textField of the alertView with below code
UITextField *alertTextField = (UITextField*)[alertView viewWithTag:REFERENCE_FIELD];
And then just put the code to show it's color to red with this code
[alertTextField setBackgroundColor:[UIColor redColor]];
May this will solve your problem :)

Did you try this?
textField.textColor = [UIColor redColor];

Try this:
- (BOOL) alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
UITextField *textField1 = [alertView textFieldAtIndex:0];
[textField1 setDelegate:self];
NSString *inputText = [[alertView textFieldAtIndex:0]text];
if ([inputText length] == 7) // If it's 7 chars long, enable the button.
{
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:#"0123456789"] invertedSet]; // Only allow numbers
if ([inputText rangeOfCharacterFromSet:set].location != NSNotFound) { //Not a Number?
//show alert view here
textField1.backgroundColor = [UIColor redColor];
alertView.message = #"Only enter numbers please!";
return NO;
}
alertView.message = #"Fill in your number:";
return YES;
} else {
alertView.message = #"Fill in your number:";
textField1.backgroundColor = [UIColor clearColor];
return NO;
}
}
You can adapt it to your needs

Related

How do you format more than two buttons inside an UIAlertView with the UIAlertStylePlainTextInput style?

I've added a UIAlertView in my application that grabs user input but I'm unsure as to how I can add a third button. Ideally the three buttons would be across the alert horizontally or two would be above the "cancel" button. The code snippet below is what I'm using to add the UIAlertView.
- (IBAction)initiateSave{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Archive"
message:#"Enter a name to save this as:"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Save Session",#"Save",nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeDefault;
alertTextField.placeholder = #"eg. My awesome file...";
alert.tag = 1;
[alert show];
[alert release];
self.name = [[alert textFieldAtIndex:0]text];
}
Apple really doesn't want you messing with UIAlertView. If the way it naturally formats itself doesn't meet your needs, consider putting up a custom presented ("modal") view instead.
This can definitely be achieved, first of all you'll need to set up something like this.
// Create Alert
UIAlertView* av = [UIAlertView new];
av.title = #"Find";
// Add Buttons
[av addButtonWithTitle:#"Cancel"];
[av addButtonWithTitle:#"Find & Bring"];
[av addButtonWithTitle:#"Find & Go"];
[av addButtonWithTitle:#"Go to Next"];
// Make Space for Text View
av.message = #"\n";
// Have Alert View create its view heirarchy, set its frame and begin bounce animation
[av show];
// Adjust the frame
CGRect frame = av.frame;
frame.origin.y -= 100.0f;
av.frame = frame;
// Add Text Field
UITextField* text = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
text.borderStyle = UITextBorderStyleRoundedRect;
[av addSubview:text];
[text becomeFirstResponder];
QUOTED FROM...
https://stackoverflow.com/a/412618/716216
Then you'll want to animate moving the UIAlertView up when the keyboard is called up... something like this...
-(void)keyboardWillShow: (id) notification {
if(showAlert==YES)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[createNewAlert setTransform:CGAffineTransformMakeTranslation(0,-60)];
[createNewAlert show];
[UIView commitAnimations];
}
}
-(void)keyboardWillHide: (id) notification {
if(showAlert==YES)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[createNewAlert setTransform:CGAffineTransformMakeTranslation(0,+60)];
[UIView commitAnimations];
}
}
QUOTED FROM... https://stackoverflow.com/a/3844956/716216
Of course you can find additional info on custom UIAlertViews in the following Apple sample code!
https://developer.apple.com/library/ios/#samplecode/UICatalog/Introduction/Intro.html
Good Luck!!!!
It's works fine to me. After showing lot of thread finally i found solution
UIAlertView* getWebUrl = [[UIAlertView alloc] initWithTitle:#"Enter URL"
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Save",
#"Save Url", nil];
getWebUrl.transform=CGAffineTransformMakeScale(1.0, 0.75);
getWebUrl.alertViewStyle=UIAlertViewStylePlainTextInput;
[getWebUrl show];
and align buttons and textfield after present alertview
-(void)willPresentAlertView:(UIAlertView *)alertView {
for (UIView *view in alertView.subviews) {
if ([view isKindOfClass:[UITextField class]]||
[view isKindOfClass:[UIButton class]] || view.frame.size.height==31) {
CGRect rect=view.frame;
rect.origin.y += 65;
view.frame = rect;
}
}
}
I don't think you can manually resize a UIAlertView, but a trick I use for including a UIActivityIndicatorView is to use "/n" in the message string to make the "message" larger (one extra line per each "/n"), therefore making enough space for everything else.
This is somewhat tricky, Here is the solution for this.You need to use \n in the message string to increase the height of the UIAlertView.
- (IBAction)initiateSave{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Archive"
message:#"\n\n\n\n\n" // Trick to increase the height
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Save Session",#"Save",nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
After increasing the height of Alert, now you can add a label to set the message in the label : "Enter a name to save this as:" at appropriate position.
[alert addSubView: messageLbl];
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeDefault;
alertTextField.placeholder = #"eg. My awesome file...";
alert.tag = 1;
[alert show];
[alert release];
self.name = [[alert textFieldAtIndex:0]text];
}
Here, a bit late but should do the trick. (Work for me).
Create UIAlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"TITLE"
message:#"BODY"
delegate:self
cancelButtonTitle:#"CANCEL"
otherButtonTitles:#"OK",#"SEARCH",nil];
alert.tag = kAlertTag;
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeDefault;
alertTextField.placeholder = #"ENTER STH";
[alert show];
[alert release];
And implement delegate method.
-(void)willPresentAlertView:(UIAlertView *)alertView {
if (alertView.tag == kAlertTag) {
[alertView setFrame:CGRectMake(17, 30, 286, 188)];
NSArray *subviewArray = [alertView subviews];
UILabel *messageLB = (UILabel *)[subviewArray objectAtIndex:2];
[messageLB setFrame:CGRectMake(10, 46, 260, 20)];
UIButton *cancelBT = (UIButton *)[subviewArray objectAtIndex:3];
[cancelBT setFrame:CGRectMake(10, 130, 100, 42)];
UIButton *okBT = (UIButton *)[subviewArray objectAtIndex:4];
[okBT setFrame:CGRectMake(194, 130, 80, 42)];
UIButton *searchBT = (UIButton *)[subviewArray objectAtIndex:5];
[searchBT setFrame:CGRectMake(112, 130, 80, 42)];
UITextField *plateTF = (UITextField *)[subviewArray objectAtIndex:6];
[plateTF setFrame:CGRectMake(10, 80, 266, 50)];
UITextField *placeTF = (UITextField *)[subviewArray objectAtIndex:7];
[placeTF setFrame:CGRectMake(15, 70, 256, 50)];
}
}
FYI [subviewArray objectAtIndex:1] is for title of alertview.

Call a method from a UIAlertView Button

i created an Alert View that ask for some password but i´m wondering how to call a method only when the "Continue" button was clicked?
This is my code:
-(IBAction)setUserAlert:(id)sender{
UIAlertView *setPassword = [[UIAlertView alloc] initWithTitle:#"Insert Password" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Continue", nil];
//The password isn´t visible.
setPassword.alertViewStyle = UIAlertViewStyleSecureTextInput;
[setPassword show];
// Call the method that i want to call only after you click "Continue".
[self displayMessage:(UIButton *)sender];
}
}
// Enable the button only when the length is >= 6 and password match
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView{
//Defining the password
NSString *password = [[NSString alloc]initWithFormat:#"12345678"];
UITextField *setPassword = [alertView textFieldAtIndex:0];
if ([setPassword.text length] >= 6 && [setPassword.text isEqualToString:password]){
return YES;
}
return NO;
}
// Action i want to call, after you click "Continue"
-(IBAction)displayMessage:(UIButton *)sender{
if ([sender.currentTitle isEqualToString:#"YES"]){
_user.text = [[NSString alloc]initWithFormat:#"Hola Mony"];
_imageUser.hidden = YES;
_goodUser.hidden = NO;
backgound.hidden = YES;
yesButton.hidden = YES;
noButton.hidden = YES;
_userLabel.hidden =YES;
}else{
_user.text = [[NSString alloc]initWithFormat:#"You can´t play"];
_goodUser.hidden = YES;
_imageUser.hidden = NO;
yesButton.hidden = YES;
noButton.hidden = YES;
_userLabel.hidden =YES;
}
}
I think to call "displayMessage" inside this one but i don´t know how to do it or if it´s correct.
-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 1){
// call displayMessage, sends and error.
}
}
If the delegate is self, add the alertView:clickedButtonAtIndex: method and determine if "Continue" was pressed by the index.

Warning on Alertview [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
warning: 'UIAlertView' may not respond to '-addTextFieldWithValue:label:'
I am using below code for put textfield in AlertView, it gives a warning like "UIAlertView may not respond - addTextFieldWithValue: label:" & "UIAlertView may not respond - textFieldAtIndex"...
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Email" message:#""
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Submit", nil];
[alert addTextFieldWithValue:#"" label:#"Email"];
// Username
textfieldName = [alert textFieldAtIndex:0];
textfieldName.keyboardType = UIKeyboardTypeEmailAddress;
textfieldName.keyboardAppearance = UIKeyboardAppearanceAlert;
textfieldName.autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
please help!
there is no method "addTextFieldWithValue" for uialertview
if you need to modify your alert view have a look at my blog here - http://www.makebetterthings.com/blogs/iphone/add-uitextfield-in-uialertview/
These is no such methods like addTextFieldWithValue in UIAlertView.
[alert addTextFieldWithValue:#"" label:#"Email"]; is a private api. My app got rejected because of using this.
Use this
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:#"Twitter"
message:#"\n\n\n\n"
delegate:self
cancelButtonTitle:#""
otherButtonTitles:#"", nil];
myAlert.frame = CGRectMake( 0, 0, 300, 260);
textfieldName = [[UITextField alloc] initWithFrame:CGRectMake(13, 95, 260, 25.0)];
[textfieldName setBackgroundColor:[UIColor whiteColor]];
textfieldName.placeholder = #"xyz";
textfieldName.keyboardType = UIKeyboardTypeAlphabet;
textfieldName.keyboardAppearance = UIKeyboardAppearanceAlert;
textfieldName.autocorrectionType = UITextAutocorrectionTypeNo;
textfieldName.delegate = self;
textfieldName.tag = 1;
[myAlert addSubview:textfieldName];
**I use in my project < and use and implement
use in .h file**
#protocol InputAlertViewDelegate
-(void)handleEnteredValue:(NSString*)_value;
-(void)handleCancellation;
#end
#interface InputAlertView : UIAlertView <UIAlertViewDelegate> {
UITextField *textField;
id<InputAlertViewDelegate> caller;
} #property(nonatomic, retain) UITextField *textField;
#property(nonatomic, retain) id<InputAlertViewDelegate> caller;
-(NSString*) theText;
-(void) prepare;
-(void) makePassword;
-(void) makeTelephone;
+(InputAlertView*) inputAlertViewWithTitle:(NSString*)_title initialFieldText:(NSString*)_text caller:(id<InputAlertViewDelegate>)_caller;
**use in .m file**
+(InputAlertView*) inputAlertViewWithTitle:(NSString*)_title initialFieldText:(NSString*)_text
caller:(id<InputAlertViewDelegate>)_caller
{
InputAlertView *_alert =
[[[self alloc] initWithTitle:_title message:#"\n" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil] autorelease];
_alert.delegate = _alert;
_alert.caller = _caller;
[_alert prepare];
_alert.textField.text = _text;
return _alert;
}
-(void)prepare
{
self.textField = [[[UITextField alloc] initWithFrame:CGRectMake(12.0, 45, 260.0, 30.0)] autorelease];
[textField setBackgroundColor:[UIColor clearColor]];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
[self addSubview:textField];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 130.0);
[self setTransform:myTransform];
-(void) show{
[textField becomeFirstResponder];
[super show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 1){ [self.caller handleCancellation];
} else{
[self.caller handleEnteredValue:[self theText]];
}
}

how to call a function as user click on search in alert view iphone

//---------Searchoing----- //
- (IBAction) Search_hadit
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Search"
message:#" "
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Search",
nil];
CGRect frame = CGRectMake(14, 35, 255, 23);
myTextField = [[UITextField alloc] initWithFrame:frame];
myTextField.borderStyle = UITextBorderStyleBezel;
myTextField.textColor = [UIColor blackColor];
myTextField.textAlignment = UITextAlignmentCenter;
myTextField.font = [UIFont systemFontOfSize:14.0];
myTextField.placeholder = #"Enter Query";
myTextField.backgroundColor = [UIColor whiteColor];
myTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
myTextField.keyboardType = UIKeyboardTypeDefault;// use the default type input method (entire keyboard)
myTextField.returnKeyType = UIReturnKeyDone;
myTextField.delegate = self;
myTextField.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
[alert addSubview:myTextField];
[alert show];
[alert release];
}
When I click search button in my app alert view show...
I want when user click search button I get the text which he enter and send this data to another function on as he click search button where I can perform search. Any idea?
I guess (acording to the title) you'll need an UIAlertViewDelegate or at least the function:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString* buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle equalToString:#"Search"]) {
[do something];
}
}
But the text in your description describes a different problem than your title. I can't provide a solution to that, though.

UIAlertView - retrieve textfield value from textfield added via code

Here is the code I have to create an UIAlertView with a textbox.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter A Username Here" message:#"this gets covered!"
delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"OK!", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
[alert setTransform:myTransform];
alert.tag = kAlertSaveScore;
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
[alert release];
[myTextField release];
My question is, how do I get the value from the textfield in:
- (void) alertView:(UIAlertView *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
}
I know I can get the standard stuff for the alertview such as actionSheet.tag and such, but how would I get the above created textfield?
#interface MyClass {
UITextField *alertTextField;
}
#end
And instead of declaring it locally, just use that.
//...
alertTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
//...
- (void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *text = alertTextField.text;
alertTextField = nil;
}
Just give it a tag, and find it using the tag later. So, using your code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter A Username Here" message:#"this gets covered!"
delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"OK!", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
[alert setTransform:myTransform];
alert.tag = kAlertSaveScore;
// Give the text field some unique tag
[myTextField setTag:10250];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
[alert release];
[myTextField release];
Then, in the call-back, wherever that happens to be and without having to worry about memory management or state management of the text field:
- (void) alertView:(UIAlertView *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Get the field you added to the alert view earlier (you should also
// probably validate that this field is there and that it is a UITextField but...)
UITextField* myField = (UITextField*)[actionSheet viewWithTag:10250];
NSLog(#"Entered text: %#", [myField text]);
}