I have a problem when I try to dismiss my UIAlertView. The UIAlertView launches correctly, and displays two buttons: "Yes" and "No". However, when I select the Yes button, nothing happens. I am wondering what I am doing wrong, and why nothing occurs when the Yes button is selected.
My code is as follows:
-(IBAction)gotoURL
{
[self displaySafariDialogue];
}
-(void)displaySafariDialogue
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Are you sure you wish to exit this app and launch the safari browser?"
message:#"Click yes to continue"
delegate:nil
cancelButtonTitle:#"Yes"
otherButtonTitles:#"No", nil];
[alert setTag:100];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"Alert view button clicked");
if(alertView.tag == 100)
{
if (buttonIndex==0)
{
NSLog(#"Yes button selected");
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
if (![[UIApplication sharedApplication] openURL:url])
NSLog(#"%#%#",#"Failed to open url:",[url description]);
}
}
}
You need to set the delegate.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Are you sure you wish to exit this app and launch the safari browser?"
message:#"Click yes to continue"
delegate:self
cancelButtonTitle:#"Yes"
otherButtonTitles:#"No", nil];
Related
I have a login screen Where there are two text fields for username and password. When email is incorrect then i moved down the keyboard and show the appropriate alert for the user. When i clicked on cancel button of alertview then alert view disappears and scroll view goes upwards. This issue only comes on devices after iOS 8.1. On iOS 8.1 and earlier, it works fine. I don't understand what is the exact cause? Below is the code
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
if (txtActiveField == txtUsername) {
[txtPassword becomeFirstResponder];
}
else if (txtActiveField == txtPassword) {
[txtPassword resignFirstResponder];
//[self loginViaEmail:self];
[self performSelector:#selector(loginViaEmail:) withObject:btnLogin afterDelay:0.0];
}
return YES;}
-(IBAction)loginViaEmail:(id)sender{
//[txtActiveField resignFirstResponder];
if ([[txtUsername.text stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" "]] length] == 0)
{
[AppDelegate showWithTitle:title message:#"Please enter Username/Email for your account"];
}
else if ([txtPassword.text length] < 6)
{
if([[txtPassword.text stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" "]] length] == 0)
[AppDelegate showWithTitle:title message:#"Please enter Password"];
else
[AppDelegate showWithTitle:title message:#"Password contain minimum 6 characters"];
}
#pragma mark Show alert with message
+(void)showWithTitle:(NSString *)title message:(NSString *)msg
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
I think it is the issue with Alert view. As from iOS 8, AlertViewController comes instead of alert view. Try using below code which works fine on iOS 8.4 and below version
if (IS_OS_8_OR_LATER) {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
}];
[alertVC addAction:cancelAction];
[[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:alertVC animated:YES completion:^{
}];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
I have a tabbed application with 2 tab views. I want to change the view from the second tab to the first when the user presses "OK" on an alert view.
The following code snippet works when I use it on an -IBAction button pressed, but it doesn't do anything when I use it inside the alert code:
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
Why does it not work when used as below, in my SeondViewController.m in my
- (void)viewDidLoad {
[super viewDidLoad];?
UIAlertView *alert =[[UIAlertView alloc]
initWithTitle:#"Camera and Target coincident"
message:msg
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
I'm just learning this, so anything you can offer would be helpful.
When I use the following code:
if ([ theProjection Trans_Initialise] == 1) {
NSString * msg = nil;
UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:#"Camera and Target coincident" message:msg delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:#"OK"]) {
self.tabBarController.selectedIndex = 0;
}
}
I get the error message on -(void) alertView "Invalid argument type void to unary expression" Am I doing something grammatical, or is it (quite possibly) something I just didn't understand?
First of all, you need a delegate call back from the alert view informing you about the button that was clicked.
Your alert view should be created like this :
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:#"Camera and Target coincident" message:msg delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
Then in your m file implement the function :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:#"OK"]) {
self.tabBarController.selectedIndex = 0;
}
}
Im trying to put in a rate App alert, but its not opening properly, in fact when I click the Yes button it dosnt open the url at all, How do I make it work?
Below is the call in the viewdidload:
The below code in viewdidload is also in app delegate
[super viewDidLoad];
//rate app
NSUserDefaults *prefsr = [NSUserDefaults standardUserDefaults];
NSInteger launchCount = [prefsr integerForKey:#"launchCount"];
if (launchCount >= 1) {
UIAlertView *alertRate = [[UIAlertView alloc] initWithTitle:#"Like this app?" message:#"Rate on the app store." delegate:nil cancelButtonTitle:#"No, thanks" otherButtonTitles:#"Yes",#"Remind me later", nil];
[alertRate show];
}
-(void)alertViewRate:(UIAlertView *)alertViewRate clickedButtonAtIndex:(NSInteger)buttonIndexP{
if (buttonIndexP == 2) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
else if (buttonIndexP == 1){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
Add the delegate Self while creating the UIAlertView
UIAlertView *alertRate = [[UIAlertView alloc] initWithTitle:#"Like this app?" message:#"Rate on the app store." delegate:self cancelButtonTitle:#"No, thanks" otherButtonTitles:#"Yes",#"Remind me later", nil];
[alertRate show];
Delegate method for UIAlertView also need to be chnaged
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 2) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
else if (buttonIndex == 1){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
}
You haven't set the delegate to self while you are creating UIAlertView object. So -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex method is not getting called when you click on buttons in the AlertView.
Change your code From
UIAlertView *alertRate = [[UIAlertView alloc] initWithTitle:#"Like this app?" message:#"Rate on the app store." delegate:nil cancelButtonTitle:#"No, thanks" otherButtonTitles:#"Yes",#"Remind me later", nil];
To
UIAlertView *alertRate = [[UIAlertView alloc] initWithTitle:#"Like this app?" message:#"Rate on the app store." delegate:self. cancelButtonTitle:#"No, thanks" otherButtonTitles:#"Yes",#"Remind me later", nil];
A button from the first alert executes the second alert which is basically a confirmation to call someone. I don't get any errors but it doesn't work.
When I press on the second alert the Call button it crashes
-(void) alertView: (UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonString = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonString isEqualToString:#"Phone"])
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Notice!" message:#"You are about to call .... Do you wish to continue?" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"Call", nil];
[alert2 show];
if ([buttonString isEqualToString:#"Call"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel://12345678"]]];
}
if([buttonString isEqualToString:#"Website"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"website"]];
}
if ([buttonString isEqualToString:#"Facebook"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.facebook.com/groups/..../"]];
}
}
}
You assigned to delegate as a nil in second alertview. That's why the alertView:clickedButtonAtIndex: delegate method is not called in second time. So you should assign to delegate as a self.
-(void) alertView: (UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonString = [alertView buttonTitleAtIndex:buttonIndex];
if([buttonString isEqualToString:#"Phone"]) {
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Notice!" message:#"You are about to call .... Do you wish to continue?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Call", nil];
[alert2 show];
}
if([buttonString isEqualToString:#"Call"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel://12345678"]]];
}
if([buttonString isEqualToString:#"Website"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"website"]];
}
if([buttonString isEqualToString:#"Facebook"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.facebook.com/groups/..../"]];
}
}
I think it will be helpful to you.
Launch the second alert with a delay. Problem is - alert needs some time to hide itself, before any new alerts can appear, thus directly calling second one will not work.
try this for example:
- (void) alertView: (UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonString = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonString isEqualToString:#"Phone"])
{
[self performSelector:#selector(callSecondAlertView) withObject:nil afterDelay:1];
}
}
- (void)callSecondAlertView
{
//show new alert view
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Notice!" message:#"You are about to call .... Do you wish to continue?" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"Call", nil];
[alert2 show];
[alert2 release];
}
If not working, or too long delay - play with afterDelay: value.
got it! for some reason the [[[alertView subviews] objectAtIndex:6] setBackgroundColor... for the fisrt alertView buttons i had interferes with the second alertView functionality. xcode though did not direct me directly to the problem until i updated xcode to 4.4.1
Use the alertView:didDismissWithButtonIndex: delegate method instead of the alertView:clickedButtonAtIndex: delegate method. The former is called after the alert is gone. This make more sense when you want to show a second based on the tapped button of the first alert view.
Also give tag to UIAlertview objects.
I want to delete data in UITableView using plist and i want to put alertview when delete button is pressed. I want to mention that i have used edit button to delete the data from tableview. code of edit button is as follows: for your information.
self.navigationItem.leftBarButtonItem=self.editButtonItem;
Plz help me solve this problem.
thanx in advance.
In your function write :
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: #"" message: #"" delegate: self cancelButtonTitle: #"OK" otherButtonTitles: nil];
alertView.tag = 1;
[alertView show];
[alertView release];
On alert "OK" button pressed
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)
{
if (alertView.tag == 1)
{
//Do your code
}
}
}
Hope it helps...
on you button action write:-
-(IBAction)btnPressed
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alert didDismissWithButtonIndex:(NSInteger)buttonIndex
{
}