Alert with 2 buttons - iphone

I'm going to have a link to a website in my app. The user will click on a button that says Website and an Alert will appear with 2 buttons. One of the buttons is just going to be a cancel button and the other button is going to open the website.
Could you help me with this?
Thanks!

put this into your header file:
#interface YourViewController : UIViewController <UIAlertViewDelegate>
put this into the class with your alert:
- (void)alertOKCancelAction {
// open a alert with an OK and cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Open?" message:#"Open Website?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Open", nil];
alert.tag = 1;
[alert show];
[alert release];
}
add this method:
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
// the user clicked one of the OK/Cancel buttons
if(alert.tag == 1)
{
if(buttonIndex == alert.cancelButtonIndex)
{
NSLog(#"cancel");
}
else
{
NSLog(#"ok");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://www.google.com"]];
}
}
}

Related

Show UIAlertView after UIActionSheet button pressed

After the user clicks a button I want an action sheet to come up asking for them to choose between two options. Once they have selected an option, I want an AlertView to come up telling them they will leave the application and have them choose to cancel to operation or continue to the other application.
Code is as follows:
.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface mapView : UIViewController <MKMapViewDelegate, UIActionSheetDelegate, UIAlertViewDelegate>
#property (nonatomic, weak)IBOutlet MKMapView *mapView;
#property (nonatomic, weak)IBOutlet UIBarButtonItem *getDirections;
- (IBAction)selectDestination:(id)sender;
- (void)checkLeave;
#end
.m
- (IBAction)selectDestination:(id)sender
{
UIActionSheet *selectDestinationAS = [[UIActionSheet alloc] initWithTitle:#"Select Destination: " delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Destination 1", #"Destination 2", nil];
[selectDestinationAS showInView:self.view];
}
- (void)checkLeave
{
UIAlertView *checkLeaveAlert = [[UIAlertView alloc] initWithTitle:#"Leave CDSI?" message:#"This will open the Maps application to continue directions. Are you sure you want to continue?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Open", nil];
[checkLeaveAlert show];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self checkLeave];
if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString: #"Destination 1"]) {
NSURL *BOHMDirections = [NSURL URLWithString:#"http://maps.apple.com/?daddr=Destination1&saddr=Current+Location"];
[[UIApplication sharedApplication] openURL:BOHMDirections];
} else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString: #"Destination 2"]) {
NSURL *BOPDirections = [NSURL URLWithString:#"http://maps.apple.com/?daddr=Destination2&saddr=Current+Location"];
[[UIApplication sharedApplication] openURL:BOPDirections];
}
}
The ActionSheet shows up, when an option is selected the Maps app opens (as desired) but the AlertView shows up only after you reenter the original app. How do I get it to show up before I leave the app?
Navigate to the external app on UIAlertView delegate.
To pass the selected item index at UIActionSheet, pass the index as parameter in checkLeave method and set as tag to the UIAlertView
By this way, the UI execution will be, on ActionSheet Clicked, the alertview ask confirmation with user. Once the user confirms, the navigation will be performed based on the action sheet selection. To hold, the action sheet selection, we are passing that data as tag.
If you need, you can add a private property to hold the item data clicked and access it in UIAlertViewDelegate.
- (void)checkLeave :(NSInteger)index
{
UIAlertView *checkLeaveAlert = [[UIAlertView alloc] initWithTitle:#"Leave CDSI?" message:#"This will open the Maps application to continue directions. Are you sure you want to continue?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Open", nil];
[checkLeaveAlert setTag:index];
[checkLeaveAlert show];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self checkLeave : buttonIndex];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 1){
//Destination 1 clicked
}
}
Similar to the action sheet, UIAlertView has a delegate protocol that you should implement/comply with. Especially the method alertView:didDismissWithButtonIndex: is of interest.
Call the Maps app there and not in clickedButtonAtIndex

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

Multiple alertviews in a single view?

I have an Iphone application in which when i am pressing a button it shows an alertview to chose the background.whichever background user is chosing will be played as the background of the audio clips.But now i need to add another alert before i am showing this alert for giving some warning.after that only i need to pop the second one.but i was done that chosing alert in the didappear of that viewcontroller and set it as a Uialertview delegate.and on the button actions i was doing different actions.Can anybody help me on achieving this?
proAlertView *loginav1=[[proAlertView alloc] initWithTitle:#"title" message:#"Choose a Background to play with this program?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Field",#"Beach", #"Stars",nil];
[loginav1 setBackgroundColor:[UIColor colorWithRed:0.129 green:0.129 blue:0.129 alpha:1.0] withStrokeColor:[UIColor colorWithHue:0.625 saturation:0.0 brightness:0.8 alpha:0.8]];
[loginav1 show];
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
//[self play];
//moviePlayer.scalingMode=MPMovieScalingModeAspectFill;
if(actionSheet.tag==123)
{
[self backButtonPressed];
}
}
else if (buttonIndex == 1)
{
videoFile = [[NSBundle mainBundle] pathForResource:#"video-track" ofType:#"mp4"];
[self play];
moviePlayer.scalingMode=MPMovieScalingModeAspectFill;
}
how can i include another alert before this is my question?
Initialize first Alertview
UIAlertView *al1 = [[UIAlertView alloc] initWithTitle:#"Warning!" message:#"Warning Msg!!!" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
al1.tag=1;
al1.delegate=self;
[al1 show];
Implement Delegate method
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==1){
// implement button events for first Alertview
if(buttonIndex==1){
//First button clicked of first Alertview
UIAlertView *al2 = [[UIAlertView alloc] initWithTitle:#"Choose BG" message:#"Choose BG?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"1",#"2",#"3", nil];
al2.tag=2;
al2.delegate=self;
[al2 show];
}
}
if(alertView.tag==2){
// implement button events for second Alertview
if(buttonIndex==1){
// First button clicked second Alertview.
}
}
}
Controller Class header
#interface ViewController : UIViewController<UIAlertViewDelegate>{
}
Hope this will fulfill your need !
You can do like this, first display warning message in alertview and when user click OK in alertview then in alertview delegate method write code to display second alertview where user can choose background.

Alert message for the button click in iphone sdk 4.3

I am beginner to xcode programming.Please tell me how to display the alert message when we are going to click the button in xcode-iphone-4.3
My Code is as follows,
- (IBAction)buttonPressed:(id)sender{
UIAlertView* mes=[[UIAlertView alloc] initWithTitle:#"Hello World!!!!!!"
message:#"This is the Iphone app" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[mes show];
[mes release];
Please help me regarding this.
-(IBAction)buttonOnePressed:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Clicked button 1"
message: #"Alert Message here"
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
[alert setTag:1];
[alert show];
}
-(IBAction)buttonTwoPressed:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Clicked button 2"
message: #"Alert Message here"
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
[alert setTag:2];
[alert show];
}
Below is the delegate method to track which button on Alertview is hit.
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1) { // UIAlertView with tag 1 detected
if (buttonIndex == 0)
{
NSLog(#"user pressed Button Indexed 0");
// Any action can be performed here
}
else
{
NSLog(#"user pressed Button Indexed 1");
// Any action can be performed here
}
}
else if (alertView.tag == 2) { // UIAlertView with tag 2 detected
if (buttonIndex == 0)
{
NSLog(#"user pressed Button Indexed 0");
// Any action can be performed here
}
else
{
NSLog(#"user pressed Button Indexed 1");
// Any action can be performed here
}
}
}
You can set tag to UIAlertView in case you have more than one UIAlertViews and can determine which UIAlertView button is clicked in its delegate method clickedButtonAtIndex using its respective tag.
In IBAction you have to write the code and give the Connections to the Button
Create the IBAction for your button and add the code for alert view in that method.

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;