How to send message to alertController? - ios

I am learning Objective-C,and I want to create a tableView.When i touch on a table,an alert view is presented.The alert view will show a name , a description and two textFields which can input the new name and description.How can i change the name and the description by clicking the "Ok" button.here is my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Coder *coder = _allCoder[indexPath.row];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Jack" message:#"Jack is a coder" preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder =#"name"; }];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"description";}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
UITextField *name = alertController.textFields.firstObject;
coder.name = name.text;
UITextField *desc = alertController.textFields.lastObject;
coder.desc = desc.text;
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}

Related

UIAlertController Action isnt working?

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];

UIAlertController how add the tag value in obj c

Using this for UIAlertView
- (void) alertStatus:(NSString *)msg :(NSString *)title :(int) tag
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil];
alertView.tag = tag;
[alertView show];
}
But now UIAlertView get deprecated. change my code
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* MyAlert = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:MyAlert];
[self presentViewController:alertController animated:YES completion:nil];
Here how can pass this tag value
alertView.tag = tag;
Help how to pass the tag value in UIAlertController. Thanks advance.
UIAlertController is the UIViewController , so we need to assign the tag for view, soe use alertController.view.tag.
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"sds" message:#"sdf" preferredStyle:UIAlertControllerStyleAlert];
alertController.view.tag = tag;
UIAlertAction* MyAlert = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:MyAlert];
[self presentViewController:alertController animated:YES completion:nil];
update
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"sds" message:#"sdf" preferredStyle:UIAlertControllerStyleAlert];
alertController.view.tag = 3;
UIAlertAction* MyAlert = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
// OK button tappped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}];
[alertController addAction:MyAlert];
[self presentViewController:alertController animated:YES completion:nil];
create a property of UIAlertController *alertController;, then use this alertController where ever you want. set the tag like this
alertController.view.tag = <YOUR TAG VALUE>;
to get the tag of that alertController, when you click on YES on alertController
//OK button tapped.
[self dismissViewControllerAnimated:YES completion:^{
NSInteger *tag = alertController.view.tag;
}];
Have not any tag property in UIAlertController. You can use block for getting button action.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Alert"
message:[NSString stringWithFormat:#"Your message"]
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//NSLog(#"OK");
}]];
[self presentViewController:alert animated:YES completion:nil];
But you can use tag in this way-
alert.view.tag = 1;

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];

How do i use the text in TextField in a block inside UIAlertController?

I have a UIAlertController, which contains a textfield and 2 buttons: OK and Cancel. i want to retrieve the text that the user entered in the textfield when he presses OK, but because the textfield is in a block i cant access the textField.text field that is inside the block from the OK button block. how do i access it and use the text entered by the user? see my code below.
- (IBAction)saveItinerary:(id)sender
{
NSString *itineraryNameInput = [[NSString alloc] init];
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"Save Itinerary"
message:#"Enter Name for the Itinerary"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"Name";
textField.text = itineraryNameInput;
}];
NSLog(#"name = %#", itineraryNameInput);
[self presentViewController:alert animated:YES completion:nil];
}
Use the textFields property of the UIAlertController.
UITextField *textField = alert.textFields.firstObject;

how to prevent ABPeoplePickerNavigationController from automatically dismissing a presented view

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:#"testTitle"
message:#"test"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* okAction = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(#"User clicked button called %#",action.title);
}];
UIAlertAction* cancelAction = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(#"User clicked button called %#",action.title);
}];
[alertController addAction:okAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
}
After selecting a property, the Alert will show up for a second then dismisses itself.
How can I prevent ABP from dismissing my alert?
OR
How can I use another event to bring my alert up to the user.

Resources