Jageen Jageen - 1 year ago 161
Objective-C Question

Display UIAlertController from UIView/NSObject class

I have

working iOS application

In order to
support iOS8
, I am replacing
UIAlertView/UIActionSheet with

Problem :

For display UIAlertController I need presentViewController
method of UIViewController class.

But UIAlertView is display from classes which are
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

Answer Source

It looks like you are currently (pre-iOS8) triggering an alert view from within your view object. That's pretty bad practice, as in general alerts should be triggered from actions and logic. And that code should live in controllers.

I suggest you refactor your current code to move the logic that triggers the alert to the correct controller, and then you can easily upgrade to iOS 8 by using self as the controller.

If instead you're calling the alert from an outside object, then pass in the controller to the method that calls the alert. Somewhere upstream you must have knowledge of the controller.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download