Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 days ago.
Improve this question
I am facing a problem that navigationController?.popViewController not working properly, but sometime it works fine. My code is
DispatchQueue.main.async {
self.isUploadinProgress = false
self.prepareUI()
if status == true{
SessionController.sharedController.showWaitingAlert(message: "Programmed Firmware successfully.\n Restarting the radar. This will take about 1 minute to complete. Please be patient.")
SessionController.sharedController.isAutoRestartInProgress = true
} else{
SessionController.sharedController.statusAlertView?.dismiss(animated: false, completion: {
let message = errorMessage ?? "Error in programming the new program firmware."
if SessionController.sharedController.isCancelledFirmwareUpgrade == false{
let alert = UIAlertController(title: "",message: message,preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.cancel, handler: nil))
if let rootViewController = SessionController.sharedController.getRootViewController(){
rootViewController.present(alert, animated: true, completion: nil)
}
}else{
DispatchQueue.main.async {
self.navigationController?.popViewController(animated: false)
}
}
})
}
}
I'm having issue with
else{
DispatchQueue.main.async {
self.navigationController?.popViewController(animated: false)
}
The code segment inside async are executing but popviewController does not works some time. Sometimes its works fine. How can I solve this issue? Thanks in advance
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have declared a variable itemGlobalArray in AppDelegate class like below
var itemGlobalArray = NSMutableArray()
and trying to use it in a view controller like below
if (AppDelegate.itemGlobalArray).count > 0 //Gives error here
{
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "BasketVC") as? BasketVC
self.navigationController?.pushViewController(vc!, animated: true)
}
else {
let alert = UIAlertController(title: "Alert", message: "cart Empty", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
switch action.style{
case .default:
print("default")
case .cancel:
print("cancel")
case .destructive:
print("destructive")
}}))
self.present(alert, animated: true, completion: nil)
}
You must declare it like static member, static members can be used without instance:
class AppDelegate {
static var itemGlobalArray = NSMutableArray()
//...
}
Than you can use it in your case:
if (AppDelegate.itemGlobalArray).count > 0 { ... }
I think the better approach is to use UIApplication.shared.delegate as? AppDelegate
I am trying to show an (AlertController) Action sheet. But I am getting this waning in console " <_UIPopoverBackgroundVisualEffectView 0x7fd65ef76ec0> is being asked to animate its opacity. This will cause the effect to appear broken until opacity returns to 1. "
here is my code :-
extension GroupDataView {
func callDot (sender : UIButton)
{
let alert = UIAlertController(title: nil, message: nil,
preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Edit Group", style: .default , handler:{ (action)in
print("User click Edit Group")
}))
alert.addAction(UIAlertAction(title: "Create Folder", style: .default , handler:{ (action)in
print("User click Create Folder button")
}))
alert.addAction(UIAlertAction(title: "Delete Group", style: .destructive , handler:{ (action)in
print("User click Delete Group button")
}))
if let popoverController = alert.popoverPresentationController {
popoverController.sourceView = sender
popoverController.sourceRect = sender.bounds
self.present(alert, animated: true, completion: {
print("completion block")
})
}
else
{
self.present(alert, animated: true, completion: {
print("completion block")
})
}
}
}
I don't know why this warning is showing in console.
ActionSheet is coming properly but how to remove that warning?
Thanks
iOS Bug
See answer by #Hardy on Apple Developer Forum: Warning message from UIPopover †
... seems to be a bug in iOS. But it does not seem to be critical. Though - I believe - that sometimes the area below the UIAlertController is not correctly drawn. I can see for a short period of time a black background flashing although the background of the underlying view is not black.
† Cached on Google here
I had the same problem, and I resolved it by setting animated: false in self.present or/and self.dismiss function. See the last comment in https://forums.developer.apple.com/thread/53677
I realise this is an old question but I was just faced with the same issue and I found a workaround by delaying the presentation by 0.001 seconds so when you normally call this function I used this code for the delay
[self performSelector:#selector(viewBookmark:) withObject:bookmarkBarButton afterDelay:0.001];
Please excuse the OBJ-C but a similar delay in swift should have the same result of not displaying 2 views at the same time.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
My iOS app requires a customized group of alerts with similar functionality. Can I pack those in a class, so I don't have to copy paste in every UIViewController?
func displayAlert(msg:String, handler: (UIAlertAction) -> Void){...}
func successMsg (msg: String){...}
func failMsg(msg: String){...}
I was trying to pack them into a subclass(AlertViewController) of UIViewController, but then there was runtime error:"Attempt to present <...UIAlertController...> on <...UIAlertController...> whose view is not in the window hierachy!"
code in myViewController:
#IBAction func leave() {
let date = getDate()
let datePresent = dateForPresent()
if stID == nil || name == nil {
return
} else {
alert.displayAlert("\(datePresent)"){action in
self.request.getResult(self.request.leavePara(self.stID!, date: date, name: self.name!) ){ (result) in
dispatch_async(dispatch_get_main_queue(), {
let flag = result["result","out_Flag"].int
print(result)
let msg = result["result","out_nszRtn"].string!
if flag == 0 {
self.alert.successMsg(msg){ action in
self.performSegueWithIdentifier("personUnwind", sender: self)
}
}else {
self.alert.failMsg(msg)
}
})
}
}
}
}
Please use this code.
func displayAlertWithMessage(title: String, message: string){
let ac = UIAlertController(title: title, message: message, preferredStype: .Alert)
ac.addAction(UIAlertAction(title:"OK", style: .Default, handler: nil))
let rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController
rootVC?.presentViewController(ac, animated: true){}
}
If you want to display alert for specific view controller you can use this function.
func displayAlertWithMessage(viewController: UIViewController, title: String, message: string){
let ac = UIAlertController(title: title, message: message, preferredStype: .Alert)
ac.addAction(UIAlertAction(title:"OK", style: .Default, handler: nil))
viewController.presentViewController(ac, animated: true){}
}
And you can call it function with this code.
displayAlertWithMessage(self, animated: true, completion: nil)
I'm not sure what the error you were getting was about, but if you want to avoid a subclass you could implement a UIAlertViewController initializer that returns an object customized to your needs.
For example, the following could be written at a global level:
extension UIAlertController {
enum AlertMessageID {
case SaveFailed
}
convenience init(messageID: AlertMessageID) {
switch messageID {
case .SaveFailed:
self.init(title: "Oops", message: "The save failed.", preferredStyle: .Alert)
}
let okayButton = UIAlertAction(title: "Okay", style: .Default, handler: nil)
self.addAction(okayButton)
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So I have written some code, which is supposed to open an app called MobilePay. The problem is that my code won't be accepted in SWIFT when i try to make a else stament. Can somebody tell me what is wrong? - SWIFT tells me that there is an error.
The error is that it expects somekind of expression
#IBAction func Invoker(sender: AnyObject) {
let alertController = UIAlertController(title: "Betal", message: "Vælg en af nedstående betalingsmulighed", preferredStyle: .ActionSheet)
let ok1 = UIAlertAction(title: "MobilePay", style: .Default, handler: { (action) -> Void in
let url = NSURL(string: "mobilepay://")
if UIApplication.sharedApplication().canOpenURL(url!) == true
{
UIApplication.sharedApplication().openURL(url!)
} else {
}
let cancel = UIAlertAction(title: "Annuller", style: .Destructive) { (action) -> Void in
}
alertController.addAction(ok1)
alertController.addAction(cancel)
presentViewController(alertController, animated: true, completion: nil)
}
}
Looks like your braces are messed up:
#IBAction func Invoker(sender: AnyObject)
{
let alertController = UIAlertController(title: "Betal", message: "Vælg en af nedstående betalingsmulighed", preferredStyle: .ActionSheet)
let ok1 = UIAlertAction(title: "MobilePay", style: .Default, handler: { (action) -> Void in
let url = NSURL(string: "mobilepay://")!
if UIApplication.sharedApplication().canOpenURL(url)
{
UIApplication.sharedApplication().openURL(url)
}
else
{
// Unable to open url
}
}) // You were missing this brace
let cancel = UIAlertAction(title: "Annuller", style: .Destructive) { (action) -> Void in
// Respond to the cancel action
}
alertController.addAction(ok1)
alertController.addAction(cancel)
presentViewController(alertController, animated: true, completion: nil)
}
// You had an extra brace here
I want the button to go to the QaController if the word Hello is typed otherwise I want the alert to appear and then go back to the home screen (viewController) but it doesn't seem to work
#IBAction func submitButton(sender: AnyObject) {
if TextField.text.containsString("Hello"){
let secondViewController:QaController = QaController()
self.presentViewController(secondViewController, animated: true, completion: nil)
} else {
let alertController = UIAlertController(title: "Thank You!", message:
"We appreciate your feedback!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
let homeViewController:ViewController = ViewController()
self.presentViewController(homeViewController, animated: true, completion: nil)
}
}
The code works fine without the IF statements so I'm not sure whats wrong :/
What goes wrong is when I click Submit, when I put "Hello" in it shows a black screen with no error message, instead of displaying the correct view
Thanks in advance!
I think you are comparing your string with lower case, May be your text field value is "hello" not equal to "Hello". So please compare like
if(TextField.text!.caseInsensitiveCompare("Hello") == NSComparisonResult.OrderedSame)
{
// do your stuff
}
else{
print(TextField.text)
}
Hope it will help you.