Alert view method is not working - iphone

In my app i have reset button which delete all the data of tables and database.but before deleting i have to put alert view and ask the question as follows:
- (IBAction)resetData:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Bills Data Entry"
message:#"Are you sure want to reset data?" delegate:nil
cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
[self databaseOpen];
NSString *deleteData=[NSString stringWithFormat:#"delete from tbl_Bills"];
[database executeQuery:deleteData];
NSLog(#"inert query: %#",deleteData);
NSLog(#"records deleted");
[table reloadData];
[database close];
// DO STUFF
}
}
This method is not called when i click on yes.

UIAlertView's delegate must not be nil if you want to invoke its delegate method.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Bills Data Entry"
message:#"Are you sure want to reset data?" delegate:self
cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];

UIAlertView delegate must be set to self if you want to invoke its delegate method.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Bills Data Entry"
message:#"Are you sure want to reset data?"
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes",nil];

When you are using Delegate method you are supposed to set "delegate: self" instead of nil.
UIAlertView *my_Alert = [[UIAlert alloc] initWithTitle:#"Title of Alert" message:#"Your
Message" delegate:self cancelButtonTitle:#"OK",
otherButtonTitles:nil,nil];

Related

How to dismiss alertview when click on the button in that alertview

I am new to iOS.
I am working on alertviews. Here is my code. Here there are 2 alertviews: successfulallert and unsuccessfulallert for login page. I am using alertview delegate also here, it will work for both alertviews but I want to work only for successful alertview and navigation should be done only for successful alertview. If anybody knows this please help me.
NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
// NSLog(#"string= %#", str);
match = [responseOfResult rangeOfString: #"successful"];
if(match.location == NSNotFound)
{
UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:responseOfResult
delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[unsuccessfulAllert show];
}
else {
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[successfulAllert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
[[self navigationController]pushViewController:registerUserScreen animated:YES];
}
}
Why don't you put "OK" as cancelButtonTitle? Everything will be handled automatically.
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[successfulAllert show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
//POP here with this:
[self.navigationController pushViewController:addItemView animated:NO];
}
}
NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
// NSLog(#"string= %#", str);
match = [responseOfResult rangeOfString: #"successful"];
if(match.location == NSNotFound)
{
UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:responseOfResult
delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[unsuccessfulAllert setTag:1];
[unsuccessfulAllert show];
}
else {
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[successfulAllert setTag:2];
[successfulAllert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag == 2)
{
[[self navigationController]pushViewController:registerUserScreen animated:YES];
}
else
{
//[[self navigationController]pushViewController:registerUserScreen animated:NO];
// OR
return;
}
}
You have many ways to correct your code, the first and very common is to use the tag property (integer) of the UIView. Since UIAlertview inherits from UIView, it has the tag property, so each time you want create an alert (or a view), set the tag and the check your condition like:
...
alert.tag=1;
[alert show];
then to know wich alert is calling the callback:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==theTagOfYourAlert){
//do your stuff
}
}
another way, in your case, could be:
if([alertView.title isEqualToString:#"Alert"]){
//do your stuff
}
}
Add tag to the two alert views and check for tag in alert view delegate.
Sample code:
NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
// NSLog(#"string= %#", str);
match = [responseOfResult rangeOfString: #"successful"];
if(match.location == NSNotFound)
{
UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:responseOfResult
delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[unsuccessfulAllert setTag:1];
[unsuccessfulAllert show];
}
else {
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[successfulAllert setTag:2];
[successfulAllert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==2 && buttonIndex == 0){
[[self navigationController]pushViewController:registerUserScreen animated:YES];
}
Yes the delegate would work for both the alertviews but you can assign a tag to each alertview Object and check for the tag in the delegate and then perform event if the tag for that particular AlertView onject matches.If u need code , i will provide.
For things like Login status updates, you might want to have the "Login Successful" message disappear automatically. Try this instead:
https://github.com/camclendenin/flashbox
This works nicely and comes in handy for situations like this. Plus you don't have to deal with all the clutter involved with UIAlertViews.

use UIAlertView when camera is running in iPhone

I'm trying to trigger an AlertView when my camera detect a face using OpenCV. I manage to do the face detection and can output an NSLog successfully. But when I tried to trigger the alert view with
NSLog(#"Face Detected");
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Face Detected" message:#"Do you really want to try again?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil] autorelease];
[alert addButtonWithTitle:#"Yes"];
[alert show];
[alert release];
I can see the Alert View is kind of triggered as the screen is dimmed but I could never see the alert view came out...
Thanks for helping!
Remove [alert release]. You already called autorelease on it.
Also, you can integrate [alert addButtonWithTitle:#"Yes"]; in the initializer:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Face Detected"
message:#"Do you really want to try again?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil] autorelease];
where are you calling this from ? Main thread or secondary ?
Because UIKit stuff should always been done on main thread.
Code example:
- (void)opencvFaceDetect
{
// stuff before
[self performSelectorOnMainThread: #selector(openAlertView) withObject:nil waitUntilDone:false];
// stuff after
}
and then
- (void)openAlertView
{
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Face Detected"
message:#"Do you really want to try again?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil] autorelease];
}

Detecting which UIAlertView was clicked

i need to pop up alert when my application loaded... I called it didfinished launching..
after clicking ok button need to show another alert message i use clickedButtonAtIndex...
Now when I clicked the ok button its calling again and again.. the alertview..
I need to call only once... what to do?
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the tab bar controller's view to the window and display.
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
viewControllersList = [[NSMutableArray alloc] init];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Alow this app to use your GPS location"
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==0) {
NSLog(#"NO");
}
else {
NSLog(#"Yes");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Do you want's to receive Push messages."
delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
}
#thanks in advance.
set delegate:nil in second alertView
I mean
if (buttonIndex==0) { NSLog(#"NO"); } else {
NSLog(#"Yes");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Do you want's to receive Push messages."
delegate:nil cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
Define each UIAlertView and in the delegate look for which Alert to respond to:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alert1) {
if (buttonIndex==0) {
NSLog(#"NO");
} else {
NSLog(#"Yes");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Do you want's to receive Push messages." delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
}
} else {
/* the second alertview using the same buttonIndex */
}
}
You can also add a tag to your AlertView and te check the tag later on the clickedButtonAtIndex method
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:#"" message:#"All local datawill be erased. Erase local data?" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Erase", #"Cancel",nil];
alert.tag =123; // added the tag so we can prevent other message boxes ok button to mix up
[alert show];
then
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex ==0 && alertView.tag==123){
// Do what ever you wish
}
}
If what you want is to receive the location, just request it and the system will automatically show the message for you. Follow this example:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:tabBarController.view];
// Create a location manager instance to determine if location services are enabled. This manager instance will be
// immediately released afterwards.
CLLocationManager *manager = [[CLLocationManager alloc] init];
if (manager.locationServicesEnabled == NO) {
UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:#"Location Services Disabled" message:#"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[servicesDisabledAlert show];
[servicesDisabledAlert release];
}
[manager release];
}
http://developer.apple.com/library/ios/#samplecode/LocateMe/Listings/Classes_AppDelegate_m.html%23//apple_ref/doc/uid/DTS40007801-Classes_AppDelegate_m-DontLinkElementID_4
Same for push notifications.
though u got many implementation solutions already ...but i think better practice would be to assign a tag to each of your AlertView and before detecting the button tapped check the tag of invoking AlertView.Hope this helps.
#Gerard:Though both location manager and push notification service raise system generated messages ,these can be checked off by users not to be shown again.So better HIG complaint method is application generating message whenever push or location manager are required.

How to change UIAlertView cancel button text

I would like to know that how to change UIAlert's cancel button text during runtime.
- (void)showAlertWithTitle:(NSString*)title message:(NSString*)message cancelButtonTitle:(NSString*)cancelTitle {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:nil];
[alertView show]
[alertView release];
}

How should I properly format this code?

I've a small issue here. I am using an if statement with UIAlertView and I have two situations, both result in UIAlertViews. However, in one situation, I want to dismiss just the UIAlertView, the other, I want the UIAlertView to be dismissed and view to return to root view.
This code describes is:
if([serverOutput isEqualToString:#"login.true"]){
[Alert dismissWithClickedButtonIndex:0 animated:YES];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
UIAlertView *success = [[UIAlertView alloc] initWithTitle:#"Success" message:#"The transaction was a success!"
delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[success show];
[success release];
} else {
UIAlertView *failure = [[UIAlertView alloc] initWithTitle:#"Failure" message:#"The transaction failed. Contact sales operator!"
delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[failure show];
[failure release];
}
}
-(void)alertView: (UIAlertView *)success clickedButtonAtIndex: (NSInteger)buttonIndex{
switch(buttonIndex) {
case 0: {
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
}
So, in both cases, they follow the above action, but obviously, that's not what I want. Any ideas on what I do here?
You will have to differentiate between the 2 uialertview in your clickedButtonAtIndex: method.
Use the tag property to differentiate.
When you create the alerview assign a tag id to them:
UIAlertView *success = [[UIAlertView alloc] initWithTitle:#"Success" message:#"The transaction was a success!" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
success.tag = 1;
[success show];
Similarly,
failure.tag = 2;
Then you switch on the tag ids
switch(alertView.tag){
case 1: //dismiss alertview
case 2: //dismiss alertview and return to root view
}
You could paste your code in Eclipse, and press ctrl+i.