iOS - alertView shouldEnableFirstButton - ios

I want the user to not be able to click the cancel button on UIAlertView, yet I still want it to be there, but shaded out. I know a shouldEnableFirstButton function is there but it doesn't involve cancel button. Do you have any idea how to do it?
Thanks.

Use the following:
UIAlertView *vw = [[UIAlertView alloc] initWithTitle:#"Information" message:#"This is a message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Cancel", nil];
[vw show];
So, essentially you are setting the "Cancel" as other button.
Now, the following will solve your problem:
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
return NO;
}
If you want to perform some action in OK button press you can always do that here:
- (void)alertViewCancel:(UIAlertView *)alertView

Related

textFieldIndex (0) is outside of the bounds of the array of text fields

I have searched far and wide for an answer but I haven't found anything close.
The following code:
UIAlertView *welcome = [[UIAlertView alloc] initWithTitle:#"Welcome"
message:#"Please Enter Your Credentials to Proceed"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[welcome show];
Gives the following error:
textFieldIndex (0) is outside of the bounds of the array of text fields
If I change the alertView type to UIAlertViewStylePlainTextInput then it works and I cant figure out why!
Any help would be appreciated.
Somewhere you are calling -textFieldAtIndex: on an alertView object that does not or is not meant to have a textField.
Check for things like:
[welcome textFieldAtIndex:0];
or...
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//...
[alertView textFieldAtIndex:0];
//...
}
This could be if you're using multiple alertView objects and haven't handled done case-handling to bifurcate the implementation for an alertView object that has a textField and another alertView that doesn't have a textField

UIAlertView dismissal in utility class

In my application i want alertview in many views.So what i did is just wrote a single alertview in a utility class and use it everywhere.This is working fine.
I even tried by setting <UIAlertViewDelegate> but in vain.
Utility Class
#interface SSUtility: NSObject<UIAlertViewDelegate> {
}
+(void)showAllert;
#end
#implementation SSUtility
+(void)showAllert{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"gotoappAppstore",#"") message:#"" delegate:self cancelButtonTitle:NSLocalizedString(#"Ok",#"") otherButtonTitles:nil];
[alert show];
[alert release];
}
#end
Now from my view
-(void)pressButton{
[SSutility showAllert]
}
Now i want to give a button action for alert view click and call a method on that button action.
So im stuck with,in which class i want to implement this method.I tried it in utility class and viewc controller but the method is not getting triggered when "ok" button is pressed.
-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
Can anyone please help me on this?
Thanks in advance.
You wire the alert view button response method by setting your alert view object delegate usually to the owner object and implementing the – alertView:clickedButtonAtIndex: method.
You need 4 parts in your code:
instantiate your UIAlertView object
send show message to your UIAlertView object
set delegate
implement the delegate method
Example:
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"myTitle" message:#"myMessage" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitle:#"Another button"];
[myAlertView setDelegate:self];
[myAlertView show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) //index 0 is cancel, I believe
{
// code for handling cancel tap in your alert view
}
else if (buttonIndex == 1)
{
// code for handling button with index 1
}
}
I would recommend you get more familiar with how delegates work. This'll come back again a lot.
You set delegate:nil in your UIAlertView's init.
You should set to delegate:self, like this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"gotoappAppstore",#"") message:#"" delegate:self cancelButtonTitle:NSLocalizedString(#"Ok",#"") otherButtonTitles:nil];
in order to use the delegate in the same class (a.k.a. self).
As a sidenote, if you use Automatic Reference Counting (ARC), you do not need [alert release] (your Xcode compiler should warn you about this)

UIAlertView Shows two times

Hope you are all doing good.
Where is my problem is concern one UIAlertView shows twice while executing the code. I have written it within -(void)viewDidAppear:(BOOL)animated as shown following
-(void)viewDidAppear:(BOOL)animated
{
//Finding city Name from coordinate
UIAlertView * alertForCoordinate=[[UIAlertView alloc]initWithTitle:#"Coordinate" message:[NSString stringWithFormat:#"latitude==>%f\nlongitude==>%f",self.userLocation.coordinate.latitude,self.userLocation.coordinate.longitude] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
[alertForCoordinate show];
[SVGeocoder reverseGeocode:CLLocationCoordinate2DMake(self.userLocation.coordinate.latitude, self.userLocation.coordinate.longitude)
completion:^(NSArray *placemarks, NSHTTPURLResponse *urlResponse, NSError *error)
{
if (placemarks.count>0)
{
CLPlacemark * placemark=[placemarks objectAtIndex:0];
currentLocationUpcoming =[placemark locality];
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:#"Location" message:[NSString stringWithFormat:#"currentLocation==>%#",currentLocationUpcoming] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[self.eventsTableView reloadData];
}
}];
}
As view appears First UIAlertView gets call and dismiss automatically then second gets call When I tap OK button of second alert and again first UIAlertView appears . I am struggling with it since last 4 hours and some of the answer put here also not intended the context in which I am working. Can anybody suggest me where I am wrong.
Thanks
You don't actually have the first UIAlertView showing twice - the animation just makes it look like this because you are showing two UIAlertViews in a row. You're probably seeing Alert 1-> Alert 2-> tap OK on Alert 2 -> Alert 1. This is because the UIAlertViews stack up if multiple are shown.
Think of it this way: Alert 1 shows first but animates for only a very brief period until it is interrupted by Alert 2 showing. Dismissing Alert 2 animates Alert 1 onscreen again, and even though it is still the same alert, it looks like it is showing twice.
Try this simple test case:
-(void)viewDidAppear:(BOOL)animated
{
[[[UIAlertView alloc] initWithTitle:#"Alert #1" message: delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
[[[UIAlertView alloc] initWithTitle:#"Alert #2" message: delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
}
You will see "Alert #1" for a fraction of a second, then "Alert #2" will remain onscreen. Tapping OK on "Alert #2" will show "Alert #1" again because it was below Alert #2 on the stack.
Your second alert overlaping the first one, and after dismissing second alert, the first one reappears on screen.
-(void)viewDidAppear:(BOOL)animated
{
UIAlertView * alertForCoordinate=[[UIAlertView alloc]initWithTitle:#"Coordinate" message:[NSString stringWithFormat:#"latitude==>%f\nlongitude==>%f",self.userLocation.coordinate.latitude,self.userLocation.coordinate.longitude] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
alertForCoordinate.tag = 1;
[alertForCoordinate show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0 && alertView.tag == 1) {
[SVGeocoder reverseGeocode:CLLocationCoordinate2DMake(self.userLocation.coordinate.latitude, self.userLocation.coordinate.longitude)
completion:^(NSArray *placemarks, NSHTTPURLResponse *urlResponse, NSError *error)
{
if (placemarks.count>0)
{
CLPlacemark * placemark=[placemarks objectAtIndex:0];
currentLocationUpcoming =[placemark locality];
/*****************Second Alert******************/
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:#"Location" message:[NSString stringWithFormat:#"currentLocation==>%#",currentLocationUpcoming] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[self.eventsTableView reloadData];
}
}];
}
}
i had same issue "UIAlertView Shows two times", but in my case it was because of coding error with long press gesture recognizer (different than original question, but may be useful for other folks that google uialertview showing twice). inside method to handle long press, i called method to show UIAlertView. my mistake was not recalling that handle long press method gets called twice each time user long presses (once for press and hold down, once when user releases their finger). the correct code recognizes gesture state (code below).
-(IBAction)handleHoldOnContact:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateBegan){
// NSLog(#"UIGestureRecognizerStateBegan hold");
//Do Whatever You want on Began of Gesture
// ...
} else if (sender.state == UIGestureRecognizerStateEnded) {
// NSLog(#"UIGestureRecognizerStateEnded release");
//Do Whatever You want on End of Gesture
// ...
}
}
I too get the same issue. To overcome this problem, i done the following things. Get the alertview as global variable. And then,
if(!alertForCoordinate){
alertForCoordinate=[[UIAlertView alloc]initWithTitle:#"Coordinate" message:[NSString stringWithFormat:#"latitude==>%f\nlongitude==>%f",self.userLocation.coordinate.latitude,self.userLocation.coordinate.longitude] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
[alertForCoordinate show];
}
After that, when you clicking "OK" button from the Alertview. set that to nil. Like the following,
alertForCoordinate = nil;
Hope, this will help you.
It could be a case of duplicating a button, and therefore it's IBaction references, in main.storyboard.
I had a button that was styled perfectly in main.storyboard, so I alt drag duplicated it and attached a new IBaction to the viewController. I didn't realize it but when I tapped that new button it fired the IBaction for both the first button and the second button, because it was hooked up to both, as it was a duplicate of the first.
I right clicked the second button in main.storyboard, saw the reference to the IBaction from the first button AND the second, then deleted the reference to the first. Problem solved. What a nightmare. Hope this helps someone.

How to hook UIAlertView to actions?

I am creating a drawing application where the user can draw using their finger strokes. I am trying to make a button that asks the user if they would like to clear the canvas. This alert has two buttons "Yes" and "No". I have the alert view appearing correctly but I have spent all day trying to figure out how to hook the buttons up to actions. I have so far had no success even after reading and watching from many instructional sources. From everything that I have read I can't understand why it would not be working. I have included UIAlertViewDelegate in my .h file also.
Here is my alert view:
- (IBAction)clearButton:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Clear Canvas"
message:#"Are you sure?"
delegate:nil
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes", nil];
[alert show];
}
Here is my clear canvas method:
- (void)clearCanvas:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1)
drawImage.image = nil;
}
Any help or pointers would be greatly appreciated! I'm self taught and still very much a beginner!
Thanks!
- (IBAction)clearButton:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Clear Canvas"
message:#"Are you sure?"
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes", nil];
[alert show];
}
Notice the difference in the delegate parameter. You must conform to the delegate you have declared in the .h
Secondly, use the delegate method -alertView:clickedButtonAtIndex

Prompt for user input and store the data

I just started iOS development, and now I'm stuck. I have a table cell, where the user cann see data. Next to it, there are two buttons. One to delete, one to edit. The edit part is where I'm stuck. I have a label field. When the user clicks on the edit button a prompt should come where the user can type in data. I already found something to do it. But there's no save button and I don't know how I could then save the variable to an NSString
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Name der neuen Liste" message:#"" delegate:self cancelButtonTitle:#"Abbrechen" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
I currently have this. lNow a prompt pops up, where I can write something, but not save. I just can press cancel afterwards. How do I work with user input in iOS? I don't want to use the label as an UITextField since it has to be clickable to perform another action.
Try this:
- (void)noteInput
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"input"
message:#"input"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Save", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
UITextField *noteText = [alertView textFieldAtIndex:0];
NSString *note = noteText.text;
}
}
Try adding a Save button in otherButtonTitles. Then, in your UIAlertViewDelegate:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
// Save button pressed
}
}

Resources