I'm trying next way:
Message *message = self.messagesArray[indexPath.row];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:LocalizedString(#"FirstAction")
message:#""
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *editMessage = [UIAlertAction actionWithTitle:LocalizedString(#"SecondAction") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:editMessage];
UIAlertAction *forwardMessage = [UIAlertAction actionWithTitle:LocalizedString(#"ThirdAction") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:forwardMessage];
UIAlertAction *deleteMessage = [UIAlertAction actionWithTitle:LocalizedString(#"DeleteMessage") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:deleteMessage];
[self setSourceViewForAlertController:alertController];
[self presentViewController:alertController animated:YES completion:nil];
[self createMenuForMessage:message];
[[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
So, my aim is to present alertcontroller for UICollectionViewCell and UIMenuController at the same time.
Like this:
Your issue is related to where are you showing your UIMenuController you have to take in account that UIView must have implemented the canBecomeFirstResponder method returning YES
- (IBAction)action:(id)sender {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#""
message:#""
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *editMessage = [UIAlertAction actionWithTitle:#"Edit" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:editMessage];
UIAlertAction *forwardMessage = [UIAlertAction actionWithTitle:#"Move" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:forwardMessage];
UIAlertAction *deleteMessage = [UIAlertAction actionWithTitle:#"Delete" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:deleteMessage];
[self presentViewController:alertController animated:YES completion:nil];
[[UIMenuController sharedMenuController] setTargetRect:self.view.bounds inView:self.view];
[[UIMenuController sharedMenuController] setArrowDirection:UIMenuControllerArrowDefault];
[[UIMenuController sharedMenuController] setMenuItems:#[[[UIMenuItem alloc]initWithTitle:#"test" action:#selector(didReceiveMemoryWarning)]]];
[[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
}
-(BOOL)canBecomeFirstResponder{
return true;
}
Related
How to change this obj c code that suits with iOS 9 above ? i have this error when i updating X Code into 8.2.1 . The code as below
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Continue"]){
NSLog(#"Continue...");
[self performSegueWithIdentifier:#"createlogin" sender:self];
}
}
The error:
UIAlertView is deprecated : first deprecated in iOS 9.0 - UIAlertView
is deprecated. Use UIAlertController with a preferredStyle of
UIAlertControllerStyleAlert instead
Thanks.
UIAlertController * alert = [UIAlertController alertControllerWithTitle:#"Add Title" message:#"Add Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * actionOK = [UIAlertAction actionWithTitle:#"Button Title" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//Here Add Your Action
}];
UIAlertAction * actionCANCEL = [UIAlertAction actionWithTitle:#"Second Button Title" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//Add Another Action
}];
[alert addAction:actionOK];
[alert addAction:actionCANCEL];
[self presentViewController:alert animated:YES completion:nil];
Use this Method if any need ask me and for more Information use this Link http://useyourloaf.com/blog/uialertcontroller-changes-in-ios-8/
Use this code
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Title" message:#"Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * continueButton = [UIAlertAction actionWithTitle:#"Continue" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
[self performSegueWithIdentifier:#"createlogin" sender:self];
}];
UIAlertAction * cancelButton = [UIAlertAction actionWithTitle:#"cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
}];
[alert addAction:continueButton];
[alert addAction:cancelButton];
[self presentViewController:alert animated:YES completion:nil];
try this:
UIAlertController *controller = [UIAlertController alertControllerWithTitle:#"Your message title"
message:#"Enter your message here."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
NSLog(#"Ok Action");
}];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:#"Skip" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
NSLog(#"Cancel Action");
}];
[controller addAction:okAction];
[controller addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
I've a UIAlertController and I've a bunch of UIAlertAcion Buttons. Now I need to show one button with other Color rather than the same color.
For Ex
Button1
Button2
Button3
Button1 and button3 should be in blue
and
button2 should be in red.
Is it possible ? How?
Just Throw your thoughts
...
My Code:
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:#"Food Menu" message:#"Select the MenuItem" preferredStyle:UIAlertControllerStyleActionSheet];
for(int i= 0; i<[menus count];i++){
UIAlertAction *action = [UIAlertAction actionWithTitle:[menu objectAtIndex:i] style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
[actionSheet dismissViewControllerAnimated:YES completion:nil];
//do SomeWork
}];
if(i==currentIndex){
//Put button Color Red
}
else{
//put button color Blue
}
[actionSheet addAction:action];
}
UIAlertAction *cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction* action){
[actionSheet dismissViewControllerAnimated:YES completion:nil];
}];
[actionSheet addAction:cancel];
[self presentViewController:actionSheet animated:YES completion:nil];
}
do like change the alert style:UIAlertActionStyleDestructive
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:alertTitle
message:alertMessage
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Cancel", #"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(#"Cancel action");
}];
UIAlertAction *resetAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Reset", #"Reset action")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action)
{
NSLog(#"Reset action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"OK", #"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"OK action");
}];
[alertController addAction:cancelAction];
[alertController addAction:resetAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
I have problem, my app using default uiactivityviewcontroller, but it too big on iphone 6+. How to fix it ?
Button in my app too big
Button other app
My code
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[self presentViewController:mail animated:YES completion:NULL];
UIAlertController *actionSheet= [UIAlertController
alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *actionSheetDeleteDraft = [UIAlertAction
actionWithTitle:#"Delete Draft"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(#"Delete is pressed");
}];
UIAlertAction *actionSheetSaveDraft = [UIAlertAction
actionWithTitle:#"Save Draft"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(#"Save pressed");
}];
UIAlertAction *actionSheetCancel = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
NSLog(#"Cancel pressed");
}];
[actionSheet addAction:actionSheetDeleteDraft];
[actionSheet addAction:actionSheetSaveDraft];
[actionSheet addAction:actionSheetCancel];
[self presentViewController:actionSheet animated:YES completion:nil];
I have used below code to show action sheet with Tweet, Facebook and Cancel button.
- (void)shareApp:(id)sender {
NSString *strCancel = NSLocalizedString(#"Cancel", nil);
NSString *strTweet = NSLocalizedString(#"Tweet", nil);
NSString *strFacebook = NSLocalizedString(#"Facebook", nil);
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(#"Share your app", nil) message:nil preferredStyle:UIAlertControllerStyleActionSheet];
// Create the actions.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:strCancel style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
NSLog(#"Cancel action occured");
}];
UIAlertAction *tweetAction = [UIAlertAction actionWithTitle:strTweet style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(#"Tweet action here");
}];
UIAlertAction *facebookAction = [UIAlertAction actionWithTitle:strFacebook style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(#"Facebook action here");
}];
// Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:tweetAction];
[alertController addAction:facebookAction];
[self presentViewController:alertController animated:YES completion:nil];
}
Now, I want to add custom view i.e. logo + tweet on each element of action sheet.
How can this be implemented?
I got the solution through your code.Just refer the below coding
NSString *strCancel = NSLocalizedString(#"Cancel", nil);
NSString *strTweet = NSLocalizedString(#"Tweet", nil);
NSString *strFacebook = NSLocalizedString(#"Facebook", nil);
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(#"Share your app", nil) message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:strCancel style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
NSLog(#"Cancel action occured");
}];
UIAlertAction *tweetAction = [UIAlertAction actionWithTitle:strTweet style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(#"Tweet action here");
}];
UIAlertAction *facebookAction = [UIAlertAction actionWithTitle:strFacebook style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(#"Facebook action here");
}];
UIImage *accessoryImage = [UIImage imageNamed:#"Twitter.jpg"];
[tweetAction setValue:accessoryImage forKey:#"image"];
UIImage *accessoryFBImage = [UIImage imageNamed:#"Facebook.png"];
[facebookAction setValue:accessoryFBImage forKey:#"image"];
[alertController addAction:tweetAction];
[alertController addAction:facebookAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES
completion:nil];
i'm using UIAlertController . But on iPad with iOS 8, actionSheet show with popover arrow. Any ideas to hide that arrow?
Here is my code:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"this is alert controller" message:#"yeah" preferredStyle:UIAlertControllerStyleActionSheet];
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");
}];
UIAlertAction *deleteAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Delete", #"Delete action")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
NSLog(#"Delete action");
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[alertController addAction:deleteAction];
UIPopoverPresentationController *popover = alertController.popoverPresentationController;
if (popover) {
popover.sourceView = self.view;
popover.sourceRect = self.view.bounds;
popover.permittedArrowDirections = UIPopoverArrowDirectionUnknown;
}
[self presentViewController:alertController animated:YES completion:nil];
Solution :
use below line for remove arrow from action sheet
[yourAlertController.popoverPresentationController setPermittedArrowDirections:0];
Sample
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Test Action Sheet" message:#"Message" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action)
{
NSLog(#"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:#"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"OK action");
}];
UIAlertAction *otherAction = [UIAlertAction
actionWithTitle:#"Other"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"Otheraction");
}];
[alertController addAction:okAction];
[alertController addAction:otherAction];
[alertController addAction:cancelAction];
// Remove arrow from action sheet.
[alertController.popoverPresentationController setPermittedArrowDirections:0];
//For set action sheet to middle of view.
alertController.popoverPresentationController.sourceView = self.view;
alertController.popoverPresentationController.sourceRect = self.view.bounds;
[self presentViewController:alertController animated:YES completion:nil];
Output
Jageen's answer, in Swift:
popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
The selected answer does not center the alert if you have a nav/status bar. To exactly center your alert controller:
alertController.popoverPresentationController.sourceRect = [self sourceRectForCenteredAlertController];
alertController.popoverPresentationController.sourceView = self.view;
alertController.popoverPresentationController.permittedArrowDirections = 0;
With the convenience method:
- (CGRect)sourceRectForCenteredAlertController
{
CGRect sourceRect = CGRectZero;
sourceRect.origin.x = CGRectGetMidX(self.view.bounds)-self.view.frame.origin.x/2.0;
sourceRect.origin.y = CGRectGetMidY(self.view.bounds)-self.view.frame.origin.y/2.0;
return sourceRect;
}
Also, the alert controller does not stay centered if the view is rotated. To keep the alert controller centered you need to update the sourceRect after rotation. For example:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// Check if your alert controller is still being presented
if (alertController.presentingViewController) {
alertController.popoverPresentationController.sourceRect = [self sourceRectForCenteredAlertController];
}
}
Or, if you do not want to use rotation events, you can use the popoverPresentationController delegate method to reposition the popover:
- (void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing _Nonnull *)view
{
// Re-center with new rect
}
You're on the wrong track using UIPopoverPresentationController to display an alert. You just do not need that snipped of code...
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"this is alert controller" message:#"yeah" preferredStyle:UIAlertControllerStyleActionSheet];
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");
}];
UIAlertAction *deleteAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Delete", #"Delete action")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
NSLog(#"Delete action");
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[alertController addAction:deleteAction];
[self presentViewController:alertController animated:YES completion:nil];