AlertView open Safari - ios

i have made an alert:
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:#"mymaths.co.uk" message:#"This is a great website for maths exercises!! Have fun!!\n\rIf you prefer to view the website in Safari just press \"Safari\"" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: #"Safari",nil];
[alert1 show];
As you can see, my second button is called "Safari", and it is delegated through this code:
-(void) alertView: (UIAlertView *)alert1: clickedButtonAtIndex:(NSInteger)buttonIndex{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://google.com"]];
}
But now if I click Ok (the cancel button) it opens safari, and if I click Safari, it opens safari as well.
if I write:
-(void) alertView: (UIAlertView *)otherButtonTitles Safari: clickedButtonAtIndex:(NSInteger)buttonIndex{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://google.com"]];
}
both buttons cancel.
How can I fix this? the aim is to press "OK" that cancels, and "Safari" to open safari

Handle the alert view delegates with button indexes in the below function that will do,
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(#"Cancel");
}
else
{
NSLog(#"Safari");
}
}

Here's what I did for my project.
alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Error Title", nil) message:authMessage delegate: self cancelButtonTitle: NSLocalizedString(#"Cancel", nil) otherButtonTitles:NSLocalizedString(#"Captcha Required Button", nil), nil];
alert.tag = captchaalert;
I'm setting a tag attribute to the alert in case there are more than one that need to open Safari when you click them.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag == captchaalert){
if(buttonIndex==1){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://accounts.google.com/DisplayUnlockCaptcha"]];
}
}
}
Hope this helps

Related

Dismiss alert view when performing delegate method

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.

iOS alertview action on a button

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);
}

Trying use an AlertView to push to another view controller

I'm having trouble programming an AlertView to push to a new ViewController. I've followed several tutorials and still have not been able to get it function properly. Any help would be appreciated.
- (IBAction)web:(id)sender {
UIAlertView *testAlert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"You are now entering a website outside of the Company App: Any links or references to other Internet sites (hyperlinks) are provided by Company merely as a convenience to users of this App." delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Proceed", nil];
[testAlert show];
[testAlert release];
}
-(void)web:(UIAlertView *)web clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
}
else if (buttonIndex == 1) {
JBWebViewController *jbweb = [[JBWebViewController alloc] initWithNibName:#"JBWebViewController" bundle:nil];
[self.navigationController pushViewController:jbweb animated:YES];
}
}
You should use UIAlertView delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
not
-(void)web:(UIAlertView *)web clickedButtonAtIndex:(NSInteger)buttonIndex
and check self.navigationController is not nil.

App hangs after UIActionSheet button clicked

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

UIAlertView not fading even after the response action is taken

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
}

Resources