AlertView:Objective C - ios

I'm trying to do an alert view that gives the user a choice of 3 options to choose from. buttonIndex 1 and 2 are fine but when i select buttonIndex 3 thing happens. I want it in a way that when buttonIndex 3 is selected and alert view with 3 options should appear and it should call a different method depending on what buttonIndex the user selects. How do i fix this code(buttonIndex==3)????
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
AddReferenceViewController *project =
[self.storyboard instantiateViewControllerWithIdentifier:#"AddRef"];
project.projectdb = self.projectdb;
[self presentViewController:project animated:YES completion:nil];
}
else if(buttonIndex == 2)
{
StyleViewController *style = [self.storyboard instantiateViewControllerWithIdentifier:#"StyleController"];
[self presentViewController:style animated:YES completion:nil];
}
else if(buttonIndex == 3)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Styles"
message:nil
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Harvard",#"Chicago", #"Vancouver",nil];
if(buttonIndex == 1){
[self Harvard];
}
else if(buttonIndex == 2){
[self Chicago];
}
else if(buttonIndex == 1){
[self Vancouver];
}
else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Information"
message:#"No Reference added to this project"
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Add Reference",#"Ref Style", #"Email References",nil];
[alert show];
}
}
}

Button index should be 0, 1, 2 for the 3 buttons and not 1, 2, 3. This will remove the bug for you
From the documentation:
buttonIndex
The index of the button that was clicked. The button indices start at 0. If this is the cancel button index, the alert view is canceling. If -1, the cancel button index is not set.

Use a different delegate for each of your alert views.
Also, note this class is deprecated and you should use UIAlertController instead.

Related

AlertView triggering keyboard after dismissal

So this is happening.
I have one view controller and it handles all of the alert views in my app. I have another view controller that has a UITextView that the user can edit, and a save button.
When they hit the save button, if the text is already saved, it triggers an alert that asks them if they're sure they want to update, and if they confirm, it updates the file and gives them a second alert that says it was a success.
What's happening is that the keyboard keeps popping up when the second alert appears. I've tried resigning the keyboard and turning off the user interaction enabled flag on the text field as soon as the save button is hit.
self.storyEditorTextView.userInteractionEnabled=NO;
[self.storyEditorTextView resignFirstResponder];
I've also tried to turn it off when the alert is responded to (since some alerts can have a textfield).
To make matters worse, when I comment out the resign and userInteractionsEnabled lines, including the one in the alert, the keyboard still appears after the first alert is dismissed, disappears when the second alert is dismissed (if you can tap it because the keyboard covers it), and you can't tap into the UITextView and bring up the keyboard without going back to the parent view.
Here's the alert code.
- (void)addPromptToFavorites
{
// throw up an alert to confirm and get a name
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Name Your Favorite!"
message:#"Would you like to add a name to your prompt?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
// add a text field
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textField = [alert textFieldAtIndex:0];
textField.text = #"My Great Prompt";
// set the tag
[alert setTag:SAVE_FAVE];
// Display the alert to the user
[alert show];
}
- (void)updateFave: (NSNumber *) theFaveId
{
NSLog(#"UPDATE FAVE\n\nself.sharedFaveMan.tempFave %#",self.sharedFaveMan.tempFave);
// NSMutableDictionary *faveDict =[[NSMutableDictionary alloc] init];
// loop through the favoritePrompts array until you find a match to the faveID
for (id element in self.sharedFaveMan.favoritePrompts) {
NSNumber *valueForID = [element valueForKey:#"id"];
if([valueForID isEqualToNumber:theFaveId])
{
self.sharedFaveMan.tempFave=element;
}
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Update Your Favorite!"
message:#"The story will be saved with the currently selected Favorite."
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
//set the tag
[alert setTag:UPDATE_FAVE];
// Display the alert to the user
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
UITextField *textField = [alertView textFieldAtIndex:0];
[textField resignFirstResponder];
if (buttonIndex !=0)
{
if(alertView.tag==UPDATE_FAVE)
{
NSLog(#"Updating Fave");
// loop through the favoritePrompts array until you find a match to the faveID
int counter=0;
for (id element in self.sharedFaveMan.favoritePrompts) {
NSNumber *valueForID = [element valueForKey:#"id"];
if([valueForID isEqualToNumber:self.sharedFaveMan.theFaveID]){
break;
}
counter ++;
}
// update the pieces of the prompt
[[self.sharedFaveMan.favoritePrompts objectAtIndex:counter] setObject:self.sharedFaveMan.faveStoryText forKey:#"storyText"];
// save it
[self saveFavorites];
[[NSNotificationCenter defaultCenter]
postNotificationName:#"updateTheTable"
object:self];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Success!"
message:#"Favorite Updated!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"OK",nil];
//play a sound
[self createSoundID: #"ticktock.aiff"];
//set the tag
[alert setTag:UPDATE_COMPLETE];
// Display the alert to the user
[alert show];
}
}
else if (buttonIndex == 0)
{
NSLog(#"%ld",(long)alertView.tag);
if(alertView.tag==SAVE_FAVE)
{
// they canceled the save
}
else if (alertView.tag==UPDATE_COMPLETE)
{
NSLog(#"Hit that");
[[NSNotificationCenter defaultCenter]
postNotificationName:#"dismissedDialogNotification"
object:self];
}
}
}
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
UITextField *textField = [alertView textFieldAtIndex:0];
if (textField && [textField.text length] == 0)
{
return NO;
}
return YES;
}
Any ideas?
Here is the code
- (void)textViewDidEndEditing:(UITextView *)textView {
[textView resignFirstResponder];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
// Return FALSE so that the final '\n' character doesn't get added
return NO;
}
// For any other character return TRUE so that the text gets added to the view
return YES;
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView {
[textView resignFirstResponder];
return true;
}
and in alert method at the begining add this line of code
[self.view endEditing:YES];

How to detect whether UIAlertView is currently showing

Is there any way to judge whether there is currently a UIAlertView instance showing? Because it is possible to show multiple UIAlertView in the same window level.
if ([self isAlertViewShowing]) {
// do not show UIAlertView again.
} else {
// show your UIAlertView.
}
Hope there is such a method called isAlertViewShowing or something else.
Method 1-
Initialize default flag for alert... If alert is not open set isAlertViewShowing as NO
Bool isAlertViewShowing;
isAlertViewShowing = NO;
if (isAlertViewShowing == NO){
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
// Now set isAlertViewShowing to YES
isAlertViewShowing = YES;
}
else
{
//Do something
}
Method 2-
Make your own function to check whether any UIAlertView is showing or not
- (BOOL)isAlertViewShowing{
for (UIWindow* window in [UIApplication sharedApplication].windows) {
NSArray* subviews = window.subviews;
if ([subviews count] > 0){
for (id view in subviews) {
if ([view isKindOfClass:[UIAlertView class]]) {
return YES;
}
}
}
}
return NO;
}
I recommended to use second method if number of UIAlertView instance
may be more than one.

UIActionSheet coming up too far from bottom of screen

So for some reason after I've just added an actionsheet it's coming up and stopping above the bottom of the screen. Is there a way to manually tell this where to stop?
- (void)showActionSheet:(UIButton *)standardButton{
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel Button" destructiveButtonTitle:nil otherButtonTitles:#"Choose From Library", #"Take Photo", nil];
popupQuery.actionSheetStyle = UIActionSheetStyleAutomatic;
[popupQuery showInView:self];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
// Choose from library tapped
NSLog(#"Choose");
} else if (buttonIndex == 1) {
// Take a photo tapped
NSLog(#"Take");
}
}
Try self.view instead of self.

UIAlertViewDelegate:clickedButtonAtIndex and two buttons

guys:
There is two buttons in my viewController of test app, the right one I call it "NO",
and the other one is "YES". The two buttons will call two different functions, and when
user press one of the buttons , I want show the user an alert to confirm that.
I know use the UIAlertViewDelegate
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
but there is two buttons, I am puzzled. How can I know which button is pressed.
So,pls help me with this, thank you in advance!
When you create an UIAlertView you can set up a tag for it
-(IBAction)yesButtonClick:(id)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle: #"Cancel" otherButtonTitles:#"OK", nil];
alert.tag = 101;
[alert show];
}
-(IBAction)noButtonClick:(id)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle: #"Cancel" otherButtonTitles:#"OK", nil];
alert.tag = 102;
[alert show];
}
In the delegate method check which alert is being shown
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == 101) {
// from YES button
}
else if (alertView.tag == 102) {
// from NO button
}
}
you can use the tag attribute to make the difference between your tow UIAlertView
in the function of button 1
alertView1.tag=1;
and in
-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(actionSheet.tag==1){
//first button was clicked
}
}
- (void)alertView:(UIAlertView *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex{
switch(buttonIndex){
case 0:
//YES button handler
break;
case 1:
//NO button handler
break;
default:
break;
}
}

adding action to alertView Button

I everybody,
I have 3 Buttons each one calls an AlertView with "Cancel" and "OK" and each "OK" Button goes to an different view.
for now I solved this with this
- (UIButton *)1_BTN
{
if (1_BTN == nil)
{
UIImage *buttonBackground = [UIImage imageNamed:#"1_btn.png"];
UIImage *buttonBackgroundPressed = [UIImage imageNamed:#"1_btn.png"];
CGRect frame = CGRectMake(655, 985, 107, 30);
1_BTN = [_IPadAppDelegate buttonWithTitle:#""
target:self
selector:#selector(1_BTN:)
frame:frame
image:buttonBackground
imagePressed:buttonBackgroundPressed];
[1_BTN setTag:1];
}
return 1_BTN;
}
......
- (void)1_BTN:(NSInteger *)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"some fancy text" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"ok", nil];
[alert setTag:[sender valueForKey:#"tag"]];
[alert show];
[alert release];
}
.......
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
if ([[alertView tag] isEqualToNumber:[NSNumber numberWithInt:1]]) {
something should happen.....
}
for all three Buttons and it works fine but for
[alert setTag:[sender valueForKey:#"tag"]];
and
if ([[alertView tag] isEqualToNumber:[NSNumber numberWithInt:1]]) {
i get this warning "Invalid receiver Type "NSInteger""
why is that so and how can I solve this better?
For one thing, you're doing this entirely wrong. Implement the UIAlertViewDelegate, specifically alertView:clickedButtonAtIndex: and check which button index (from 0 .. n where n is the last button). Act accordingly based on your known fixed indices.
Secondly, NSInteger is a scalar non-object type, and cannot receive messages. You would want an equality comparison instead, i.e., alertview.tag == 1. But as I said previously, don't do it that way.
The UIView property "tag" isn't an object, it is just a simple NSInteger. It's almost the same as "int" you probably know from C/C++. I corrected a few line in your code. Now it should work.
Another thing: The allocation of the UIButton seems a bit strange to me. Maybe you should check your memory management.
- (UIButton *)1_BTN
{
if (1_BTN == nil) {
UIImage *buttonBackground = [UIImage imageNamed:#"1_btn.png"];
UIImage *buttonBackgroundPressed = [UIImage imageNamed:#"1_btn.png"];
CGRect frame = CGRectMake(655, 985, 107, 30);
1_BTN = [_IPadAppDelegate buttonWithTitle:#"" target:self selector:#selector(1_BTNAction:) frame:frame image:buttonBackground imagePressed:buttonBackgroundPressed];
[1_BTN setTag:1];
}
return 1_BTN;
}
- (void)1_BTNAction:(UIButton *)sender {
NSInteger tagNumber = [sender tag];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"sone fancy text" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"ok", nil];
[alert setTag:tagNumber];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
if ([alertView tag] == 1) {
//something should happen
}
}
}

Resources