Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
833 views
in Technique[技术] by (71.8m points)

objective c - Display UIAlertController from UIView/NSObject class

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I solved an essentially similar problem today. Like Jageen, I ran into a situation where I wanted to present a UIAlertController but from a non-UIViewController class. In my case, I wanted an alert to pop up when the failure block of a HTTP request is run.

This is what I used and unlike our friend here, it worked quite perfectly for me.

UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(errorAlert, animated: true, completion: nil)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...