How to run a method from a UIAlertView - iphone

I want to have this method [User logOut]; run when a user touches the Log Me out button in the following UIAlertView.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Logged in!" message:#"Logged in to App!" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:#"Log me out", nil];
[alert show];
[alert release];
how would I go about doing that? The "Okay" works for the cancel button. I just need the other button/method to work.
thanks for any help

You can do something like;
- (void)AlertConfirm
{
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:#"Confirm"];
[alert setMessage:#"Log out?"];
[alert setDelegate:self];
[alert addButtonWithTitle:#"Yes"];
[alert addButtonWithTitle:#"No"];
[alert show];
[alert release];
}
And
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
// Yes, do something
}
else if (buttonIndex == 1)
{
// No
}
}
Good luck,
Nathan

You need to use the UIAlertView delegate method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
Make sure you set yourself as the delegate of your alertView.

Set the alert view's delegate to be self, and in your .h add
The following delegate method will be called when a button is pressed:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
Then you can see what button was pressed by doing something like:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if ((alertView == alert) && (buttonIndex == 0))
NSLog(#"alert's \"Okay\" button was pressed");
else
NSLog(#"alert's \"Log Out" button was pressed");
}

Set your view controller to be the delegate of the UIAlertView:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Logged in!" message:#"Logged in to App!" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:#"Log me out", nil];
alert.delegate = self;
[alert show];
[alert release];
Then handle in the delegate callback:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == alertView.cancelButtonIndex) {
// Cancelled
return;
}
// Log them out
}
Note the test (buttonIndex == alertView.cancelButtonIndex). This is better form than checking an absolute value for the button index.

Related

UIAlertView buttons don't work

I'm using an alert view to alert the user when the internet is not connected. I have two buttons in the alert view and they both don't seem to work. I already implemented the in the .h file. I used NSLog to check whether it responds when I click. It responds in the console, but does nothing when the button is pressed. Here's a snippet of my code.
- (IBAction)startButton:(id)sender
{
if (![self connectedToNetwork])
{
UIAlertView *internetAlert = [[UIAlertView alloc] initWithTitle: #"Network Error!" message: #"You are not connected to the internet" delegate: self cancelButtonTitle: #"OK" otherButtonTitles: #"Open Settings", nil];
[internetAlert show];
}
}
- (void)alertView: (UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(#"Ok clicked");
HomeViewController *blah = [self.storyboard instantiateViewControllerWithIdentifier:#"HomeViewController"];
[self.view addSubview: blah.view];
}
else if (buttonIndex == 1)
{
NSLog(#"Settings clicked");
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:#"prefs:root=WIFI"]];
}
}
I used [self.contentView addSubview: blah.view] because I do not have a navigation controllers. Any thoughts on what I'm doing wrong
Here you are using wrong method for UIAlertView button clicked. you have to use another method of UIAlertView.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Please change to the following code and it will work.
- (IBAction)startButton:(id)sender
{
if (![self connectedToNetwork])
{
UIAlertView *internetAlert = [[UIAlertView alloc] initWithTitle: #"Network Error!" message: #"You are not connected to the internet" delegate: self cancelButtonTitle: #"OK" otherButtonTitles: #"Open Settings", nil];
[internetAlert show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(#"Ok clicked");
HomeViewController *blah = [self.storyboard instantiateViewControllerWithIdentifier:#"HomeViewController"];
[self.view addSubview: blah.view];
}
else if (buttonIndex == 1)
{
NSLog(#"Settings clicked");
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:#"prefs:root=WIFI"]];
}
}
Please let me know if you still have any questions.

How to return keyboard for UITextField in UIAlertview in iphone?

I have one UIAlertView containing one UITextField with ok and cancel button. When I click on ok button i am calling webService. I want to return keyboard before calling webservice. Keyboard is not returning till response is not coming from webservice. Because of that half screen not visible to user during loading webservice. Here is my code which i did.
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:#"Forgot Password" message:#" " delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
// Adds a username Field
alertEmailtextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 29.0)];
alertEmailtextfield.layer.cornerRadius = 07.0f;
[alertEmailtextfield setBorderStyle:UITextBorderStyleRoundedRect];
alertEmailtextfield.placeholder = #"Enter Email";
[alertEmailtextfield setBackgroundColor:[UIColor whiteColor]];
alertEmailtextfield.autocapitalizationType =UITextAutocapitalizationTypeNone; //For Making Caps Off
[alertview addSubview:alertEmailtextfield];
[alertview show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
if ([alertEmailtextfield.text length] != 0) {
if (![self validEmail:alertEmailtextfield.text]) {
[AlertView UIAlertView:#"Enter email is not correct"];
}else{
//[alertEmailtextfield resignFirstResponder];
[NSThread detachNewThreadSelector:#selector(startSpinner) toTarget:self withObject:nil];
Boolean isForgotPassword = [serverConnection forgotPassword:alertEmailtextfield.text];
if (isForgotPassword) {
NSLog(#"Mail is send");
[AlertView UIAlertView:#"New password is send to your mail"];
[spinner stopAnimating];
}else{
[AlertView UIAlertView:#"User does not exist"];
[spinner stopAnimating];
}
}
}else{
[AlertView UIAlertView:#"Text Field should not be empty"];
}
}
}
Please if any one knows how to return keyboard in UIAlertView's UITextField, help me.
Your UI is stuck waiting for your webservice to complete. Call your webservice in a background thread your problem will be solved.
[self performSelectorInbackgroundThread:#selector(yourWebservice)];
Might be u are doing both task (webservice call and keyboard resinging) in same main thread so untill ur data is not loading from server keyboard is not resign from view. call weservice method in background thread like.
-(void)alertView:(CustomAlert *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[textField resignFirstResponder];
UPdate
[self performSelectorInBackground:#selector(yourWebservice) withObject:nil];
}

Creating two action sheets in one view

I created two action sheets in one view. There are two buttons, each will initiate one action sheet.
The problem: when i press on first choice in both action sheets the same action is triggered.
Here's my code:
-(IBAction) ChangeArrow:(id)sender{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Change Arrow"
delegate:self
cancelButtonTitle:#"cancel"
destructiveButtonTitle:#"Red"
otherButtonTitles:#"Blue",#"Black",nil];
[actionSheet showInView:self.view];
[actionSheet release];}
- (void) actionSheet: (UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex ==[actionSheet destructiveButtonIndex]) {
self.bar.image=[UIImage imageNamed:#"red"];
}
else if(buttonIndex == 1){
self.bar.image=[UIImage imageNamed:#"blue"];
}
else if(buttonIndex == 2){
self.bar.image=[UIImage imageNamed:#"dark"];}
}
//Second Action sheet:
-(IBAction) Background:(id)sender{
UIActionSheet *actionSheet2 = [[UIActionSheet alloc] initWithTitle:#"Change Background"
delegate:self
cancelButtonTitle:#"cancel"
destructiveButtonTitle:#"Sky"
otherButtonTitles:#"Thumbs",#"Smiley",nil];
[actionSheet2 showInView:self.view];
[actionSheet2 release];
}
- (void) actionSheet2: (UIActionSheet *)actionSheet2 didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex ==[actionSheet2 destructiveButtonIndex]) {
self.background.image=[UIImage imageNamed:#"sky"];
}
else if(buttonIndex == 1){
self.background.image=[UIImage imageNamed:#"thumbs"];
}
else if(buttonIndex == 2){
self.background.image=[UIImage imageNamed:#"smiley"];}
}
Set the tag property on each actionsheet to a different value. Then you can check sender.tag to see which action sheet called your method.
Ex.
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Change Arrow"
delegate:self
cancelButtonTitle:#"cancel"
destructiveButtonTitle:#"Red"
otherButtonTitles:#"Blue",#"Black",nil];
actionSheet.tag = 1;
UIActionSheet *actionSheet2 = [[UIActionSheet alloc] initWithTitle:#"Change Arrow"
delegate:self
cancelButtonTitle:#"cancel"
destructiveButtonTitle:#"Red"
otherButtonTitles:#"Blue",#"Black",nil];
actionSheet2.tag = 2;
Then
- (void) actionSheet: (UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
if(actionSheet.tag == 1) {
//do something
} else if(actionSheet.tag == 2) {
//do something else
}
}

How can I dismiss UIAlertView?

How can I dismiss UIAlertView ? This code doesn't work.
#property (nonatomic, retain) UIAlertView *activityAlertView;
- (void)viewDidLoad
{
self.activityAlertView = [[UIAlertView alloc] initWithTitle:#"Receiving data" message:#"\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil, nil];
[activityAlertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
}
-(void) myfunc
{
[self alertView:activityAlertView clickedButtonAtIndex:1];
}
The - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated method of the UIAlertView class does what you want. eg:
[myAlertView dismissWithClickedButtonIndex:-1 animated:YES];
Since you are using a delegate callback method of UIAlertView, I think it is better you use the following code
[myAlertView dismissWithClickedButtonIndex:0 animated:YES];
if not then use the above suggested code
[myAlertView dismissWithClickedButtonIndex:-1 animated:YES];

Handling alert buttons when your view has more than 1 alert

-(void)alertOKCancelAction
{
// open a alert with an OK and cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Return Home" message:#"Are
you sure you want to return to the menu?" delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
}
-(void)alertConnectionLost
{
// open a alert with an OK and cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Connection Lost"
message:#"The connection to the other device has been lost" delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
}
else
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
}
As you can see above, I have 2 alerts. But they both call the same method to handle there key presses. How can I tell which alert is currently alive and respond to the keypresses differently depending on which alert is up?
Use: [alert setTag:1]; and [alert setTag:2]; respectively
then you can do:
if([actionSheet tag] == 1){
//do thing for first alert view
}
else if([actionSheet tag] == 2){
//do something for second alert view
}