I have a textField and a button. On the button I defined an alertview now I want what I write in textfield appear in alertview.
- (IBAction) btnClicked {
NSString *str=textTag.text;
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:#"Hello World!" message: str delegate: nil cancelButtonTitle:#"OK" otherButtonTitles: nil, nil];
[alert show];
[alert release];
}
The program is build successful but when I clicked on button nothing is displayed in the alertView.
What is the reason nothing is displayed?
What is textTag defined as? You need something such as this in your header file:
IBOutlet UITextField *textTag;
Not only that, you need to hook up textTag in interface builder to the actual text field. Have you done that?
To help diagnose the problem, add the following line below the NSString *str = ... line:
NSLog(#" Text I found in the text field = .%#.", textTag.text);
You should see your text field text appear in XCode console when you hit the button.
Related
I’m popping up a UIAlertView with a UITextField in it. I want the text field to auto-capitalize all words. I’m doing this by setting properties of the text field, but they have no effect at runtime. Here’s my code:
UIAlertView* alert = [[UIAlertView alloc] initWithTitle: title
message: message
delegate: self
cancelButtonTitle: #"Cancel"
otherButtonTitles: #"Create", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField* titleField = [alert textFieldAtIndex: 0];
titleField.autocapitalizationType = UITextAutocapitalizationTypeWords;
[alert show];
this autocapitalization is working on UITextField form XIB but this is not working on UIAlertView ,i am using iOS 6.
If it doesn't change your requirements, using
titleField.autocorrectionType = UITextAutocorrectionTypeDefault;
along with
titleField.autocapitalizationType = UITextAutocapitalizationTypeWords;
gives you the desired effect.
Make sure "Auto-Capitalization" is enabled in Settings in the iOS Simulator and/or on your test device.
Settings > General > Keyboard > Auto-Capitalization
If disabled, it will ignore the autocapitalizationType property on your UIAlertView's text field.
You can use uitextfield delegates
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(#"textFieldDidEndEditing");
textField.text=[textField.text uppercase];
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
It's Irritating ...!!!
I googled about this problem, found some Relative Questions but not Satisfactory Answers.
So I have One - (IBAction) method that adds some UITextField's Values to NSMutableArray when "Add" Button is Clicked. I am simply trying to show UIAlertView, if the UITextField is empty.
My Code :
- (IBAction)addButtonPressed:(id)sender
{
if ([textField1.text length]==0 || [textField2.text length]==0 || !someFlag)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"MyApp" message:#"Please Enter Valid Data..." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
else
{
// Code to add textField's value to Array.
}
}
My Problem :
Whenever I press "Add" Button with empty textField , UIAlertView appears thrice.
1) For the First Time It appears with "Close" Button. (I have never added...) It disappears within some time.
2) Second Time It appears with "OK" Button. (That's what I added...) It disappears when I press "OK" Button.
3) Third Time It appears with "Close" Button again. It disappears when I press "Close" Button.
EDIT :
Similar Question : UIAlertView Pops Up Three Times per Call Instead of Just Once.
Can someone help me to found solution from this ?
Your code contains no issues. There's not 3 it's only 2 alerts. here's the flow of alert view:
As soon as you click on add button there called 2 selector (may be one inside other OR two IBAction by one button) which contains alert view code in them
Now that alert2 (with cancel button) get called before alert1(with ok button)
Then alert1 get called and hide alert2
Now when you resolve alert1(by clicking on ok button) alert2 shows up again
Now what you need to do is to check "if your button is not connected with 2 IBActions", which that should be as you have no such code to call another alert in this method. And check if it helps.
Strange....!!!
Sometimes it happens that you Totally Neglect certain lines of your code When you are Over-Irritated. It happend to me also. I neglected one method that is called from -addButtonPressed Method , Which has One AlertView (With "Close" Button of course) inside it.
That's the Solution itself !!!
yes I have face same problem but my case is different than you .
You should try [textfield.text isEqualToString:#""]; because this is the standard way to compare empty text field in Objective-C.
Check that you dismiss your alert view properly sometimes we don't give focus on dismissing the alert view so because of that your alert view remain active and when you reopen you are app it shows 2 to 3 times depending on your condition. So you can use the delegate did dismiss alert view with button index for dismissing the alert view in view did disappear. I am not sure but it should work for you Good luck dude.
And I am not sure but I think your IBAction Button is overwrite for each time when you clicked in any button so you should check it also.
Try below code......let me know it is working or not!!!!
what you have done is you have given other button nil 2 times..so may be that is the problem...
Happy Coding!!!!
if ([textField.text length]==0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"MyApp" message:#"Please Enter Valid Data..." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
Try compare like this.
if([testBox.text isEqualToString:#""]
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:errorDesc
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
- (IBAction)addButtonPressed:(id)sender
{
if ([textField.text length]==0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Information",#"") message:NSLocalizedString(#"Txt is Empty!",#"") delegate:nil cancelButtonTitle:NSLocalizedString(#"OK",#"") otherButtonTitles:nil];
[alert show]; alert = nil;
}
else
{
// Code to add textField's value to Array.
}
}
First check that how many times you call IBAction method when Button tapped???
Other wise put instance of UIAlertView is a public.. I mean put in .h file and access it as self.yourAlertViewName in .m file.
Thanks :)
Check with below code:
if ([textField.text length]==0)
{
UIAlertView *objAlertMsg = [[UIAlertView alloc] initWithTitle:#"MyApp"
message:#"Please Enter Valid Data..."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[objAlertMsg show];
[objAlertMsg release];
}
Check that, I have set delegate to "nil" instead of "self". And make sure you have not implement delegate in view controller if it's not required.
Hope it will be helpful to you.
Cheers.
I create a class to call UIAlertview show on my screen. I write the UIAlert function in another class. Both these two classes are not my viewController class.
I use this UIAlert, which is a UITextfield inside, to store texts into a plist file.
here is the class to call UIAlert:
#import "Story.h"
#implementation Story
...
+ (void)stage1
{
AlertClass *pointer = [AlertClass new];
[pointer doAlert];
}
here is the class AlertClass.m file:
- (void)doAlert
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
//this makes crash!
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
self.storyFlow.text = [alertView textFieldAtIndex:0].text;
}
Before I add UIAlertViewDelegate in the .h and override the method "clickedButtonAtIndex", it works great. However, I need to store some data from the UITextfield inside the alert view. I get crash and don't know the message it responds as following.
Please help me to solve this problem. Thanks.
[crash pic] https://dl.dropbox.com/u/47381923/crash.tiff
do an NSLog on the text you get back from the Alert View to see whether that is the crash or the subsequent 'self.storyFlow.text = ' is causing it. Perhaps self.storyFlow has not been created yet (with alloc/init)
I have an alert view with a uitextfield added as a subview.
In my uitableview controller the keyboard shows fine.
However in a different view I wanted to do the same thing. So instead of using a UITableView Controller I made it a UIViewController so that I could add a toolbar at the bottom of the view.
But when I display the UIAlertView the keyboard is hidden. I'm thinking it's behind the view because the alert view moves up to make room for the keyboard.
Any ideas?
UPDATE:
The reason the keyboard was being hidden was because I was dismissing a modalviewcontroller after showing the alert. For some reason it would dismiss the keyboard also I guess. Just rearranged the order an fit works fine now...
Try implementing the didPresentAlertView: method and inside set the text field to firstResponder like so:
- (IBAction)someActionThatTriggersAnAlertView:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"TITLE" message:#"MESSAGE" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Done", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *someTextField = [alert textFieldAtIndex:0];
someTextField.keyboardType = UIKeyboardTypeAlphabet;
someTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
someTextField.autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
[alert release];
}
#pragma mark - UIAlertViewDelegate Methods
- (void)didPresentAlertView:(UIAlertView *)alertView {
[[alertView textFieldAtIndex:0] becomeFirstResponder];
}
UIAlertView Documentation
UIAlertViewDelegate Documentation
I'm really new at this. I've been working my way through a couple of "HelloWorld" type tutorials that basically have a text field, a button, and when you click the button it alerts whatever's in the text field. I've gotten as far as getting an alert to show and being able to set the title, message, and button text directly. However my attempts at getting the contents of my UITextField keep failing. I either get errors, no response, or just "(null)". My code's here.
#synthesize textField;
- (IBAction)btnClicked:(id)sender {
//text of textField
NSString *str = [[NSString alloc] initWithFormat:#"Hello, %#", textField.text];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hello!"
message:str delegate:self
cancelButtonTitle:#"Done"
otherButtonTitles:nil];
[alert show];
[alert autorelease];
}
It looks like the textField property is not properly linked to the UITextField object you have in interface builder. Make sure your IBOutlets are set correctly.