UIAlertController Action isnt working? - ios

I have added a UIAlterController to my view. IT triggers fine, looks fine, and has its action, however tapping the Cancel action does nothing, it doesnt run any code, even if i add a block it wont execute it, it just doesnt seem to recognise the tap at all.
The code looks fine to me, so is there some other cause?
- (void)connectionFailed:(NSString *)title {
NSString *message = nil;
if ([title isEqualToString:NSLocalizedString(#"Connection Refused", #"connection Refused")]) {
message = NSLocalizedString(#"You do not have permission to use this console", #"incorrect permissions message");
}
else {
message = NSLocalizedString(#"Connection Failed", #"connection failed error message");
}
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:nil];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
});
Edit: it appears the issue is down to view leaks causing problems, not sure of the fix but here is the breakdown in debugging:
debugging image of view stack

Use this:
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:nil];
Change UIAlertActionStyleDefault to UIAlertActionStyleCancel

Try this:
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {
//your code here ...
}];

- (void)connectionFailed:(NSString *)title {
NSString *message = nil;
if ([title isEqualToString:NSLocalizedString(#"Connection Refused", #"connection Refused")]) {
message = NSLocalizedString(#"You do not have permission to use this console", #"incorrect permissions message");
}
else {
message = NSLocalizedString(#"Connection Failed", #"connection failed error message");
}
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style: UIAlertActionStyleCancel
handler:nil];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
});
just edited your cancel button alert style....

Try this :
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
UIAlertActionStyleDefault
handler:nil];

Related

UIAlertController buttons are not visible

I have an Objective C project and want to show some alert to user, On some iPads the UIAlertController buttons are not visible, i have attached an image below.
Any one faced an issue like this? any workarounds?
Device Details
iOS 14.4
iPad Air 2
sample code used
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#“Alert” message:#“Message” preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *yesAction = [UIAlertAction actionWithTitle:#“YES” style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
}];
UIAlertAction *noAction = [UIAlertAction actionWithTitle:#“NO” style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
}];
[alert addAction:yesAction];
[alert addAction:noAction];
[alert show];
image
this looks weird enough.
But I think the problem is that you are calling the show method.
And you need 'presentViewController' because it is no longer UIAlertView' but UIAlertViewController.
Please use my snippet.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Alert" message:#"Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//button click event
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:cancel];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
For a more in-depth study, I recommend this article

Detect Alert Button Click Event

I have the following implementation to be reused entire app. I store the following code in the Utility.m class.
CustomViewController.m
How can I capture click event in the following
[self presentViewController:[Utility oneButtonDisplayAlert:#"Error" withMessage:#"Please try again later"] animated:YES completion:nil];
Utility.m
+ (UIAlertController *)oneButtonDisplayAlert : (NSString*)title withMessage : (NSString*) message
{
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle your yes please button action here
}];
[alert addAction:yesButton];
return alert;
}
Add a block parameter to your oneButtonDisplayAlert:withMessage: method. Call that block inside the alert action's handler.
+ (UIAlertController *)oneButtonDisplayAlert:(NSString *)title withMessage:(NSString *)message andOKHandler:(void (^)(void))handler
{
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if (handler) {
handler();
}
}];
[alert addAction:yesButton];
return alert;
}
And then call it as:
UIAlertController *alert = [Utility oneButtonDisplayAlert:#"Error" withMessage:#"Please try again later" andOKHandler:^{
// whatever code you need when OK tapped
}];
[self presentViewController:alert animated:YES completion:nil];
Note: Code in this answer might have a typo. Syntax not verified.

dispatch async code error - Objective C

I am new to Objective C but have been working on Swift for a while now. I assumed Objective c to be logically similar to swift. I have to present an alert controller while I am processing a json data request; so I had to use dispatch async to get it to work in swift. Here's the code I used in Swift:
func alertMessage(message : String) -> Void {
let alert = UIAlertController(title: "Alert", message: message, preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alert.addAction(okAction)
dispatch_async(dispatch_get_main_queue(),{
self.presentViewController(alert, animated: true, completion: nil)
})
}
But I tried to do the same thing in objective C like so
- (void)alertMessage : (NSString*) message {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Alert" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:NULL];
[alert addAction:okAction];
[dispatch_async(dispatch_get_main_queue(), ^(void){
[self presentViewController:alert animated:true completion:NULL];
})];
}
I'm getting an error "expected identifier". What am I doing wrong?
Its just syntax error. Try following
- (void)alertMessage : (NSString*) message {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Alert" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:NULL];
[alert addAction:okAction];
dispatch_async(dispatch_get_main_queue(), ^(void){
[self presentViewController:alert animated:true completion:NULL];
});
}
Because “dispatch_async” is a C function, you should call it like this.
- (void)alertMessage : (NSString*) message {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Alert" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:NULL];
[alert addAction:okAction];
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:alert animated:true completion:nil];
});
}
Can try now
- (void)alertMessage : (NSString*) message {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Alert" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:NULL];
[alert addAction:okAction];
dispatch_async(dispatch_get_main_queue(), ^(void){
[self presentViewController:alert animated:true completion:nil];
});
}
Output:
Happy coding..

UIAlertController as a Utility that accepts function parameter

I have this UIAlertController as a utility that accepts two paremeters, the title and the content. I want to modify the "confirm" button. I want to duplicate this utility and add another parameter the will execute a specific function.
-(UIAlertController *) modalWithTitle : (NSString *) title andContent: (NSString *) content{
UIAlertController *alert = [UIAlertController alertControllerWithTitle: title message:content preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){}];
[alert addAction:defaultAction];
return alert;
}
Example code :
UIAlertController *alert =[[ModalController alloc] modalWithTitle:#"Error" andContent:#"Network unavailable."
andAction:<ENTER FUNCTION TO EXECUTE HERE>];
[self presentViewController:alert animated:YES completion:nil];
You can write it like this:
+ (UIAlertController *)modalWithTitle:(NSString *)title andContent:(NSString *)content andHandler:(void (^)(UIAlertAction *))handler {
UIAlertController *alert = [UIAlertController alertControllerWithTitle: title message:content preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:handler];
[alert addAction:defaultAction];
return alert;
}
Usage:
void (^handler)(UIAlertAction *) = ^(UIAlertAction *action) {
// code to execute
};
[[ModalController alloc] modalWithTitle:#"title" andContent:#"content" andHandler:handler];
Another approach:
+ (UIAlertController *)modalWithTitle:(NSString *)title andContent:(NSString *)content andHandler:(void (^)(void))handler {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:content preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
handler();
}];
[alert addAction:defaultAction];
return alert;
}
Usage:
void (^block)(void) = ^{
// code to execute
};
[[ModalController alloc] modalWithTitle:#"title" andContent:#"content" andHandler:block];

UIAlertController giving an error

I am new on IOS. I have a sample project and trying to learn Obj-C on that.
Now I am on the stage that is about learning how to use UIAlertController.
And I have a code like that:
if (loanAmount == 0) {
UIAlertController *ErrorMessage =[UIAlertController alertControllerWithTitle:#"Invalid amount" message:#"Enter a valid number" preferredStyle:UIAlertControllerStyleAlert];
} else
{ // sets labels
self.interestLabel.text = [NSString stringWithFormat:#"%i%c",interestRate,percentage];
self.periodLabel.text = [NSString stringWithFormat:#"%i months",months];
self.totalLabel.text = [NSString stringWithFormat:#"$%i",(loanAmount + tinterest)];
========================================================================
But When I run the simulator. There should be pop up message on Simulator. Instead of that, there is an error that says:
015-10-11 13:56:39.985 iBank[8055:1231178] Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UIAlertController: 0x7ca82400>)
Any help would be greatly appreciated
try this
if (loanAmount == 0) {
UIAlertController * ErrorMessage = [UIAlertController
alertControllerWithTitle:#"Invalid amount"
message:#"Enter a valid number"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Cancel", #"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(#"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"OK", #"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"OK action");
}];
[ErrorMessage addAction:cancelAction];
[ErrorMessage addAction:okAction];
[self presentViewController: ErrorMessage animated:YES completion:nil];
}
else
{
self.interestLabel.text = [NSString stringWithFormat:#"%i%c",interestRate,percentage];
self.periodLabel.text = [NSString stringWithFormat:#"%i months",months];
self.totalLabel.text = [NSString stringWithFormat:#"$%i",(loanAmount + tinterest)];
}

Resources