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.
Related
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
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];
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.
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
I have a button in a menu which when touched, pops up a alert message with two buttons: "Cancel" and "Yes". This is the code I have for the alert:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Exit game"
message:#"Are you sure?"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Yes", nil];
[alert show];
Is it possible to add an action to the button "Yes"?
In your code set the UIAlertView delegate:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Exit game" message:#"Are you sure?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Yes", nil]; [alert show];
As you have set delegate to self, write the delegate function in the same class as shown below:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) { // Set buttonIndex == 0 to handel "Ok"/"Yes" button response
// Cancel button response
}}
You need to implement the UIAlertViewDelegate
and add the following...
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
// do stuff
}
}
Yes it is easy. See that argument called "delegate" that you have set to nil right now? Set that to an object... usually "self" if you are calling it from your view controller and then implement the selector for UIAlertViewDelegate.
You also need to declare that your view controller conforms to the UIAlertViewDelegate protocol. A good place to do this is in the "private" continuation class of the view controller.
#interface MyViewController() <UIAlertViewDelegate>
#end
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"Button pushed: %d", buttonIndex);
}