Two Alertviews on one view [duplicate] - objective-c

This question already has answers here:
Two Alert Views in One View Controller - buttonIndex Response
(4 answers)
Closed 7 years ago.
EDIT:
Problem Solved ==> Simply giving a tag solved the problem.
I have the following problem:
On a view I have two UIalertviews:
NSString *message = [NSString stringWithFormat:#"Users must enter this code to join the meeting: %#", meetingCode];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Meeting code"
message:message
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:#"Copy to clipboard", nil];
[alert show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == [alertView cancelButtonIndex])
{
NSLog(#"Code not copied");
}
else
{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = meetingCode;
NSLog(#"Code copied");
}
}
and this one:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
AgendaModel* agenda = _meeting.agenda[indexPath.row] ;
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:agenda.id,#"id",agenda.name,#"name", nil];
NSString *message = [NSString stringWithFormat:#"Are you sure that you want to delete : %#?", agenda.name];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning"
message:message
delegate:self
cancelButtonTitle:#"Close"
otherButtonTitles:#"Delete", nil];
[alert show];
NSString *delete_url = [NSString stringWithFormat:#"RestAgendas/delete.json"];
[_meeting.agenda removeObject:agenda];
[self.tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[JSONAPI getWithPath:delete_url andParams:dict completion:^(id json, JSONModelError *err) {
NSLog(#"%#", json);
}];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == [alertView cancelButtonIndex])
{
NSLog(#"Agenda Won't Be Deleted");
}
else
{
NSLog(#"Agenda Will Be Deleted");
}
}
Now is the problem that I got the error: Duplicate declaration of method 'alertView:clickedButtonAtIndex'
How can I fix this? I tried some stuff I found here but I still can't make it work. Can someone help me?

Yep, like you said, giving a tag allows you to have multiple UIAlertView in one single view.
Same goes for UIActionSheet or UIAlertController
for future reference, even though this is probably a duplicate, simply create your alert like usual and add
myAlert.tag = 123; //any number of your choice
and in alertView:clickedButtonAtIndex
you can find it using a switch or some if's
if (alertview.tag = 123){
// this is my alert
}else if(alertview.tag = 333){
// this is my other alert
}
For what it's worth, I suggest using else if and not just if to avoid the whole method to be read everytime, and order your if's by decreasing likeliness of being called.
Note you can also give tags to buttons, views, cells, labels, and just pretty much every outlet you can find.

Related

Prevent from UIAlertView to repeat when click on tableViewCell

I'm kinda stuck with a stupid problem, in my app when a user press on a TableViewCell he can change some value in the server, before i am sending the information to server i want to show UIAlertView to the user to make sure he wants to do that.
so in didSelectRowAtIndexPath:
i wrote this
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Caution!" message:#"bla bla " delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[alert show];
then its calls the UIAlertView method clickedButtonAtIndex:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
NSString *url = #"some url";
NSDictionary *paramerter = #{#"parameter":parameter};
[self.jsonHandler startParseWithParameters:paramerter andUrlAdress:url withCompletion:^(int errValue) {
if (errValue == 1) {
NSLog(#"ERROR");
}else if (errValue == 0){
[self AlertViewWithTitle:#"success" andMessage:#"success"];
}
}];
}
}
the problem is that the if (buttonIndex == 0) called for every cell and change them all!
and i get UIAlertView message for every cell in the tableView.
how can i prevent it?
EDIT
Ok the problem caused because i use the same buttonIndex for both UIAlertViews
i solved it using this:
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"buttonString"]){
//Do what ever you need
}
hope someone find it helpful.

Errors with Alert Views

I have an error when I run my code: It says undeclared identifier of 'alertView' for this line of code:
-(void)alertView:(UIAlertView* )alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)
else if(butonIndex==1)
}
Thank you for your help.
It goes something like the following.
- (void)dismissPop2:(NSString *)projectname {
projectdelete = projectname;
NSString *msg = [NSString stringWithFormat:#"Are you sure you want to delete this project (%#)?",projectname];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"- Project deletion -"
message:msg
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Delete", nil];
[alert setTag:100];
[alert show];
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView { // Validation
if ([alertView tag] == 100) {
return YES;
}
else {
return YES;
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 100) {
// Deleting a project
if (buttonIndex == 1) {
// Do something since the user has tapped YES
}
}
If it doesn't help, then you should show more work.
You likely have a syntax error earlier in your code that is breaking the parser. Check to make sure the function above the one you posted is closed with a }, all [] are matched, and statements end with a ;
If you post a bit more, someone could probably point it out, but I'm sure you can find it by reading your code.

The iOS app crashed right after clicking the button on a UIAlertview

I tried to dial a number with the phone app after the user click on a button on a UIAlertview. The phone app did open, but the original app crashed right after clicking the button on the UIAlertview. Does anyone one know the reason? I did try to make sure I released everything that should be released. Thanks! Below is the code:
-(IBAction)dialButtonPressed:(UIButton *)numberButton
{
if ([company isEqualToString:#"Not Found"]==true){
message = [[UIAlertView alloc] initWithTitle:#"Sorry"
message:#"No replace number found. Would you like to dial anyway?"
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes", nil];
message.tag = 0;
if(phoneLinkString)
{
[phoneLinkString release];
phoneLinkString = nil;
}
[message show];
[message autorelease];
phoneLinkString = [[NSString stringWithFormat:#"tel:%#",replace]retain];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
[self release];
message = nil;
if(message.tag == 0 && buttonIndex == 1){
NSURL *phoneLinkURL = [NSURL URLWithString:phoneLinkString];
[[UIApplication sharedApplication] openURL:phoneLinkURL];
}
- (void)dealloc {
[phoneNumberString release];
[phoneNumberLabel release];
[self release];
[message release];
[super dealloc];
}
The newest code
-(IBAction)dialButtonPressed:(UIButton *)numberButton
{
if ([company isEqualToString:#"Not Found"]==true){
message = [[UIAlertView alloc] initWithTitle:#"Sorry"
message:#"No replace number found. Would you like to dial anyway?"
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes", nil];
message.tag = 1;
if(phoneLinkString)
{
[phoneLinkString release];
phoneLinkString = nil;
}
[message show];
[message autorelease];
phoneLinkString = [[NSString stringWithFormat:#"tel:%#",replace]retain];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(message.tag == 1 && buttonIndex == 1){
NSURL *phoneLinkURL = [NSURL URLWithString:phoneLinkString];
[[UIApplication sharedApplication] openURL:phoneLinkURL];
message = nil;
}
}
- (void)dealloc {
[phoneNumberString release];
[phoneNumberLabel release];
[super dealloc];
}
but it still crashed after clicking the button on the UIAlertview. The error is 0x3beb85b0: ldr r3, [r4, #8] EXC_BAD_ACCESS (code=1, address=0x7269634f) Any help would be appreciated. Thanks!
The problem might be that you set your alert to nil before your if statement. Try putting it after.
The crash is happening due to this code. [self release];.
When you call self release the view in which the alert is displayed will released and deallocated, not the alertView. That's the cause of crash.
You are already releasing the alertViews memory in the dialButtonPressed: method using [message autorelease];
So no need to release the alertView again in clickedButtonAtIndex. So change the method like:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if(alertView.tag == 0 && buttonIndex == 1)
{
NSURL *phoneLinkURL = [NSURL URLWithString:phoneLinkString];
[[UIApplication sharedApplication] openURL:phoneLinkURL];
}
message = nil;
}
Your crash is being caused by poor memory management. The primary issue is calling [self release]. It's a pretty rare case that this is appropriate.
Another issue is your attempt to check the message.tag right after setting message to nil. Calling the tag property on a nil object will always result in a value of 0.
Your dealloc method is all wrong. Don't call [self release]. Don't call [message release] since you autoreleased it when you showed it.
BTW - never use a tag of 0. This is the default. If you want to use the tag, always use a non-zero value so you can distinguish the value from the default.

UIAlertView EXC_BAD_ACCESS error

I cannot figure out why I keep getting an EXC_BAD_ACCESS error on the line below in the action sheet method:
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:numberFinal]];
I added my code, but I just don't see why its being over released.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
number = [[results objectAtIndex:indexPath.row]objectForKey:#"phone"];
number = [number stringByReplacingOccurrencesOfString:#"-" withString:#""];
numberFinal = [NSString stringWithFormat:#"tel:%#",number];
//tel:1234567890
NSLog(#"NUMBER:%#",numberFinal);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UIAlertView *alert2 = [[[UIAlertView alloc]initWithTitle:#"Call" message:#"Call This Person?" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil]autorelease];
alert2.tag = kAlertViewTwo;
[alert2 show];
// [alert2 release];
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if(actionSheet.tag == kAlertViewOne) {
if (buttonIndex == 0)
{
}else{
}
}
else if(actionSheet.tag == kAlertViewTwo) {
if (buttonIndex == 0)
{
//ok button clicked - close alert
}
else
{
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:numberFinal]];
}
}
}
Try do an NSLog before the line and print numberFinal to see if it allocated ok.
Make sure your class is a UIAlertView Delegate
Try the same line of code in another function to see if it causes the same issue. It might be because you are accessing sharedApplication from within the UIAlertView click event.
Hope that helps :)

Two UIAlertView consecutively in didFinishLaunchingWithOptions

I want two alert views to show up only when the user opens my application for the first time -- the second to appear after the first is dismissed. I have it set up to only show the UIAlertViews when it has not been shown before and I do not need help with this. I need help figuring out how to display two alert views in a row when this is the case.
-(void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex does not work for me.
Here is the code I have -- remember this is in didFinishLaunchingWithOptions:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL didFirstLaunch = [defaults boolForKey:#"DidFirstLaunch"];
if (!didFirstLaunch) {
[defaults setBool:YES forKey:#"DidFirstLaunch"];
UIAlertView *successAlert = //not important
[successAlert show];
[successAlert release];
//Somehow show second alert after the first is dismissed
}
I'm gonna post a very simple solution using GCD & blocks (GCD part is just in case the alert view is created on another thread then the main thread, callback should be safe to perform on the main thread). Remember, I just coded this in like 5 mins, so you definitely should work on improving the code. One thing that's a bit ugly is the delegate parameter that is overridden in my subclass. The interface of the subclass could be changed a bit to make it more obvious of what happens ...
Anyway, here goes ...
First create a subclass of UIAlertView, make it look somewhat like the following ...
#interface FSAlertView () <UIAlertViewDelegate>
#property (nonatomic, copy) void (^dismissHandler)(NSInteger buttonIndex);
#end
#implementation FSAlertView
#synthesize dismissHandler = _dismissHandler;
- (void)showWithDismissHandler:(void (^)(NSInteger buttonIndex))dismissHandler
{
self.dismissHandler = dismissHandler;
self.delegate = self;
[self show];
}
// Alert view delegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
dispatch_async(dispatch_get_main_queue(), ^ {
if (_dismissHandler)
{
_dismissHandler(buttonIndex);
}
});
}
Now in the app we can create alert views like the following ...
FSAlertView *alert1 = [[FSAlertView alloc] initWithTitle:#"Alert 1"
message:#"Some message"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Show 2nd Alert", nil];
[alert1 showWithDismissHandler:^ (NSInteger buttonIndex) {
NSLog(#"button pressed: %d", buttonIndex);
if (buttonIndex == 1)
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Alert 2"
message:#"Hi!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert2 show];
}
}];
If i understand your question correctly , then this may help:
UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:#"Alert 1" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[firstAlert show];
[self performSelector:#selector(test:) withObject:firstAlert afterDelay:2];
[firstAlert release];
UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:#"Alert 2" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[secondAlert show];
[self performSelector:#selector(test:) withObject:secondAlert afterDelay:2];
[secondAlert release];
-(void)test:(UIAlertView*)alert{
[alert dismissWithClickedButtonIndex:-1 animated:YES];
}
This will show two alert views one after the other.
NOTE: I am not sure if you are dismissing the alerts with cancel button so i am dismissing them automatically after few seconds.
Try this:
UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[firstAlert setTag:444];
[firstAlert show];
firstAlert = nil;
AlertView Delegate Method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
switch (alertView.tag) {
case 444:
{
//Cancel ButtonIndex = 0
if (buttonIndex == 1) {
UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:#"Title 2" message:#"Message2" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Dismiss", nil];
[secondAlert setTag:555];
[secondAlert show];
secondAlert = nil;
}
}
break;
case 555:
{
if (buttonIndex == 1) {
NSLog(#"Code Here");
}
}
break;
}
}