how to move to next view controller after showing alertview - iphone

I have iPhone and I want that when AlertView is shown and user presses OK button after that view should be changed but it is not happening.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Thank you" message:#"" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
return;
[self moveToView];
-(void)moveToView
{
MainViewController*targetController=[[MainViewController alloc]init];
[self.navigationController pushViewController:targetController animated:YES];
}

Please use UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Code to move to next view
[self moveToView];
}
Note: Implement the UIAlertViewDelegate in your interface Declaration. Also while declaring UIAlertView set the delegate to self.
Hope this helps you.

Its simple. Implement UIAlertViewDelegate and put code there.
- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
MainViewController*targetController=[[MainViewController alloc]init];
[self.navigationController pushViewController:targetController animated:YES];
[targetController release];
}

in .h , implemented UIAlertViewDelegate
#interface ClassName : ParentClassName <UIAlertViewDelegate>
in .m , Add this method,
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
[self moveToView];
}

Implement alert view's delegate:
In your yourClass.h file:
#interface yourClass : UIViewController<UIAlertViewDelegate>{
}
#end
In your yourClass.m file:
#implementation yourClass
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
[self moveToView];
}
#end

Well , you are setting self as the delegate of the UIAlertView. That is correct and it's the first step you must take. After that go ahead and implement this method :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self moveToView];
}
You can also make a switch statement here to see what button was pressed. But since you only have the OK button on the AlertView , it's not necessary.
Hope this helps.
Cheers!

Implement your code in - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex method and also add UIAlertViewDelegate in your class .h file.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"The index: %d",buttonIndex);
MainViewController*targetController=[[MainViewController alloc]init];
[self.navigationController pushViewController:targetController animated:YES];
[targetController release];
}
I think it will be helpful to you.

Related

alertView didDismissWithButtonIndex never called

Please excuse me if something is not post right... first time posting.
I have seen a few questions simular to this but none with the same problem. I am running IOS 6.1 and Xcode 4.6. The problem is that didDismiss is never called, only willDismiss. My code is below along with the log output. Any ideas?
#import "MenkLabUIAlertTestViewController.h"
#interface MenkLabUIAlertTestViewController ()
#end
#implementation MenkLabUIAlertTestViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)test:(id)sender {
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"Encrypting File(s)" message:#"Please wait..." delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
// UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[av show];
[av dismissWithClickedButtonIndex:-1 animated:YES];
}
- (void) alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(#"willDISMIS");
}
- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(#"didDISMIS");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Log output:
2013-07-08 17:27:04.055 testUIAlertView[10534:11303] willDISMIS
This is just a test app, however, it is the exact same problem that exists in my current application.
Thanks in advanced. Been racking my head on this all day!
I think this an artifact of the fact that you are showing, then immediately dismissing the alert view in the same method -- you would never actually do this in a real app. If you create a property for the alert view, and then do the test like below, it works fine:
- (IBAction)test:(id)sender {
self.av = [[UIAlertView alloc] initWithTitle:#"Encrypting File(s)" message:#"Please wait..." delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[self.av show];
[self performSelector:#selector(dismissAlertView) withObject:nil afterDelay:1];
}
-(void)dismissAlertView {
[self.av dismissWithClickedButtonIndex:-1 animated:YES];
}
- (void) alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(#"willDISMIS");
}
- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(#"didDISMIS");
}
I had faced a similar issue, as a workaround we added a selector Method which runs after some delay which will instead trigger the dismissal of alert view. I am not sure why it does not work if we ask the alert to to dismiss immediately after it is shown. Hope it helps.
I ran into this problem too. For me it was related to trying to programatically dismiss it with a button index on -1. We ended up going down a different path in the end for other reasons. However, There is a cancel button index on the actionsheet that you can try calling it with.
I ran into this problem once. For me the problem was caused by a collision between animations. The didDismiss selector is called when the animation ends. If another animation is started between willDismiss and didDismiss then in some rare circumstances the didDismiss doesn't have to be called.
Also note that it never works well if you try to dismiss the alert before it is fully displayed.
I have added. That solves my problem.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
}
}

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];

UIAlertView. Using otherbutton as a openURL.

I want it once the user taps the 'otherbutton' it goes to my link.
But at the moment once the button is pressed it doesn't do anything.
Sorry im on my iPhone so I've used pastebin for the code
http://pastebin.com/fvgk87ih
Thanks alot
Conform your controller class to the UIAlertViewDelegate protocol.
#interface MyViewController : UIViewController <UIAlertViewDelegate>
Create the alert view with delegate set to self:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Follow" message:#"me on twitter" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
Respond to the delegate method
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [alertView cancelButtonIndex]) {
// open url
}
}
The delegate of your UIAlertView must be set to the class (self in most cases) that's using it. Make sure your class calling the UIAlertView does conform to the UIAlertViewDelegate delegate.
You need to set the UIAlertView's delegate property to self instead of nil and implement the protocol in the header.
Header File:
#interface MyViewController : UIViewController <UIAlertViewDelegate>
In addition, check for the buttonIndex. Currently, (after performing the above), it will open the URL if any button is pressed.
- (void)alertView: (UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex
{
if (buttonIndex != [alertView cancelButtonIndex])
{
//open url
}
}

How to check alertview values

This is my code :
self.myAlert = [[[UIAlertView alloc] initWithTitle:#"MNB" message:#"R u want to delete" delegate:self cancelButtonTitle:#"OK",nil otherButtonTitles:#"Cancel",nil] autorelease];
[myAlert show];
Here I would like to process if OK button click and also for cancel button, I would like to redirect the page if OK button clicked....I need the coding, IF condition statement when OK button clicked.....pls help me....
Read UIAlertViewDelegate Protocol Reference.
You can implement following methods.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
Just need to write the Delegate method of UIAlertView like this
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex==1){
NSLog(#"Cancelled Clicked");
}
if(buttonIndex==0){
NSLog(#"O.K Clicked");
}
}
Will surely work :)
You can use UIAlertView delegate methods. For example
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0) //your OK button
{
//Some code here
}
else if(buttonIndex == 1)
{
//Some other code
}
}

iphone navigationController : wait for uialertview response before to quit the current view

I have a view with a back button managed with a navigation controller and I want to check if a file has been saved when the user click on the back button.
If the file has been saved you go back in the previous view, else a uialertview ask you if you want to save the file or not.
So I did that but the view disapear and the alertview appear after.
-(void)viewWillDisappear:(BOOL)animated {
if(!self.fileSaved){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"Save the file?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes",nil];
[alert show];
[alert release];
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
NSLog(#"NO");
break;
case 1:
NSLog(#"yes");
break;
default:
break;
}
}
When viewWillDisappear is called, it's already too late. You should intercept the back button earlier on. I have never done it, but my suggestion is to set the delegate on the navigationBar property in your viewDidAppear method:
// save the previous delegate (create an ivar for that)
prevNavigationBarDelegate = self.navigationController.navigationBar.delegate;
self.navigationController.navigationBar.delegate = self;
Don't forget to set it back in viewWillDisappear:
self.navigationController.navigationBar.delegate = prevNavigationBarDelegate;
Then intercept the shouldPopItem method:
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
if(!self.fileSaved) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"Save the file?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes",nil];
[alert show];
[alert release];
return NO;
}
if ([prevNavigationBarDelegate respondsToSelector:#selector(navigationBar:shouldPopItem:)])
return [prevNavigationBarDelegate navigationBar:navigationBar shouldPopItem:item];
return YES;
}
And in the YES handler for the dialog, manually pop the controller:
[self.navigationController popViewController:YES];
You must subclass UINavigationController for this to work. Then override - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item .
You should set up a custom Delegate protocol that your view controllers adopt and, if you allow it to pop, call your [super navigationBar shouldPopItem:], else, return NO to the above method.
Wouldn't it be easier just to add a left button item as in the following:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(saveThisDate)];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];
To follow up on nobre response and as Jon mentionned it, the best way is to subclass UINavigationController.
The easiest way and fastest way to acheive this :
Modify the class of your navigation controller in Interface Builder to inherit from CustomNavigationControllerDelegate
Implement the CustomNavigationControllerDelegate protocol in your UIViewController
#interface YourViewController <CustomNavigationControllerDelegate>
#pragma mark - UINavigationBar Delegate Methods
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancel otherButtonTitles:ok, nil];
alert.tag = kpopup_back;
[alert show];
return NO;
}
Register your controller as the delegate
#pragma mark - viewWillAppear
- (void) viewWillAppear:(BOOL)animated
{
((CustomNavigationController*)self.navigationController).customDelegate = self;
}
Finally and important part, REMOVE the delegate (to avoid to re-trigger yourself on the pop) and pop the controller yourself in the the UIAlertViewDelegate
case kpopup_back :
{
if(buttonIndex != 0) //OK
{
((CustomNavigationController*)self.navigationController).customDelegate = nil;
[self.navigationController popViewControllerAnimated:YES];
}
}
break;
It works flawlessly on my side, hope it can help.
Here are the sources :
CustomNavigationControllerDelegate.h
#import <UIKit/UIKit.h>
#protocol CustomNavigationControllerDelegate <NSObject>
#optional
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item;
#end
#interface CustomNavigationController : UINavigationController
#property (nonatomic, retain) id<CustomNavigationControllerDelegate> customDelegate;
#end
CustomNavigationControllerDelegate.m
#import "CustomNavigationController.h"
#interface CustomNavigationController ()
#end
#implementation CustomNavigationController
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
if (_customDelegate && [_customDelegate respondsToSelector:#selector(navigationBar:shouldPopItem:)]) {
return [_customDelegate navigationBar:navigationBar shouldPopItem:item];
}
return YES;
}
#end