Reputation: 6657
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.
Upvotes: 1
Views: 1408
Reputation: 157
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.
Upvotes: 1
Reputation: 19
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.
Upvotes: 0