UIAlertView not calling - ios

I'm attempting to create a simple webpage browser-esque application, where whenever the user wants to use a back, home, or forward function they are asked through an alert view to confirm.
The console window confirms that it has no problem calling the IBAction but comes up blank whenever I expect it to call Alert View.
Any help would be appreciated.
- (IBAction)controlPanelActions:(id)sender{
if (_controlPanel.selectedSegmentIndex == 0)
{
if ([_viewWeb canGoBack])
{
[self askToGoHome:nil];
}
}
- (IBAction)askToGoHome:(id)sender {
UIAlertView *alertDialog;
alertDialog.tag = 2;
alertDialog = [[UIAlertView alloc]
initWithTitle: #"Confirm"
message:#"Continue with Home Action?"
delegate: self
cancelButtonTitle: #"Yes"
otherButtonTitles: #"No", nil];
[alertDialog show];
}
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if (alertView.tag == 1)
{
if ([buttonTitle isEqualToString:#"Yes"])
{
NSLog(#"back - yes");
[_viewWeb goBack];
}
}
if (alertView.tag == 2)
{
NSLog(#"home - pre-yes");
if ([buttonTitle isEqualToString:#"Yes"])
{
NSLog(#"home - yes");
HOMEPAGEURL = [[NSURL alloc] initWithString:HOMEPAGE];
[self.viewWeb loadRequest:[NSURLRequest requestWithURL:HOMEPAGEURL]];
}
}
if (alertView.tag == 3)
{
if ([buttonTitle isEqualToString:#"Yes"])
{
[_viewWeb goForward];
}
}
}

You set alertDialog.tag = 2;before you call init.
So,everytime you set tag,you set tag to a nil.It will not work.

Alertview alloc and init method sets its tag to 0.
So alertDialog.tag = 2;
will not work.
add this line after alloc and init method.

Related

Multiple AlertView with tags, can't cancel

I searched Stack for ways to implement multiple alertViews. Most of the answers were to use Tags. This way works great, except for one huge thing- the cancel button! When the alertView pops up, whether you tap "cancel" or "yourButtonTitle", your action goes through. Is there a way to cancel an alertView with Tags?
Here is my code:
#define TAG_ONE 1
#define TAG_TWO 2
- (IBAction)someButton1 {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Call" message:#"Call number?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Call", nil];
alertView.tag = TAG_ONE;
[alertView show];
}
- (IBAction)someButton2 {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Log Out?" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Yes", nil];
alertView.tag = TAG_TWO;
[alertView show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == TAG_ONE) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:101-101-1010"]];
} else if (alertView.tag == TAG_TWO){
[PFUser logOut];
[self performSegueWithIdentifier:#"showLogin" sender:self];
}
}
you can set TAG for only the UIAlertview , but you can Identify the button using button Index
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == TAG_ONE) {
if(alertView.cancelButtonIndex == buttonIndex){
// Do cancel
}
else{
// Do the success thing
}
}
else if (alertView.tag == TAG_TWO) {
// same thing followed
}
}
buttonIndex == 0 // for OK , buttonIndex == 1 // for Cancel
additional Reference
You must add if (alertView.cancelButtonIndex == buttonIndex){
// Do cancel
} before asking if (alertView.tag == TAG_ONE) {

UIAlertView button action not working

I have one alert view and when I click on yes button it is supposed to produce another alert view and a toast message,but it is not happening. I couldn't figure it out. Here is my code:
-(void)myMethod {
UIAlertView *saveAlert = [[UIAlertView alloc] initWithTitle:#"First Message"
message:#"My First message"
delegate:nil
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes", nil];
saveAlert.tag=0;
[saveAlert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:NO];
}
This is the method I am using to provide the functionality for different alert views.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag==0) {
if (buttonIndex == 0)
{
//Code for Cancel button
}
if (buttonIndex == 1)
{
//code for yes button
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
hud.mode = MBProgressHUDModeText;
hud.labelText = #"Successfully displayed First Message";
hud.margin = 10.f;
hud.yOffset = 150.f;
hud.removeFromSuperViewOnHide = YES;
[hud hide:YES afterDelay:3];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Second Message"
message:#"My second message"
delegate:nil
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes",nil];
alert.tag=1;
[alert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:YES];
}
}
if (alertView.tag==1) {
if (buttonIndex == 0)
{
//Code for Cancel button
}
if (buttonIndex == 1)
{
//Code for yes Button
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
hud.mode = MBProgressHUDModeText;
hud.labelText = #"Succesfully displayed Second Message";
hud.margin = 10.f;
hud.yOffset = 150.f;
hud.removeFromSuperViewOnHide = YES;
[hud hide:YES afterDelay:3];
}
}
}
Can anyone help in finding the issue. Why I cannot get my second alert after clicking yes button in first alert?
You have not set the delegate for your UIAlertView and also make sure your delegate conforms to UIAlertViewDelegate protocol. Find the code snippet below.
You controller conforms to UIAlertViewDelegate protocol:
#interface YourViewController : UIViewController <UIAlertViewDelegate>
Create UIAlertView and set the deleagte:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"First Message"
message:#"Show second message"
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes", nil];
[alertView show];
Implement UIAlertViewDelegate delegate method:
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if( 0 == buttonIndex ){ //cancel button
[alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
} else if ( 1 == buttonIndex ){
[alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
UIAlertView * secondAlertView = [[UIAlertView alloc] initWithTitle:#"Second Message"
message:#"Displaying second message"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[secondAlertView show];
}
}
You are specifying nil as the delegate for your alert views. You need to specify an object so the alertView:clickedButtonAtIndex: method can be called!
If you'd like to handle this more clear, you could use a block-based AlertView.
Create new file->Subclass of->UIAlertView
SuperAlertView.h
#import <UIKit/UIKit.h>
#class MySuperAlertView;
typedef void (^MySuperAlertViewBlock) (MySuperAlertView *alertView);
#interface MySuperAlertView : UIAlertView
- (instancetype) initWithTitle:(NSString *)title message:(NSString *)message;
- (void) addButtonWithTitle:(NSString *)buttonTitle block:(MySuperAlertViewBlock) block;
#end
SuperAlertView.m
#import "MySuperAlertView.h"
#interface MySuperAlertView()<UIAlertViewDelegate>
#property NSMutableArray *blocks;
#end
#implementation MySuperAlertView
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message
{
if (self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:nil])
{
self.blocks = [NSMutableArray array];
}
return self;
}
- (void)addButtonWithTitle:(NSString *)buttonTitle block:(MySuperAlertViewBlock)block
{
[self addButtonWithTitle:buttonTitle];
[self.blocks addObject:block ? [block copy] : [NSNull null]];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
MySuperAlertViewBlock block = self.blocks[buttonIndex];
if ((id) block != [NSNull null]){
block(self);
}
}
#end
Usage:
MySuperAlertView *alertView = [[MySuperAlertView alloc] initWithTitle:#"Info" message:NSLocalizedString(#"EMAIL_SENT_SUCCESSFULL", nil)];
[alertView addButtonWithTitle:#"Ok" block:^(MySupertAlertView *alertView) {
// handle result from ok button here
}];
[alertView addButtonWithTitle:#"cancel" block:NULL];
dispatch_async(dispatch_get_main_queue(), ^{
[alertView show];
});

iOS AlertView trigger which button has been pressed

I am trying to see what option the client has pushed on the alert view button, though the logging doesn't appear to be working
-(IBAction)flattenBtn:(id)sender
{
UIAlertView* flatView = [[UIAlertView alloc] initWithTitle:#"Flatten Image"
message:#"Are you sure you want to flatten your image?" delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Yes",#"No",nil];
[flatView show];
[flatView release];
}
- (void)flatView:(UIAlertView *)flatView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(#"YES");
}
else if (buttonIndex == 1)
{
NSLog(#"NO");
}
}
Try it using like this :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Yes"])
{
//your code if YES pressed;
}
else
{
//your code if NO pressed;
}
}

No known class method for selector when using blocks

i am new to use block syntax and facing the below problem. The below code is calling another static method of a class that causing the problem. below code is called from click of next button on bar . is there any mistake on the syntax of this code?
-(BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
if (![sender isKindOfClass:[UIBarButtonItem class] ]) {
return true;
}
// Trim the spaces
self.stewardsNameTextField.text = [self.stewardsNameTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ];
self.trackNameTextField.text = [self.trackNameTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ];
self.curatorNameTextField.text = [self.curatorNameTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ];
bool isValid =[JLTValidator validateFields: #[self.stewardsNameTextField, self.trackNameTextField, self.curatorNameTextField, self.weatherConditionSegment, self.trackConditionSegment] withScrollToCallback: ^(UIView * invalidField) // problem is here. Is this incorrect syntax?
{
if (invalidField == self.stewardsNameTextField || invalidField == self.trackNameTextField || invalidField == self.curatorNameTextField)
{
[invalidField becomeFirstResponder];
}
else
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
CGPoint top = CGPointMake(0, invalidField.frame.origin.y - 90);
[_scrollView setContentOffset:top animated:YES];
_scrollView.scrollIndicatorInsets=contentInsets;
}
if (!isValid) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry!" message:#"Please fill out the marked fields." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
return isValid;
}];
}
The definition of the static method being called to the class:
+(BOOL)validateFields:(NSArray *)fields
{
return [JLTValidator validateFields:fields withScrollToCallback:nil];
}
+(BOOL)validateFields:(NSArray *)fields andShouldDisplayMessage : (bool) shouldDisplayMessage
{
return [JLTValidator validateFields:fields withScrollToCallback:nil andShouldDisplayMessage:shouldDisplayMessage];
}
+(BOOL)validateFields:(NSArray *)fields withScrollToCallback : (void (^) (UIView *))scrollToCallback
{
return [JLTValidator validateFields:fields withScrollToCallback:scrollToCallback andShouldDisplayMessage:true];
}
+(BOOL)validateFields:(NSArray *)fields withScrollToCallback : (void (^) (UIView *))scrollToCallback andShouldDisplayMessage : (bool) shouldDisplayMessage
{
}
Whats wrong here? pls guide.
+(BOOL)validateFields:(NSArray *)fields withScrollToCallback : (void (^) (UIView *))scrollToCallback
The block parameter in this method doesn't return anything (you can see 'void' there). So if inside the block, you return BOOL value (isValid) is incorrect. Moreover, you returned the value that you're trying to get (isValid = ..... isValid) which is also a mistake.
I don't know what u're trying inside the block, but to solve this, you can do like below:
bool isValid =[JLTValidator validateFields: #[self.stewardsNameTextField, self.trackNameTextField, self.curatorNameTextField, self.weatherConditionSegment, self.trackConditionSegment] withScrollToCallback: ^(UIView * invalidField) // problem is here. Is this incorrect syntax?
{
if (invalidField == self.stewardsNameTextField || invalidField == self.trackNameTextField || invalidField == self.curatorNameTextField)
{
[invalidField becomeFirstResponder];
}
else
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
CGPoint top = CGPointMake(0, invalidField.frame.origin.y - 90);
[_scrollView setContentOffset:top animated:YES];
_scrollView.scrollIndicatorInsets=contentInsets;
}
}];
if (!isValid) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry!" message:#"Please fill out the marked fields." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}

Set background from UIAlertView option xcode

Im trying to do an app with 4 ImageViews and 4 UIButtons. When one of the UIButtons are pressed a UIAlertView should appear with 3 options. One named "Picture" should open the photo library so the user can change the ImageView picture. I have wrote a code for this but it wont work. Do anyone have any suggestions on how to make it work?
Thanks in advance!
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
NSString *buttonOne = changeButtonOne; //the UIButtons
NSString *buttonTwo = changeButtonTwo;
NSString *buttonThree = changeButtonThree;
NSString *buttonFour = changeButtonFour;
if([buttonOne isEqualToString:#"buttonOne"])
{
if([title isEqualToString:#"Done"])
{
NSLog(#"You pressed done");
}
else if([title isEqualToString:#"Phone number"])
{
NSLog(#"You pressed Phone number");
}
else if([title isEqualToString:#"Picture"])
{
imagePickerController = [[UIImagePickerController alloc]init]; //I think its this part which are wrong
[imagePickerController setDelegate:self];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController animated:YES completion:nil];
}
}
else if([buttonTwo isEqualToString:#"buttonTwo"])
{
if([title isEqualToString:#"Done"])
{
NSLog(#"Yoy pressed done");
}
else if([title isEqualToString:#"Phone number"])
{
NSLog(#"You pressed Phone number");
}
else if([title isEqualToString:#"Picture"])
{
imagePickerController2 = [[UIImagePickerController alloc]init]; //I think its the part thats wrong.
[imagePickerController2 setDelegate:self];
[imagePickerController2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController2 animated:YES completion:nil];
}
}
else if([buttonThree isEqualToString:#"buttonThree"])
{
if([title isEqualToString:#"Done"])
{
NSLog(#"You pressed done");
}
else if([title isEqualToString:#"Phone number"])
{
NSLog(#"You pressed Phone number");
}
else if([title isEqualToString:#"Picture"])
{
imagePickerController3 = [[UIImagePickerController alloc]init]; //i think this is the wrong part
[imagePickerController3 setDelegate:self];
[imagePickerController3 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController3 animated:YES completion:nil];
}
}
else if([buttonFour isEqualToString:#"buttonFour"])
{
if([title isEqualToString:#"Done"])
{
NSLog(#"You pressed done");
}
else if([title isEqualToString:#"Phone number"])
{
NSLog(#"You pressed Phone number");
}
else if([title isEqualToString:#"Picture"])
{
imagePickerController4 = [[UIImagePickerController alloc]init]; //this is probably the wrong part
[imagePickerController4 setDelegate:self];
[imagePickerController4 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController4 animated:YES completion:nil];
}
}
}
#end
You should use the tag property of UIAlertView to differentiate between the alerts and the buttonIndex (not the button title) to know which button was pressed.
So don't use isEqualToString at all in the alert view delegate method. You want this to work with different languages.
In the button action, before you show the alert:
alertView.tag = 1; // 1 = button1, 2 = button2 etc.
Alert view delegate:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1) { //button1
if (buttonIndex == alertView.cancelButtonIndex) {
// handle cancel button
}
if (buttonIndex == alertView.firstOtherButtonIndex) {
// handle action 1
}
if (buttonIndex == alertView.firstOtherButtonIndex+1) {
// handle action 2
}
}
if (alertView.tag == 2) { //button2
if (buttonIndex == alertView.cancelButtonIndex) {
// handle cancel button
}
if (buttonIndex == alertView.firstOtherButtonIndex) {
// handle action 3
}
if (buttonIndex == alertView.firstOtherButtonIndex+1) {
// handle action 4
}
}
}

Resources