1

In order to allow for better customization in my app, I want to put UIAlertController within a UIViewController subclass. The alert should be a child of the UIViewController to allow for more flexibility.

What is the best way to achieve this?

I have tried so far to add the alert as a child view controller. When I want the alert to show up, I present the container view controller modally.

I have also tried to call presentViewController on the view controller (with the alert as a parameter). However, I'm not sure what the correct approach is to achieve what I am looking for.

Edit: Neither of these solutions were working as I had hoped, but I don't know if that is because I did something wrong or because the approach is wrong.

Edit 2: The added functionality is providing UIKeyCommands to make selection of the alert options easier. I am not subclassing UIAlertController for two reasons: (1) the documentation states that it should not be subclassed, and (2) adding key commands UIAlertController doesn't work.

erdekhayser
  • 6,537
  • 2
  • 37
  • 69

2 Answers2

1

Could you please explain what sort of flexibility such an approach provides? I'm not doubting that it does, as I'm relatively new to iOS development, but I can't quite figure out what you're trying to achieve using a child view controller.

If you simply want to present a UIAlertController from your view controller, just create the alert controller, configure it, and use presentViewController to display it.

As a general approach, however, I follow the advice found in the top answer in this thread. It suggests using either a UIAlertController subclass or category with a custom show method that provides the alert controller with its own window so that alerts can be presented 'globally' so to speak.

Community
  • 1
  • 1
g.Rapido
  • 147
  • 1
  • 9
  • I added some more information in the second edit. I'll look at the linked thread and get back to you. Thanks! – erdekhayser Jan 16 '16 at 22:14
  • It turns out that the best way to achieve this is to call `presentViewController` from the container view controller, and have this VC over the current context. – erdekhayser Jan 17 '16 at 03:56
0

I don't think your question is absolutely clear, but maybe you want a custom pop-up view controller, not alert? Then you probably need something like STPopupController - it's customisable and easy to use.

Mion Kovalski
  • 19
  • 1
  • 7