UIAlertView is dismissed, but UIKeyboard lags - ios

I am running iOS8 and I am showing a UIAlertView, like this
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Test" message:#"Please enter number" delegate:self cancelButtonTitle:#"Enter" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[[alert textFieldAtIndex:0] becomeFirstResponder];
[alert show];
And then when the enter is tapped I have
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"Entered: %#",[[alertView textFieldAtIndex:0] text]);
}
And when enter is tapped the method is called an I see the number and the UIAlertView goes away but the keyboard lags for about 1 second or 2. How can I make is so that the keyboard doesn't lag, I tried calling
[self resignFirstResponder];
from with in the clickedButtonAtIndex method but it didn't change anything.
Thanks for the help.

You are telling the view controller (self) to resignFirstResponder but it's actually the text field that it the first responder, so do this:
[[alertView textFieldAtIndex:0] resignFirstResponder];

Related

Make UIAlertView stay

I have a UIAlertView that I implemented in viewDidLoad. I'm trying to make the alertView stay when the otherButton (buttonAtIndex:1) was selected. Here is my code:
UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Message:"
delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Done", nil];
[dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];
[dialog show];
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) return;
[alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
}
When the second button was selected ("Done"), the alertView goes away. How can I make it stay?
You should create your own alert view class that is NOT a subclass of UIAlertView. UIAlertView's documentation, it says under 'Subclassing notes:
The UIAlertView class is intended to be used as-is and does not support subclassing. (...)
Above referenced in UIAlertView Apple Documentation section marked Subclassing Notes
You might have what you want here :
Subclass UIAlertView and then overload
-dismissWithClickedButtonIndex:animated:, e.g.
#implementation MyAlertView
-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
if (buttonIndex should not dismiss the alert)
return;
[super dismissWithClickedButtonIndex:buttonIndex animated:animated];
}
#end

Dismiss alert view when performing delegate method

hi there when i run my delegate method which is parsing json data the alert view appears to freeze whilst it is performing the method is there anyway to hide the alert view whilst the app is running the code I've tried
- (IBAction)btnAdd:(id)sender {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Add Source" message:#"Enter the web address of the json data" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Add", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert setTag:0];
[alert dismissWithClickedButtonIndex:-1 animated:YES];
[alert show];
}
this doesn't actually do anything.
any advice?
*UPDATE
in the delegate method i get the same result
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag == 0) {
if (buttonIndex == 1) {
NSString *textEnteredraw = [[alertView textFieldAtIndex:0] text];
[alertView dismissWithClickedButtonIndex:-1 animated:YES];
From method
- (IBAction)btnAdd:(id)sender
Remove
[alert dismissWithClickedButtonIndex:-1 animated:YES];
Because as #Dima said, you are dismissing the alertView before you are even showing it.
You call the code to hide the alert before you even show it. This method is meant to be called after the alert is shown.

UIAlertView with a textfield not popping up keyboard in iOS 8 [duplicate]

This question already has answers here:
UIAlertView's textfield does not show keyboard in iOS8
(8 answers)
Closed 8 years ago.
I have a UIAlertView with a textfield inside (UIAlertViewStylePlainTextInput) that automatically pops up a keyboard in iOS 7. However, when I upgraded to iOS 8, the keyboard didn't automatically popup and instead I would have to tap on the textfield to get the keyboard to popup. Is there anyway to restore that automatic keyboard popup to the UIAlertView textfield when I call it?
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"" message:#"The amount?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeNumberPad;
[alertTextField setTextAlignment:NSTextAlignmentCenter];
[alert show];
[alert release];
Update: [alertTextField becomeFirstResponder]; does not work nor does
[[alert textFieldAtIndex:0] setDelegate:self];
[[alert textFieldAtIndex:0] becomeFirstResponder];
Also, there is a textfield flashing indicator inside the textview, which is problematic because there is no actual keyboard that pops up automatically until the user taps on the textfield itself.
I had to unselect "Connect Hardware Keyboard" in the iOS Simulator Hardware->Keyboard menu options for running in iOS 8.
If you add this line just after [alert show] it will make the keyboard pop up.
[alertTextField becomeFirstResponder];
Hope this helps

Edit Text in Pop Up Box

I need some kind of easy way to be able to edit a title when a button is pressed. I would basically just redirect to a view controller with the edit possibility included but that just seems to "heavy". It would be far better to just be able to edit text in a pop up box or something. Is this possible and if so how would I do this?
You can use an UIAlertview control to update your title.
In your button click put the following code.For example
-(IBAction)ButtonClick:(id)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
[alert release];
}
In the UIAlertViewDelegate method
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == alertView.cancelButtonIndex)
NSLog(#"%#", [alertView textFieldAtIndex:0].text);
}
I hope this will help you.
Please note the alert.alertViewStyle = UIAlertViewStylePlainTextInput only works in iOS 5.0 and later.

Show Next Screen Only After User Closes Alert

I'm working on a losing screen and I'd like to let users view their game before showing the stats screen. I've decided to use an alert to show users the current screen. After they hit OK, they should be taken to the stats screen.
The problem is, the stat screen pops up at the same time as the alert. How can I make sure the stats screen opens after the user closes the alert?
if ([self.model userHasLost]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry!" message:#"You lost..." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[self _showGameEndScreenWitnWin:NO]; //Stats screen should open after alert is closed
}
You need to use delegate method of UIAlertView and also need to set delegate of UIAlertView = self,
Following is delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
check buttonIndex in this method it's give you NSInteger and manage click of button.
For more information read this UIAlertView Delegates.
You need to set the alert view delegate. In this example self will conform to the UIAlertViewDelegate protocol.
if ([self.model userHasLost]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry!" message:#"You lost..." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.delegate = self;
[alert show];
}
You can then listen to a delegate method for when the alert view is dismissed :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[self _showGameEndScreenWitnWin:NO]; //Stats screen should open after alert is closed
}
For this situation need to use delegate method,
For simple Example Refer Here: http://www.idev101.com/code/User_Interface/UIAlertView.html

Resources