When the user opens my app, I want to check for a value stored in the defaults, and if it is not present prompt the user to input a value. The storing/reading of the value appears to be working.
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(#"Checking if there is an email address set");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString * emailAddress = [defaults objectForKey:#"emailAddress"];
if (IsEmpty(emailAddress))
{
NSLog(#"email address is blank, prompting user to enter one..");
self.emailPromptAlert = [[UIAlertView alloc] initWithTitle:#"Alert"
message:#"Enter Email address:"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[self.emailPromptAlert setAlertViewStyle:UIAlertViewStylePlainTextInput];
[self.emailPromptAlert setTag:1];
[self.emailPromptAlert show];
}
}
The problem that I have, is when I do a clean install of my app and load for the first time, the alert shows up as expected but there is no keyboard shown, so the user can't actually type anything.
Clicking the home button then bringing the app back into the foreground again, the same alert is on screen but this time the keyboard is actually showing.
What can I do to make sure the keyboard gets shown the first time?
That's a weird behavior, Why don't you try showing the alert in the next runloop
[self.emailPromptAlert performSelector:#selector(show) withObject:nil afterDelay:0];
I had the same problem. In my case, the keyboard was already visible at the time that I showed the alert. I resolved it by dismissing the keyboard before showing the alert:
[myTextField resignFirstResponder];
[myAlert show];
I am not totally sure, but maybe this could work?
self.emailPromptAlert = [[UIAlertView alloc] initWithTitle:#"Alert"
message:#"Enter Email address:"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[self.emailPromptAlert setAlertViewStyle:UIAlertViewStylePlainTextInput];
[self.emailPromptAlert setTag:1];
[self.emailPromptAlert show];
//try this?
UITextField *textField = [self.emailPromptAlert textFieldAtIndex:0];
[textField becomeFirstResponder];
This doesn't really get to the root of the problem of why it isn't showing in the first place, but it could be a solution.
Related
In my iOS app, I create a UIAlert and add a UITextView to it dynamically:
UIAlertView *tweetAlert = [[UIAlertView alloc] initWithTitle:#"Compose Tweet"
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
tweetTextView = [UITextView new];
[tweetTextView setText:[NSString stringWithFormat:#"%# %#", [[NSUserDefaults standardUserDefaults] valueForKey:#"tweetText"], self.setShareURL]];
[tweetAlert setValue:tweetTextView forKey:#"accessoryView"];
[tweetAlert addButtonWithTitle:#"Tweet"];
[tweetAlert show];
[tweetTextView becomeFirstResponder]; // focus textview and open keyboard
I attempt to focus on the UITextView in the last line so that the keyboard is open when the UIAlertView appears. However, the keyboard does not appear, and the UITextView is not in focus.
I also tried
[[tweetAlert valueForKey:#"accessoryView"] becomeFirstResponder];
But I get the error
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIAlertView 0x1358b3200> valueForUndefinedKey:]: this class is not key value coding-compliant for the key accessoryView.'
Any idea how to focus on the dynamically created UITextView, which is embedded as an accessoryView in a UIAlertView?
You can use the UIAlertViewDelegate method - (void)didPresentAlertView:(UIAlertView *)alertView to make the textview become first responder.
- (void)didPresentAlertView:(UIAlertView *)alertView {
[tweetTextView becomeFirstResponder];
}
It is generally not recommended that you modify the view hierarchy of alert views. You should just use a text entry UIAlertView:
UIAlertView *tweetAlert = [[UIAlertView alloc] initWithTitle:#"Compose Tweet"
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
tweetAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
I want the user to not be able to click the cancel button on UIAlertView, yet I still want it to be there, but shaded out. I know a shouldEnableFirstButton function is there but it doesn't involve cancel button. Do you have any idea how to do it?
Thanks.
Use the following:
UIAlertView *vw = [[UIAlertView alloc] initWithTitle:#"Information" message:#"This is a message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Cancel", nil];
[vw show];
So, essentially you are setting the "Cancel" as other button.
Now, the following will solve your problem:
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
return NO;
}
If you want to perform some action in OK button press you can always do that here:
- (void)alertViewCancel:(UIAlertView *)alertView
Hope you are all doing good.
Where is my problem is concern one UIAlertView shows twice while executing the code. I have written it within -(void)viewDidAppear:(BOOL)animated as shown following
-(void)viewDidAppear:(BOOL)animated
{
//Finding city Name from coordinate
UIAlertView * alertForCoordinate=[[UIAlertView alloc]initWithTitle:#"Coordinate" message:[NSString stringWithFormat:#"latitude==>%f\nlongitude==>%f",self.userLocation.coordinate.latitude,self.userLocation.coordinate.longitude] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
[alertForCoordinate show];
[SVGeocoder reverseGeocode:CLLocationCoordinate2DMake(self.userLocation.coordinate.latitude, self.userLocation.coordinate.longitude)
completion:^(NSArray *placemarks, NSHTTPURLResponse *urlResponse, NSError *error)
{
if (placemarks.count>0)
{
CLPlacemark * placemark=[placemarks objectAtIndex:0];
currentLocationUpcoming =[placemark locality];
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:#"Location" message:[NSString stringWithFormat:#"currentLocation==>%#",currentLocationUpcoming] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[self.eventsTableView reloadData];
}
}];
}
As view appears First UIAlertView gets call and dismiss automatically then second gets call When I tap OK button of second alert and again first UIAlertView appears . I am struggling with it since last 4 hours and some of the answer put here also not intended the context in which I am working. Can anybody suggest me where I am wrong.
Thanks
You don't actually have the first UIAlertView showing twice - the animation just makes it look like this because you are showing two UIAlertViews in a row. You're probably seeing Alert 1-> Alert 2-> tap OK on Alert 2 -> Alert 1. This is because the UIAlertViews stack up if multiple are shown.
Think of it this way: Alert 1 shows first but animates for only a very brief period until it is interrupted by Alert 2 showing. Dismissing Alert 2 animates Alert 1 onscreen again, and even though it is still the same alert, it looks like it is showing twice.
Try this simple test case:
-(void)viewDidAppear:(BOOL)animated
{
[[[UIAlertView alloc] initWithTitle:#"Alert #1" message: delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
[[[UIAlertView alloc] initWithTitle:#"Alert #2" message: delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
}
You will see "Alert #1" for a fraction of a second, then "Alert #2" will remain onscreen. Tapping OK on "Alert #2" will show "Alert #1" again because it was below Alert #2 on the stack.
Your second alert overlaping the first one, and after dismissing second alert, the first one reappears on screen.
-(void)viewDidAppear:(BOOL)animated
{
UIAlertView * alertForCoordinate=[[UIAlertView alloc]initWithTitle:#"Coordinate" message:[NSString stringWithFormat:#"latitude==>%f\nlongitude==>%f",self.userLocation.coordinate.latitude,self.userLocation.coordinate.longitude] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
alertForCoordinate.tag = 1;
[alertForCoordinate show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0 && alertView.tag == 1) {
[SVGeocoder reverseGeocode:CLLocationCoordinate2DMake(self.userLocation.coordinate.latitude, self.userLocation.coordinate.longitude)
completion:^(NSArray *placemarks, NSHTTPURLResponse *urlResponse, NSError *error)
{
if (placemarks.count>0)
{
CLPlacemark * placemark=[placemarks objectAtIndex:0];
currentLocationUpcoming =[placemark locality];
/*****************Second Alert******************/
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:#"Location" message:[NSString stringWithFormat:#"currentLocation==>%#",currentLocationUpcoming] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[self.eventsTableView reloadData];
}
}];
}
}
i had same issue "UIAlertView Shows two times", but in my case it was because of coding error with long press gesture recognizer (different than original question, but may be useful for other folks that google uialertview showing twice). inside method to handle long press, i called method to show UIAlertView. my mistake was not recalling that handle long press method gets called twice each time user long presses (once for press and hold down, once when user releases their finger). the correct code recognizes gesture state (code below).
-(IBAction)handleHoldOnContact:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateBegan){
// NSLog(#"UIGestureRecognizerStateBegan hold");
//Do Whatever You want on Began of Gesture
// ...
} else if (sender.state == UIGestureRecognizerStateEnded) {
// NSLog(#"UIGestureRecognizerStateEnded release");
//Do Whatever You want on End of Gesture
// ...
}
}
I too get the same issue. To overcome this problem, i done the following things. Get the alertview as global variable. And then,
if(!alertForCoordinate){
alertForCoordinate=[[UIAlertView alloc]initWithTitle:#"Coordinate" message:[NSString stringWithFormat:#"latitude==>%f\nlongitude==>%f",self.userLocation.coordinate.latitude,self.userLocation.coordinate.longitude] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
[alertForCoordinate show];
}
After that, when you clicking "OK" button from the Alertview. set that to nil. Like the following,
alertForCoordinate = nil;
Hope, this will help you.
It could be a case of duplicating a button, and therefore it's IBaction references, in main.storyboard.
I had a button that was styled perfectly in main.storyboard, so I alt drag duplicated it and attached a new IBaction to the viewController. I didn't realize it but when I tapped that new button it fired the IBaction for both the first button and the second button, because it was hooked up to both, as it was a duplicate of the first.
I right clicked the second button in main.storyboard, saw the reference to the IBaction from the first button AND the second, then deleted the reference to the first. Problem solved. What a nightmare. Hope this helps someone.
I just started iOS development, and now I'm stuck. I have a table cell, where the user cann see data. Next to it, there are two buttons. One to delete, one to edit. The edit part is where I'm stuck. I have a label field. When the user clicks on the edit button a prompt should come where the user can type in data. I already found something to do it. But there's no save button and I don't know how I could then save the variable to an NSString
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Name der neuen Liste" message:#"" delegate:self cancelButtonTitle:#"Abbrechen" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
I currently have this. lNow a prompt pops up, where I can write something, but not save. I just can press cancel afterwards. How do I work with user input in iOS? I don't want to use the label as an UITextField since it has to be clickable to perform another action.
Try this:
- (void)noteInput
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"input"
message:#"input"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Save", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
UITextField *noteText = [alertView textFieldAtIndex:0];
NSString *note = noteText.text;
}
}
Try adding a Save button in otherButtonTitles. Then, in your UIAlertViewDelegate:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
// Save button pressed
}
}
Please refer the code below:
UIAlertView *progressAlertView = [[UIAlertView alloc]
initWithTitle:#"Title"
message:#"Message"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
progressAlertView.message=#"Hello, I am the new one";
Voice over always reads "Message" and never reads the new message string set in the second line "Hello, I am the new one". There's no way to change this accessibility label of the bodyTextLabel control/subView of UIAlertView.
Any idea how to make UIAlertView change its accessibility labels after its alloc'ed?
Thanks,
-Shilpi
You can use accessibilityTraits as UIAccessibilityTraitUpdatesFrequently to read updated message.
it works like:
alertObj.accessibilityTraits = UIAccessibilityTraitUpdatesFrequently;
Thanks
use this way for set message of UIAlertView
UIAlertView *progressAlertView = [[UIAlertView alloc]
initWithTitle:#"Title"
message:#"Message"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
[alertView setMessage:#"Hello, I am the new one"];
[alertView show];
For More About UIAlertView Refer this
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html
You can change the subviews of UIAlertView to change single attributes like the accessibility label:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Test"
message:#"Message"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
// In a standard UIAlertView there are 2 UILabels and some buttons
// The UILabel with the message is always the 2nd object in the array
NSArray *subviews = alert.subviews;
UILabel *messageLabel = [subviews objectAtIndex:1];
[messageLabel setAccessibilityLabel:#"Some other text"];
With this code you only change the accessibility label, so that VoiceOver reads another text but the old text is shown.
In your case you should set the accessibility label to the same as you want to set the message of the UIAlertView.