Cant dismiss my previous alertcontroller completely
Asked Answered
V

1

0

I am trying to use one single alert controller which include multiple alert showing functions and one dismiss function.But I am having this warning in my console and my other alert don't show.I wonder why?and the solution for that.

Here is my alert controller

import UIKit

class MyAlertViewController: UIViewController {

var myAlertController : UIAlertController!

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func displayLoadingAlert(viewController: UIViewController?) -> UIAlertController {

    var controllerToPresent = viewController
    if controllerToPresent == nil {
        controllerToPresent = self
    }

    //create an alert controller
    myAlertController = UIAlertController(title: "Loading...", message: “We are receiving data,please wait“, preferredStyle: .ActionSheet)

    controllerToPresent!.presentViewController(myAlertController, animated: true, completion: nil)

    return myAlertController
}

func connectionErrorAlert(viewController: UIViewController?) -> UIAlertController {

    var controllerToPresent = viewController
    if controllerToPresent == nil {
        controllerToPresent = self
    }

    //create an alert controller
    myAlertController = UIAlertController(title: "Not Connected", message: “No internet Connection”, preferredStyle: .Alert)
    let defaultAction = UIAlertAction(title: "OK", style: .Default,handler:nil)
    myAlertController.addAction(defaultAction)

    controllerToPresent!.presentViewController(myAlertController, animated: true, completion: nil)

    return myAlertController
}

func requestTimeOutErrorAlert(viewController: UIViewController?) -> UIAlertController {

    var controllerToPresent = viewController
    if controllerToPresent == nil {
        controllerToPresent = self
    }

    //create an alert controller
    myAlertController = UIAlertController(title: "Request Time Out", message: “Please Click Retry”, preferredStyle: .Alert)
    let defaultAction = UIAlertAction(title: "OK", style: .Default,handler:nil)
    myAlertController.addAction(defaultAction)

    controllerToPresent!.presentViewController(myAlertController, animated: true, completion: nil)

    return myAlertController
}

func dismissLoadingAlert(){
    myAlertController.dismissViewControllerAnimated(true, completion: nil)
}

}

I use to do dismissLoadingAlert() when I get the results from API.But,when I don't get the results from API.i used to do this delegate method from my protocol.

func didNotReceiveAPIResults(results: Bool,error:NSError){
    dispatch_async(dispatch_get_main_queue(), {
        // This condition will be enter if we dont get the results and show user with alert.
        if (results) {
            // I have done dismiss first data receiving alert,what do i doing wrong?
            self.myAlertController.dismissLoadingAlert()
            if error.localizedDescription == "The Internet connection appears to be offline."{
                self.myAlertController.connectionErrorAlert(self)
                self.carTableView.hidden=true
                self.retryButton?.hidden=false
                self.retryButton?.enabled=true
            }
            if error.localizedDescription == "Request Time Out."{
                self.myAlertController.requestTimeOutErrorAlert(self)
                self.carTableView.hidden=true
                self.retryButton?.hidden=false
                self.retryButton?.enabled=true
            }

        }else{
            self.myAlertController.displayLoadingAlert(self)
            self.retryButton?.hidden=true
            self.retryButton?.enabled=false
            self.carTableView.hidden=false
            self.carTableView.reloadData()
        }
    })
}
Vestpocket answered 15/6, 2015 at 8:9 Comment(0)
C
1

You are on the wrong track. You should not subclass this controller.

The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

If you need a convenience class, refactor using a NSObject subclass that presents dynamic UIAlertController instances.

Candiscandle answered 16/6, 2015 at 8:21 Comment(6)
I am new to swift.I don't know what to do now.Is there any way that i can write my custom alert view to show and dismiss successfully.Thank you.I really need to call this custom alert from my view controllers classVestpocket
Short answer - you should not. - Long answer, just subclass a normal UIViewController and present it modally. For the types of alerts you want to display, you might also want to look at 3rd party frameworks such as MBProgressHUD.Candiscandle
yeah,thanks for the help,i now subclass to UIViewController.But,i still cant solving the error which was "Attempt to present xxxxxx.... while a presentation is in progress"Vestpocket
Don't worry about the error. It is caused by you messing with private APIs which won't be the case once you cease subclassing UIAlerController.Candiscandle
No,I changed my subclass to UIViewController.But,the problem is after i dismiss first alert which was title with "Loading..." and display the second alert because of internet connection,the second alert doesn't display and show the above warning.Why i cant successfully dismiss my first alertVestpocket
If you can't do it, you should stick with my "short answer" ;-).Candiscandle

© 2022 - 2024 — McMap. All rights reserved.