In my UIAlertView, I want to open another UIView when "OK" button is pressed.
But the problem is, even after the UIView is displayed, alert remains in screen and once it fades away, the UIView seems to be disabled.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Add details" message:#" Do you like to set the details now?" delegate:self cancelButtonTitle:#"Yes" otherButtonTitles:#"No",nil];
[alert show];
[alert release];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{ // the user clicked one of the OK/Cancel buttons
NSString *title = [alertView title];
if([title isEqualToString:#"Add details"])
{
.......
Any help would be appreciated!
Why not check for the button pressed in the delegate method instead of the title?
That is, in
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0){
// User pressed "YES"
}else{
// User pressed "NO"
}
}
The cancel button has index 0 and the other buttons increase in index. In order to detect which alert was this, you should also give it a tag. Hope this helps.
Could be because the new view is added before the alert view is actually dismissed. So better use the didDismissWithButtonIndex delegate to show a new view on button click event of an existing alert view, instead of clickedButtonAtIndex
- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
//Add the view
}
Instead of
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//Add the view
}
Related
I have UIViewController with UITextView. This TextView always firstResponder.
- (void)viewDidLoad
{
[super viewDidLoad];
[self createBottomBar];
_textView.delegate = self;
[_textView becomeFirstResponder];
}
When user tap "video" button my app show UIAlertView with textfield
- (IBAction)showVideoPicker:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_(#"Link to movie")
message:_(#"Insert a link to YouTube clip or a Coub")
delegate:self
cancelButtonTitle:_(#"Cancel")
otherButtonTitles:_(#"Attach"), nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
When user tap "Attach" button alert dismiss and keyboard dismiss and then immediately show. I would like another scenario: keyboard do not dismiss, because text view should always be a first responder. Just UIAlertView dismiss wihout keyboard dismiss/show animation.
For more understanding see gif here (do not have enough reputation to post images). Sorry for my English.
Have you tried setting the first responder on the Attach button tap via UIAlertView delegate? It's the only way here:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) [_textView becomeFirstResponder];
}
The keyboard might or might not jump a little, but that's all we have.
I have a segue that I want to be preformed when I push the Manual Entry and Scan Tag buttons on my AlertView.
#implementation triageViewController
- (IBAction)addNew:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Add A New Entry"
message:#"Choose a way to add a new entry."
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Manual Entry", #"Scan Tag", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != alertView.firstOtherButtonIndex) {
[self performSegueWithIdentifier:#"manual" sender: self];
}
if (buttonIndex != alertView.cancelButtonIndex) {
[self performSegueWithIdentifier:#"scan" sender: self];
}
}
(I realize this would probably make the cancel button preform a segue right now, but i'm more concerned with making it work)
I have the two segues in my storyboard, going from a UITabViewController to two different UIViewControllers. neither of these are being called, so when I tap them. I tried using both push and modal segues, but neither one was working.
I also tried the if/else statement like so:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
[self performSegueWithIdentifier:#"manual" sender: self];
}
if (buttonIndex == 1) {
[self performSegueWithIdentifier:#"scan" sender: self];
}
}
But this is not working either.
Could someone help me figure out what is going wrong? Again in case I was unclear, I want to preform a segue from a UIAlertView pop-up located on a UITabBarController to two separate UIViewControllers.
Thanks
It looks like your UIAlertView has a delegate of nil so your function is never being called. Set that to self and then in your view controller's header make sure you're implementing the UIAlertViewDelegate protocol.
I have 2 view controllers, ViewController 1(VC1) and 2(VC2). In VC2 I have a back and done button. On clicking back button it goes directly to VC1 and on done it makes an api call and when it gets a response it shows an alert view and clicking ok goes back to VC1. Now when I make a api call a loading bar shows up and disappears when I get response and shows the AlertView. But if during that fraction of second when the loading disappears and AlertView is going to be popped up if I click on back and the view changes to VC1, the alert appears on VC1 and results in a crash.
This is a rare case as no user will purposely try for it but I was wondering if that crash can be managed without disabling the back button. I think there can be other instance such cases like if we are making an asynchronous calls and if the user is allowed to use UI while waiting for response and if any error alert that was suppose to show on one ViewController shows up in another may result in crash since the delegate that alert is referring to is that of the previous view controller. So is there any way to handle this kind of crash efficiently?
//Alert View sample
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[[message objectAtIndex:1] capitalizedString] message:[message objectAtIndex:0] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] ;
[alert setTag:701];
[alert show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ([alertView tag] == 701)
if (buttonIndex == 0)
{
[self.navigationController popViewControllerAnimated:YES];
}
}
The proper way to fix this problem is to use an instance variable to keep a reference to the alert view.
This instance variable should be set to nil in the alertView:didDismissWithButtonIndex: delegate method.
In the view controller's dealloc method, you call dismissWithClickedButtonIndex:animated: if the instance variable is still set.
Assume _alertView is the instance variable.
Create the alert:
_alertView = [[UIAlertView alloc] initWithTitle:[[message objectAtIndex:1] capitalizedString] message:[message objectAtIndex:0] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] ;
[_alertView setTag:701];
[_alertView show];
Update your existing alertView:clickedButtonAtIndex: method:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 701) {
_alertView.delegate = nil;
_alertView = nil;
if (buttonIndex == 0) {
[self.navigationController popViewControllerAnimated:YES];
}
}
}
Add:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
_alertView = nil;
}
Add:
- (void)dealloc {
if (_alertView) {
_alertView.delegate = nil;
[_alertView dismissWithClickedButtonIndex:_alertView.cancelButtonIndex animated:NO];
_alertView = nil;
}
}
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);
}
I am trying to use a UIAction sheet to confirm a user’s action. The log prints fine... but the app hangs and shows the lightened circle in the middle like when you do a UIAlert view. I’m sure its something simple... but can’t seem to find it.
-(IBAction)showActionSheet:(id)sender
{
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:#"Are you sure?" delegate:self cancelButtonTitle:#"Cancel Button" destructiveButtonTitle:#"Reset Player" otherButtonTitles:nil];
popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[popupQuery showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSLog(#"Destructive Button Clicked");
}
else if (buttonIndex == 1) {
NSLog(#"Cancel Clicked");
}
Implement this handler instead of clickedButtonAtIndex:
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
Please release the UIActionSheet i.e where you create
[popupQuery release];