2

I have the situation where the alert is showing up in the upper left-hand corner of the screen (and cut off), as described in UIAlertController is moved to buggy position at top of screen when it calls `presentViewController:`

My code is copy and paste from Apple's documentation:

- (void) alertHere
{

    UIAlertController* alert = [UIAlertController 
alertControllerWithTitle:@"My Alert"
                message:@"This is an alert."

   preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                handler:^(UIAlertAction * action) {}];

    [alert addAction:defaultAction];

    [self presentViewController:alert animated:YES 
    completion:nil];
}

I am calling this from a UIViewController.

I simply don't understand the stackoverflow discussion referenced above, nor the answers. I need something a poor simple programmer from steppes can understand. Apple doesn't consider this a bug (apparently), but I don't understand why and I certainly don't understand how to fix it.

Thank you for a nice simple solution, or at least a simple description of what I'm doing wrong.

Reza Dehnavi
  • 2,256
  • 3
  • 16
  • 30
user938797
  • 167
  • 3
  • 15
  • The question you linked is about presenting another view controller from an alert controller. Are you doing the same? – rmaddy Sep 08 '18 at 18:33
  • Frankly, I am not sure. I am calling this from my main viewcontroller. This is the limit of my knowledge! It is not clear to me from the documentation that the UIAlertController is a viewcontroller (although I guess that would make sense). But if it is, where AM I supposed to call it from? Thank you. – user938797 Sep 08 '18 at 19:06
  • Yes, UIAlertController is a view controller. The alert should be presented from another view controller, just like any other view controller would. The problem in the other question you linked is from someone presenting an alert controller but then presenting another view controller from the alert controller. It does not seem that you are doing that so the other question you linked is irrelevant to your issue. – rmaddy Sep 08 '18 at 20:50
  • Thank you rmaddy, but what am I doing wrong? Because as far as I can tell I'm presenting the uialertcontroller from another view controller. – user938797 Sep 08 '18 at 21:01
  • There's no obvious problem from the code you posted. – rmaddy Sep 08 '18 at 21:04
  • Oy vey. Thanks, I appreciate the time, but it's not working. – user938797 Sep 08 '18 at 21:20
  • When/where do you call `alertHere` ? – koen Sep 09 '18 at 11:28
  • I have try calling it from viewDidAppear and viewDidLoad (as well as a few other places). – user938797 Sep 10 '18 at 00:38

2 Answers2

1

According to this post

Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy

It seems you call alertHere from viewDidLoad so for solved your problem you must call it in ViewDidAppear.

Reza Dehnavi
  • 2,256
  • 3
  • 16
  • 30
  • Sorry, tried this and it didn't work. Acts just the same. No error reported, just appears in the wrong spot. Thanks though. – user938797 Sep 09 '18 at 22:55
  • There is not much more to share, here is the viewDidAppear in the main viewController. I have eliminated everything else for this test. - (void) viewDidAppear: (BOOL) inAnimated { [super viewDidAppear: inAnimated]; [self alertHere]; } – user938797 Sep 10 '18 at 17:45
1

The answer lies here: How to present UIAlertController when not in a view controller?

It took my a while to understand it, but once implemented, it works.

user938797
  • 167
  • 3
  • 15