Show Alert in clickedButtonAtIndex? - iphone

i need to show a confirm alert after the user press buttonIndex 1 but... if i use popViewcontroller in clickedButtonAtIndex it crash without errors.
The problem is that
[self.navigationController popViewControllerAnimated:YES];
is called before second Alert click...
how to fix?
This is my code:
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:#"OK!"
message:#"Completed"
delegate:self
cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
[self.navigationController popViewControllerAnimated:YES];
}
}

Set the tag properties of the two UIAlertViews to 1 and 2, respectively. Then, in the delegate method, use if statements to check the tag of the UIAlertView argument.
Example:
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1)
{
//check the button index
//create and display the other alert view (set the tag property here to 2)
}
else if (alertView.tag == 2)
{
//pop the view controller
}
}

Related

Tableview was not getting refreshed from alertview?

In my application I have a table view in second view controller. After some time I display an alert view using timer on which I have two buttons yes and no. When I click yes the alert view gets dismissed and when I click the no it should delete all items in table view and reload it.
Problem
I am not able to refresh the table view when I go to first view controller and return to second view controller.If I am in same view controller, it reloads perfectly.
Timer function
start_Timer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:#selector(start_method) userInfo:nil repeats:NO];
Alert show
-(void)start_method
{
Timerfunction_alert = [[UIAlertView alloc] initWithTitle:#"" message:#"You have less than one minute before all items on your order list are released as it is about to exceed the 15-minutes hold time allocated to you to complete your order. Do you wish to continue ordering this item?"
delegate:self
cancelButtonTitle:#"NO"
otherButtonTitles:#"YES", nil];
[Timerfunction_alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView == Timerfunction_alert)
{
if (buttonIndex == 0)
{
//here to refresh the table view
[self.tableview reloaddata]
}
}
}
try this:
[self performSelector:#selector(start_method) withObject:nil afterDelay:30];
My experience tells me that you should follow this snippet
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView == paymentalert)
{
if (buttonIndex == 0)
{
// reload table after main thread gets over, sothat app doesn't crash
[self performSelectorOnMainThread:#selector(ReloadTable) withObject:nil waitUntilDone:YES];
}
}
}
//here to refresh the table view
-(void)ReloadTable
{
[self.tableview reloaddata]
}
[NSTimer scheduledTimerWithTimeInterval:30 target:self selector:#selector(start_method) userInfo:nil repeats:YES];
-(void)start_method
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"You have less than one minute before all items on your order list are released as it is about to exceed the 15-minutes hold time allocated to you to complete your order. Do you wish to continue ordering this item?" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
alert.tag=1;
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag==1)
{
if (buttonIndex == 1)
{
//here to refresh the table view
[self.tableview reloaddata]
}
}
}

How to distinguish between UIAlertView Cancel and UIAlertViewStyleSecureTextInput's TextView Return?

I have created a UIAlertView
alert = [[UIAlertView alloc] initWithTitle:#"Test"
message:#"Working"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
alert.tag = kAlertTypePIN;
UITextField *textField = [alert textFieldAtIndex:0];
textField.delegate = self;
If I press Retun key in UIAlertView textfield it works fine, it calls:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
and then
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(#"didDismissWithButtonIndex");
// If no text or wrong text show alert again otherwise normal actions
}
But if I press the cancel button, it 1st calls textFieldDidEndEditing which in turn calls the alert delegate. And again it calls the alert delegate method by itself.
So alert to be displayed is not getting shown and keyboard starts to pop up and goes back. So no alert is being shown in case when its to be shown.
If any doubts in the flow, please ask me.
How can I rectify the issue?
unset the delegate of the textField in alertView:willDismissWithButtonIndex:
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
UITextField *textField = [alert textFieldAtIndex:0];
textField.delegate = nil;
}
When the alert is dismissed the textField will end editing, and afterwards it will call the textFieldDidEndEditing: delegate method.
If you set the delegate to nil before the dismissal starts, the delegate method can't be called.
Besides that, a better design would be to have a cancel button "Cancel", and an other button "Submit". When the textField ends you dismiss the alert with "Submit", and not "Cancel".
You simply want to hide UIAlertiView on cancel click?
Then if your cancel buttonindex is 1 then:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
if(buttonIndex == 1) {
[[alert textFieldAtIndex:0] resignFirstResponder];
}
}
try this condition.. as you dont want alert to be gone when Pin is not there...
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if([textField.text length]!=0)
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
If you want to hide UIAlertiView on "Cancel" button click then just simply add this code to clickedButtonAtIndex:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
[[alert textFieldAtIndex:0] resignFirstResponder];
}
}
Try this
Add UIAlertView following delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
[[alert textFieldAtIndex:0] resignFirstResponder];
}

poptorootview when click on alerrtview ok

I have a home view ,when click on that it is going to another view again i am going to another view.when click on a button on that view a modalview will appear and then subsequently 3 more modal views when click on each modalview.when click on the final modalview an alert will appear and when click on that alert i want to show the root homeview.Is it possible
?
Display AlertView using given code snippet:
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:title message: #"Alert Message"
delegate: self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
Delegate Method implementation :
(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
Sample Code given below:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Alert Message?" message:#"Error......" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK"] autorelease];
[alert show];
The implemention alertView's delegate functions is given below
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
//cancel clicked ...do your action
}
else if (buttonIndex == 1)
{
//OK clicked
[self.navigationController popToViewController animated:YES];
}
}
just give the delegate in .h file and after in delegate method of alertview write bellow code..
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
[self.navigationController popToRootViewControllerAnimated:YES];///this line is important..
}
else{
// do your action...
}
}
i hope this answer is useful to you..
:)
int c=[self.navigationController.viewControllers count]-4;
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:c] animated:YES];

opening previous/back screen in iphone clicking on leftbarbuttonitem showing alert with "ok" and "cancel"

i am working on a navigation based application.On the top of the navigation bar there is a by default back button option.i have implemented a uialertview on click event of the back button.
-(IBAction)gotolevelcontroller:(id)sender//method is declared in leftbarbuttonitem's action selector
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Exit" message:#"Do You want to exit?" delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:#"Cancel", nil];
alert.delegate = self;
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
{
//go to previous screen of navigation control.
//what is code to go to previous screen
NSLog(#"ok");
}
else
{
//remain on same screen of navigation control
NSLog(#"cancel");
}
}
any suggestions?
Thanks
Add this code
if (buttonIndex == 0)
{
[self.navigationController popViewControllerAnimated:YES];
}
You are looking for the poptoviewcontroller method of the navigation controller.
[self.navigationcontroller poptoviewcontroller:<prev-viewcontroler> animated:YES/NO];
Back button pops the current viewController.
so add :
[self.navigationController popViewController Animated:YES];
to buttonIndex == 0
and for cancel.. just type return;

UIAlertView brings up completely unexpected function

-(void) buttonClick:(id) sender
{
action=[[UIAlertView alloc] initWithTitle:nil message:#"Confirm for action" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[action addButtonWithTitle:#"Yes"];
[action show];
[action release];
}
- (void)alertView:(UIAlertView *)action didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[self func1];
}
else if (buttonIndex ==0) //cancel
{
}
}
instead of executing func1 when clicked Yes on alertview, the app pop up a registration form from my registration page in the app.
set a break point on "if (buttonIndex == 1) " and follow the code using step into button. the problem should be somewhere else in your program.