I have working iOS application
In order to support iOS8
, I am replacing UIAlertView/UIActionSheet with
UIAlertController
.
Problem :
For display UIAlertController I need presentViewController
method of UIViewController class.
But UIAlertView is display from classes which are inherited
from
UIView or NSObject
,
I can not get [self presentViewController...]
method for obvious reason.
My Work :
I tried getting rootViewController form current window and display UIAlertController.
[[[UIApplication sharedApplication] keyWindow].rootViewController presentViewController ...]
but have some rotation problems like if my current view controller do not have rotation support it will rotate if UIAlertController is open.
Question :
Did any one faced same problem and have safe solution ?
if yes please provide me some example or give some guide
In Swift 3:
For Display UIAlertController in NSObject Class use below Code.
// Put Below Method in Your Global Helper Class.
In general, alerts should be handled in the view controller. Here's an example of the code required:
Swift 3
I had a situation where a subview contains a button to dismiss it. I present an alert to confirm the action. It sends a message to the delegate - which is the view controller containing the subview - to remove the subview
Originally I presented a UIAlertView from a UIView. Refactoring for UIAlertController, since the UIAlertController can't present itself like a UIAlertView can, I came up with the following (in Swift; easily translated to ObjC):
Add a protocol to the subview:
Add a delegate for the subview, and add a handler for the button/gesture tap:
Set the calling UIViewController as the delegate of the subview, e.g., in its viewDidLoad() method, and include protocol methods:
This avoids the need to find the topmost view controller, or pass references to view controllers to subviews (other than in an object/delegate relationship).