- (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.
Related
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
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.
In my app, I want to create a UIActionSheetor UIAlertController (UIAlertControllerStyleActionSheet) like Apple Music.
I want to show user profile picture and some basic details of user in first row of UIActionSheet and other option like normal UIActionSheet.
In somewhat I can give you the sample coding for your question
UIAlertController *alertMusicVideos = [UIAlertController
alertControllerWithTitle:#"Music Videos"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *actionAddProfilePhoto = [UIAlertAction
actionWithTitle:#""
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do Whatever you want to do here
[alertMusicVideos dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *actionPlayNext = [UIAlertAction
actionWithTitle:#"Play Next"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
[alertMusicVideos dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *actionAddToUpNext = [UIAlertAction
actionWithTitle:#"Add to Up Next"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do Whatever you want to do here when click this button
[alertMusicVideos dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *actionAddToPlayList = [UIAlertAction
actionWithTitle:#"Add To PlayList"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do Whatever you want to do here when click this button
[alertMusicVideos dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *actionDeleteFromMyMusic = [UIAlertAction
actionWithTitle:#"Delete From My Music"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action)
{
//Do Whatever you want to do here when click this button
[alertMusicVideos dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *actionCancel = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action)
{
//Do Whatever you want to do here when click this button
[alertMusicVideos dismissViewControllerAnimated:YES completion:nil];
}];
[actionAddProfilePhoto setValue:[[UIImage imageNamed:#"yourProfile.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:#"image"];
[alertMusicVideos addAction:actionAddProfilePhoto];
[alertMusicVideos addAction:actionPlayNext];
[alertMusicVideos addAction:actionAddToUpNext];
[alertMusicVideos addAction:actionAddToPlayList];
[alertMusicVideos addAction:actionDeleteFromMyMusic];
[alertMusicVideos addAction:actionCancel];
[self presentViewController:alertMusicVideos animated:YES completion:nil];
UAlertView is deprecated in iOS 9 and later. What would be an alternative?
UIAlertView *new = [[UIAlertView alloc] initWithTitle:#"Success" message:#"Your InApp Purchases were successfully restored" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[new show];
You can use this code to replace an alert view:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Title" message:#"Message" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
If you need multiple actions you can use:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Title" message:#"Message" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:#"Button 1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// action 1
}]];
[alertController addAction:[UIAlertAction actionWithTitle:#"Button 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// action 2
}]];
[alertController addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self dismissViewControllerAnimated:YES completion:nil];
}]];
[self presentViewController:alertController animated:YES completion:nil];
You get often detailed information including the replacement suggestion by ⌘-clicking on the symbol which displays the class/method declaration.
In case of UIAlertView you will see
"UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead"
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"Info"
message:#"You are using UIAlertController"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
I made a category for that:
+ (void)alertViewWithTitle:(NSString *)aTitle message:(NSString *)aMessage viewController:(UIViewController *) aVC
{
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:aTitle ? aTitle : #""
message:aMessage
preferredStyle:UIAlertControllerStyleAlert];
UIViewController *topVC = aVC ? aVC : [UIApplication sharedApplication].keyWindow.rootViewController;
[topVC presentViewController:alert animated:YES completion:nil];
}
The parameters aTitle and aVC are optional but aVC should be used if known.
PS: avoid the "new" as a variable name this is a reserved word, I actually don't know if it will compile though.
UIAlertController has been around since iOS 8.
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"My Title"
message:#"Enter User Credentials"
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
I have use "UIAlertController" on iOS 8 and later. Let see:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Success" message:#"Your InApp Purchases were successfully restored" preferredStyle:UIAlertControllerStyleAlert];
And add buttons:
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//do something when click button
}];
Remember:
[alertController addAction:okAction];
Then show it:
[self presentViewController:alertController animated:YES completion:nill];
If you want to show a actionsheep, you change
"preferredStyle:UIAlertControllerStyleActionSheet"
I want to show popup if I click a button on toolbar. I am using ios 8 SDK.
I am using below code to do the same, but now I want to remove title from it.
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:[appDelegate encTitle]
delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil
otherButtonTitles:#"Add Bookmark", #"Cancel",nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
actionSheet.cancelButtonIndex = 1; // make the second button red (destructive)
[actionSheet showInView:self.view]; // show from our table view (pops up in the middle of the table)
[actionSheet release];
If I use like this initWithTitle:#"" then also title block comes in the popup.
currently it is like below image
want to achieve like this
Please help me to fix the issue.
After using UIalertviewController as suggested by sweetAngel, its coming like this in iPhone 4s....please help to show it properly in all the devices.
If you are using iOS 8, then you can try this one :
UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"Add Bookmark" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
// Your code for bookmark
[alert1 dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
[alert1 dismissViewControllerAnimated:YES completion:nil];
}];
[alert1 addAction:ok];
[alert1 addAction:cancel];
[self presentViewController:alert1 animated:YES completion:nil];
In iOS 8, not only you should definetely not use retain / release, but in order to present an action sheet you should use UIAlertController because UIActionSheet is deprecated. Here's an example:
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:#"Evacuate Building!" message:#"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:#"Kick through door" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
// destructive action completion
}];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:#"Walk calmly" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// default action completion
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Do nothing" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// cancel action completion
}];
[actionSheet addAction:destructiveAction];
[actionSheet addAction:defaultAction];
[actionSheet addAction:cancelAction];
[self presentViewController:actionSheet animated:YES completion:nil];