iOS 8 iPad only bug with action sheets - ios

On an iPhone running iOS 8, the code below causes an action sheet to pop up. However, on an iPad running iOS 8 the code below does not cause an action sheet to pop up and instead nothing happens.
NSUserDefaults *defauj = [NSUserDefaults standardUserDefaults];
NSArray *cod = [defauj objectForKey:#"customlistofstuff"];
UIActionSheet* actionSheet = [[UIActionSheet alloc] init];
actionSheet.delegate = self;
for(int i=0;i<[cod count];i++)
{
[actionSheet addButtonWithTitle:[cod objectAtIndex:i]];
}
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:#"None"];
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];

Try this:
[actionSheet showInView:[UIApplication sharedApplication].keyWindow.rootViewController.view];
It looks like you can't present action sheets on UIWindows directly anymore, you have to present them on an actual view that is managed by a view controller, so the root view controller's view is perfect for this.
I think this has less to do with the fact that UIActionSheet is deprecated (and you can't just magically switch to UIAlertController just yet if you need to support iOS 7), and more to do with the way their presentation is handled in the underlying implementation — I'm guessing it now relies on the view the sheet is presented in having a view controller, which is not true for windows.
EDIT: If you have a view controller presented modally over the top of the root view controller, this obviously won't work as the root view controller's view is no longer visible. You'll need to present the sheet in a view that is currently visible, e.g. the view of the current view controller (self.view).

I bet this has something to do with UIActionSheet being deprecated in iOS 8. You're supposed to use UIAlertController instead with a preferredStyle of UIAlertControllerStyleActionSheet.
Try using that instead and see if it works. You'll have to use blocks instead of methods, but that shouldn't be too hard to do.

According to Apple's Human Interface Guidelines about Temporary Views, A cancel button should only be used when the view presenting the action sheet is a popover, because, according to the HIG, users can tap outside the popover to dismiss the action sheet.
Therefore, cancel buttons do not work on iPads.
UIActionSheet has also been deprecated, and you should use UIAlertController instead.

Related

How to get reference to UIPopoverController when using adaptive segue?

In my iOS 7 app, I detected if a segue was a popover via this check in prepareForSegue:
if ([segue isKindOfClass:[UIStoryboardPopoverSegue class]])
But now that I am using adaptive segues, the Present as Popover segue is no longer returning true in the above check. This is because segue is no longer a UIStoryboardPopoverSegue, instead it is a UIStoryboardPopoverPresentationSegue. However, one cannot simply add the Presentation word because that's not defined.
What is the appropriate way to detect when the segue is a popover from an adaptive segue, as opposed to a full screen modal presentation?
And, how do you get a reference to the popover for iOS 8? The following is what I'm doing for iOS 7 but again because it's not a UIStoryboardPopoverSegue this will cause a crash.
UIPopoverController *popover = ((UIStoryboardPopoverSegue *)segue).popoverController;
popover.popoverContentSize = CGSizeMake(380, 1000);
There actually was no need to get a reference to the popover for iOS 8. You can access the popoverPresentationController directly in the view controller that's presented. Then use dismissViewControllerAnimated to dismiss the view controller. You can set the popover content size directly in the view controller that's being presented via preferredContentSize. I found I had no need to obtain a reference in prepareForSegue, at least when running on iOS 8. iOS 7 is a different story.
Elaborating on Joey's answer, which led me to what seems the new manner of achieving what we used to do with UIPopoverController.
This code in prepareForSegue:Sender:
UIViewController *destination = segue.destinationViewController;
UIPopoverPresentationController *ppc = destination.popoverPresentationController;
ppc.delegate = self;
is a simple way to successfully set your view controller as delegate of a UIPopoverPresentationController much the same as you are probably used to doing with the old UIPopoverController.
And, of course, while you are at it you'll probably add:
[destination setPreferredContentSize:CGSizeMake(300.00f, 300.00f)];
if you were in the habit of setting UIPopoverController size here as well.
This may not be the most foolproof way to do it but you could check the popoverPresentationController property on your destination view controller. From there you can configure the popover anchor and such.
Check the "Configuring a Popover for Display" section of the UIPopoverPresentationController doc. (Not sure if we're allow to link to them at this point, are we?)
Note too that now we're talking about UIPopover*Presentation*Controllers, not UIPopoverControllers. It's a little confusing...

What is the large popup view in UIKit called?

I am writing an iPhone application where I want to utilize the large popup view, but I don't know what it's called. I have a picure on it.
I mean the middle square that isn't shadowed. It has the title "Köp mer utrymme". I know it's from an iPad, but I'm pretty sure a similiar one exists on the iPhone, for example, the iTunes-store agreements. I looked in Apple's UIKit User Interface Catalog, but I couldn't found it there.
Does anyone know what it's called or how to get it?
On iPad it could be a UIPopoverController with a custom view controller inside it, or a modally presented view controller. You can't use that on iPhone (at least not in the same way, popovers don't exist and modal views are full screen).
On iPhone you could use a UIAlertView, or you could search github / cocoacontrols for a suitable 3rd party implementation.
The view on the picture is presented modally. When presenting a view modally, you can customize the presentstion style. The default style is UIModalPresentationFullscreen, but the style in the picture is UIModalPresentationFormSheet.
To present a view controller in that style, you first create an instance of the view controller and then set its style.
MyViewController *vc = [[MyViewController alloc] init];
vc.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:vc animated:YES completion:nil];
Note that isn't possible to change the presentation style for iPhone. (I must have imagined that the iTunes-store agreement wasn't fullscreen.)
Use KGModal.h and KGModal.m class. For a PopVieController is is best ....and easy to make and add control over it and no need to set frame for orientation ....
Find these 2 Class (KGModal.h and KGModal.m)...

PresentViewController without fullscreen

Quick question again. When I use presentViewController to present a new viewcontroller on top of my current one it is full screen. How do I get it to present a specific size? Or should I use another method.
Code:
- (IBAction)showProfile:(id)sender {
ProfileView *profileTop = [[ProfileView alloc] init];
profileTop.delegate = self;
[self presentViewController:profileTop animated:YES completion:nil];
}
If you are developing an app for iPad then you can make use of viewController's modalPresentationStyle property, You need to set for presenting viewController.
It has 4 values for that variable.
UIModalPresentationFullScreen = 0,
UIModalPresentationPageSheet,
UIModalPresentationFormSheet,
UIModalPresentationCurrentContext
You can select which one suites you the best.
I'd suggest doing a little more research, specifically in Apple's reference. Of note, there is this quote from the View Controller Programming Guide (http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html):
Presentation Styles for Modal Views
For iPad apps, you can present content using several different styles. In iPhone apps, presented views always cover the visible portion of the window, but when running on an iPad, view controllers use the value in their modalPresentationStyle property to determine their appearance when presented. Different options for this property allow you to present the view controller so that it fills all or only part of the screen.
And specifically, on the API reference page for presentViewController (http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/presentViewController:animated:completion:):
On iPhone and iPod touch, the presented view is always full screen. On iPad, the presentation depends on the value in the modalPresentationStyle property.
Only the iPad appears to have any support for non-fullscreen modals.
On iPad you can just use:
[viewcontroller setModalPresentationStyle:UIModalPresentationFormSheet];
example:
LoginDialogViewController * login_dialog = [[LoginDialogViewController alloc] init];
[login_dialog setModalPresentationStyle:UIModalPresentationFormSheet];
[self presentViewController:login_dialog animated:true completion:nil];
You can use the same code. Then adjust the view size in the xib file. See the below figure

iPad crash with UIActionSheet displayed from child view controller

I apologize if this has been asked but I can't seem to find it anywhere. I even recreated my issue in a demo project in case any of you want to see it first-hand, although I don't know where I should post it.
I have a xibless UINavigationController based app. Some of my child ViewControllers have a button on the right side at the top that then displays a UIActionSheet. My app is designed for iPhone and iPad, so when I get ready to display the UIActionSheet I do:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:[NSString stringWithFormat:#"%# Menu", [self title]] delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Email", #"Print", nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleDefault];
if ([actionSheet respondsToSelector:#selector(showFromBarButtonItem:animated:)])
[actionSheet showFromBarButtonItem:[[self navigationItem] rightBarButtonItem] animated:YES];
else [actionSheet showInView:[self view]];
[actionSheet release];
On iPad, I'm trying to show the UIActionSheet attached to the right bar button and on iPhone it should slide in from the bottom. All of this works beautifully.
Unfortunately, if you tap the button and show the menu on iPad, but then tap the back button on the top left side of the app, the menu doesn't dismiss. Instead UINavigationController dutifully pops back and the UIActionSheet is still there. If you try to tap something on the menu you of course get a crash. If the user would have tapped anything else on the screen instead of the Back button, the menu properly dismisses.
If you try this test on iPhone, everything works as expected. There is no issue.
My demo project has an AppDelegate and a ViewController and that's about it. The AppDelegate builds an NSDictionary of NSDictionaries just so I have a model I can recurse through to demonstrate the issue. The ViewController shows all of the keys of the dictionary and if the corresponding value is an NSDictionary, you can tap it to drill down.
This is an interesting problem. Here's what the UIActionSheet Class Reference has to say.
On iPad, this method presents the action sheet in a popover and adds
the toolbar that owns the button to the popover’s list of passthrough
views. Thus, taps in the toolbar result in the action methods of the
corresponding toolbar items being called. If you want the popover to
be dismissed when a different toolbar item is tapped, you must
implement that behavior in your action handler methods.
So when you display the action sheet, it's automatically creating a UIPopoverController and set the containing toolbar (or navigation bar) as the popover's passthrough views, allowing touch events to continue. I think the best bet is to create an instance variable for your action sheet and to force it to dismiss if it is visible in -viewWillDisappear:.
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if (self.actionSheet.window) // If action sheet is on screen, window is non-nil
[self.actionSheet dismissWithClickedButtonIndex:self.actionSheet.cancelButtonIndex animated:animated];
}
Have you tried force-dismissing the ActionSheet on viewWillDisappear?
Try this:
// In MyViewController.m
- (void)viewWillDisappear:(BOOL)animated {
[actionSheet dismissWithClickedButtonIndex:nil animated:animated];
}
*The crash sounds like a possible EXC_BAD_ACCESS. You might be losing your pointer reference to 'actionSheet' when you change views due to your release. Might be good to hang on to a reference to actionSheet in your .h file and manage the timing of your release.
*Also see the docs for info about the dismiss message: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIActionSheet_Class/Reference/Reference.html

UIActionSheet cancel button strange behaviour

I have a UIBarButtonItem opening an action sheet to offer users choices about what to do. Everything works as expected unless I try to click on the "Cancel" button. The target of the button appears to have moved up from where it should be. I can only activate it by clicking somewhere in the middle of the "Cancel" and "Ok" buttons.
I've tried at action sheets in other applications and they work fine, so it's not just my big thumb. The action sheet is opening in a UIViewController
- (void)showOpenOptions
{
UIActionSheet *sheet = [[UIActionSheet alloc]
initWithTitle:NSLocalizedString(#"Open link in external application?", #"Open in external application")
delegate:self
cancelButtonTitle:NSLocalizedString(#"Cancel", #"Cancel")
destructiveButtonTitle:NSLocalizedString(#"Open Link", #"Open Link")
otherButtonTitles:nil];
[sheet showInView:self.view];
[sheet release];
}
Instead of passing the current view controller's view to the action sheet, use the showFromTabBar: method of UIActionSheet.
The Right Way
This will give the correct tappable area:
[actionSheet showFromTabBar:self.tabBarController.tabBar];
The Wrong Way
This will put the tappable area in the wrong place (if you're using a tab bar or toolbar):
[actionSheet showInView:self.view];
If you're using a toolbar, use the showFromToolbar: method instead. You'll need a reference to the toolbar, most likely an ivar
[actionSheet showFromToolbar:self.myToolbar];
My Old Answer Also works, but is hacky:
Just found a possible answer:
01-Dec-2008 10:22 PM Tom Saxton:
I looked at this bug some more, and it seems to be an issue with the tabbar.
If you call UIActionSheet's [sheet showInView:self.view] from a view controller that is a child of a UITabViewController, then the hit testing on the cancel button fails in that portion of the UIActionSheet that lies above the tabbar's view.
If you instead pass in the UITabBarController's view, then the UIActionSheet acts as expected.
NOTE: in iPhone OS 2.1 and earlier, the UIActionSheet came up from the top of the tab bar when you pass the child view, but in 2.2, it comes up from the bottom of the tab bar, and thus covers the tab view.
http://openradar.appspot.com/6410780
Edit: It works correctly when I change the view to be the tab bar's view
[sheet showInView:self.parentViewController.tabBarController.view];
I found an answer over here that works.
using: [filterActionSheet showInView:[self.view window]];
i tried a few ways to get to my tab bar and they way this app is set up it seem convoluted...
Instead use:
[sheet showFromTabBar:theTabBar];
Here is the fix.Try this:
[actionsheet showInView:[UIApplication sharedApplication].keyWindow];
I think a combination of three of the answers is the right way of handling this:
[actionSheet showFromTabBar:self.tabBarController.tabBar];
i.e., use showFromTabBar (that's why it exists) and you don't need the parentViewController as Nathan pointed out (in fact, self.parentViewController.tabBarController.tabBar returns nil for me.
FYI - had the same problem with UIDocumentInteractionController's actionsheet stepping on the tabbar. Used the following to fix.
UIViewController *parentView = [[self parentViewController] parentViewController];
[docController presentOptionsMenuFromRect: rect inView: parentView.view animated:YES];
write simplite code
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
this work fine

Resources