How to present UIAlertController in Popover for iPhone with iOS 8 - ios8

I want to show a popover from navigation bar's title which is successfully configured as a button with multiple choices like Alert Controller on iPhone application. The following code is called when the title is tapped, but it shows action sheet on the bottom instead of popover. Can you help me find what's wrong? Thanks.
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil
message: nil
preferredStyle: UIAlertControllerStyleActionSheet];
[alertController addAction: [UIAlertAction actionWithTitle:#"Close" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// dismiss viewcontroller
[self dismissViewControllerAnimated:YES completion:nil];
}]];
[alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(#"Cancel", nil) style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// Handle Cancel
// do nothing
}]];
alertController.modalPresentationStyle = UIModalPresentationPopover;
alertController.preferredContentSize = CGSizeMake(250.0, 180.0);
UIPopoverPresentationController *popoverPresentation = alertController.popoverPresentationController;
popoverPresentation.sourceRect = self.parentViewController.navigationItem.titleView.frame;
popoverPresentation.sourceView = self.view;
popoverPresentation.permittedArrowDirections = UIPopoverArrowDirectionUp;
popoverPresentation.delegate = self;
[self presentViewController: alertController animated: YES completion: nil];

You cannot do it with code (programmatically) with UIAlertController as it has no popoverPresentationController on the iPhone. It is available only in iPad.
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:PDFDataArray applicationActivities:nil];
if ([activityVC respondsToSelector:#selector(popoverPresentationController)] ) {
NSLog(#"Popover available for this device type");
}
I've successfully presented popovers on the iPhone in the past, using adaptive segue style Present As Popover in interface-builder/storyboard and implementing adaptivePresentationStyleForPresentationController delegate method to return the correct UIModalPresentationStyle. You can also try this approach.

Related

Removing blur effect iOS 8 takes a few seconds

I implemented UIBlurEffect in my view controller when my UIAlertController appears via self.view addSubView method. However, in the action code where I had dismissed the alert controller, it takes a couple of seconds of lag before the blur effect is removed. Is there any reason for the lag?
// Create effect
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
// Add effect to an effect view
UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
visualEffectView.frame = self.view.frame;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Input Required" message:#"You are using UIAlertController" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = nil;
if(self.m_txtStaffID.text.length == 0)
{
// Add the effect!
[self.view addSubview:visualEffectView];
cancel = [UIAlertAction actionWithTitle:#"Dismiss" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action)
{
[self.m_txtStaffID becomeFirstResponder];
[visualEffectView removeFromSuperview]; // this takes a full 1-2 seconds to take effect
[alert dismissViewControllerAnimated:YES completion:nil]; // but this is almost immediately!
}];
[alert addAction:cancel];
alert.message = #"Enter your staff ID and try again";
[self presentViewController:alert animated:YES completion:nil];
return;
}

make modally presentation from UISplitViewControllers primary view to detail view

I have a plus button in my primary view controller of a UISplitViewController and i want to present something modally in my detail view, just like apple does when adding a new contact in address book in iPad. I have tried everything but nothing. I managed to do it but when i am trying to embed my presented view controller into a UINavigation controller then my presented controller covers the full screen. Any suggestions? Here is my code:
UINavigationController *navController = [self.splitViewController.viewControllers lastObject];
DetailTableViewController *controller = (DetailTableViewController *)navController.topViewController;
controller.definesPresentationContext = YES;
controller.providesPresentationContextTransitionStyle = YES;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
EditTableViewController *etvc = (EditTableViewController *)[storyboard instantiateViewControllerWithIdentifier:#"EditTableViewController"];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:etvc];
etvc.patient = patient;
if (IDIOM == IPAD)
{
etvc.modalPresentationStyle = UIModalPresentationCurrentContext;
[controller presentViewController:nav animated:YES completion:nil];
} else {
[self presentViewController:nav animated:YES completion:nil];
}
I just successfully solved this problem by creating a custom segue whose implementation is:
- (void)perform
{
UIViewController *ctrl = self.sourceViewController;
UIViewController *dest = self.destinationViewController;
dest.modalPresentationStyle = UIModalPresentationCurrentContext;
[ctrl presentViewController:dest animated:YES completion:nil];
}
I'm seeing the behavior I want by invoking this segue from my detail view controller on the modal view I want to overlay it.
I think where your code is going haywire is here:
etvc.modalPresentationStyle = UIModalPresentationCurrentContext;
I think it should be:
nav.modalPresentationStyle = UIModalPresentationCurrentContext;
Though I haven't tested it.
Note that the Apple docs suggest that modalPresentationStyle is ignored on the iPhone (or on "horizontally compact devices"), so your "IS_IPAD" check may be redundant.
Hope this helps!

Use UIAlertView and UIAlertController for same app

I want to display an alert. But as uialertView is deprecated for iOS 8, what can I do to display the alert for both kind of OS?
Try this code snippet to display the alert view in iOS8.This will definitely work.
- (IBAction)btn1Clicked:(UIButton *)sender {
// Alert style
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"ALERT!" message:#"What will you do?" preferredStyle:UIAlertControllerStyleAlert];
__weak ViewController *wself = self;
// Make choices for the user using alert actions.
**UIAlertAction** *doSomethingAction = [UIAlertAction actionWithTitle:#"I'm doing something" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
__strong ViewController *sself = wself;
sself.alertResponseLabel.text = #"You did something!";
}];
UIAlertAction *doNothingAction = [UIAlertAction actionWithTitle:#"I'm totally ignoring this" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
__strong ViewController *sself = wself;
sself.alertResponseLabel.text = #"OK, just ignore me...";
}];
// Add actions to the controller so they will appear
[**alert addAction:doSomethingAction];
[alert addAction:doNothingAction];**
alert.view.tintColor = [UIColor blackColor];
**[self presentViewController:alert animated:YES completion:nil];** }

iOS 8 UIAlertView not showing buttons

Display some alerts in my application using the UIAlertView in iOS 7.1 works perfectly in iOS 8 the alert appears, but without the buttons to cancel, OK, and others ... This causes the user can not close the alert and consequently gets stuck on this screen, having to close the application.
I tried to implement the UIAlertView and previous versions for iOS UIAlertController 8, see the code below:
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
UIAlertView *alerta = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"s000xS2", #"Alerta") message:NSLocalizedString(#"s000xS40", nil) delegate:self cancelButtonTitle:NSLocalizedString(#"s000xS34", #"Não") otherButtonTitles:NSLocalizedString(#"s000xS35", #"Sim"), nil];
[alerta show];
}else{
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:NSLocalizedString(#"s000xS2", #"Alerta")
message:NSLocalizedString(#"s000xS40", nil)
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* sim = [UIAlertAction
actionWithTitle:NSLocalizedString(#"s000xS35", #"Sim")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[Util abrirSite:[[[Player sharedPlayer] emissora] site]];
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* nao = [UIAlertAction
actionWithTitle:NSLocalizedString(#"s000xS34", #"Não")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:sim];
[alert addAction:nao];
[self presentViewController:alert animated:NO completion:nil];
}
With this code I have the same problem, the buttons are not displayed in the alert, any suggestions to get around this?
Note, I'm using strings for internationalization, they usually work, already tested by placing a string directly (# "...") but it did not work.
Try this:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"ALERTA!" message:#"What will you do?" **preferredStyle:UIAlertControllerStyleAlert**];
__weak ViewController *wself = self;
UIAlertAction *nao = [UIAlertAction actionWithTitle:#"I'm doing something" ***style:UIAlertActionStyleCancel*** handler:^(UIAlertAction *action) {
__strong ViewController *sself = wself;
sself.**lbl**.text = #"You did something!"; **//the text "You did something!" gets displayed on a label(if created) named lbl**
}];
[alert addAction:nao];
[self presentViewController:alert animated:NO completion:nil];

iOS 7: Restrict device current orientation when modal view controller is presented

I have a UIViewController which contains a UICollectionView. On tapping any of the UICollectionViewCell I present a modal view controller.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
PopViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"DetailsView"];
vc.view.backgroundColor = [UIColor clearColor];
vc.transitioningDelegate = self;
vc.modalPresentationStyle = UIModalPresentationCustom;
[self presentViewController:vc animated:YES completion:nil];
The PopViewController shows up correctly. Now I want to restrict the device orientation when PopViewController is being presented. That is if PopViewController is presented in portrait mode then it should not change to landscape even if I switch to landscape mode (using Rotate Left or Right in simulator) until I dismiss the PopViewController.
I have used following code in PopViewController:
-(BOOL)shouldAutorotate {
return NO;
}
What else (or instead) is needed to lock the pop up view to the current orientation?
in your modal controller try to add this, also (iOS > 6)
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
to support iOS 5 or below you must additional add:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

Resources