ios - How to call my custom alert controller function to display in other view controller? -


i have write custom alert view controller.but,i having error @ calling alert controller other view controller.it show me error described below.

error on console

2015-06-15 10:21:50.610 automobile[4197:62165] warning: attempt present <uialertcontroller: 0x7d11cf70> on <automobile.loadingalertviewcontroller: 0x7bfa55d0> view not in window hierarchy! 

here loadingalertviewcontroller

import uikit  class loadingalertviewcontroller: uialertcontroller {  var loadingalertcontroller : uialertcontroller!  override func viewdidload() {     super.viewdidload() }  override func didreceivememorywarning() {     super.didreceivememorywarning()     // dispose of resources can recreated. }  func displayloadingalert() -> uialertcontroller {      var controllertopresent = viewcontroller     if controllertopresent == nil {             controllertopresent = self     }     //create alert controller     loadingalertcontroller = uialertcontroller(title: "loading...", message: "we receiving data network.please wait.", preferredstyle: .alert)      let indicator = uiactivityindicatorview()     indicator.color = uicolor.redcolor()     indicator.settranslatesautoresizingmaskintoconstraints(false)     loadingalertcontroller.view.addsubview(indicator)      let views = ["pending" : loadingalertcontroller.view, "indicator" : indicator]     var constraints = nslayoutconstraint.constraintswithvisualformat("v:[indicator]-(7)-|", options: nil, metrics: nil, views: views)     constraints += nslayoutconstraint.constraintswithvisualformat("h:|[indicator]|", options: nil, metrics: nil, views: views)     loadingalertcontroller.view.addconstraints(constraints)      indicator.userinteractionenabled = false     indicator.startanimating()      controllertopresent!.presentviewcontroller(loadingalertcontroller, animated: true, completion: nil)      return loadingalertcontroller }  func dismissloadingalert() -> uialertcontroller {     loadingalertcontroller.dismissviewcontrolleranimated(true, completion: nil)     return loadingalertcontroller }  } 

then decalare loadingalertviewcontroller @ view controller want display every time request api.

this viewcontroller.

import uikit  class viewcontroller : uiviewcontroler{  var loadingalertcontroller : loadingalertcontroller! var api = myapi()  override func viewdidload() {         loadingalertcontroller = loadingalertviewcontroller()         api.getresults()         showalert(true)  }  func showalert(alert : bool){         if alert{            loadingalertcontroller.displayloadingalert(self)         }else{            loadingalertcontroller.dismissloadingalert()         }  } 

any help?please?if there way,how call view controller.thank you

change method so:

func displayloadingalert(viewcontroller: uiviewcontroller?) -> uialertcontroller {     var controllertopresent = viewcontroller     if controllertopresent == nil {     controllertopresent = self }  // of code  controllertopresent.presentviewcontroller(loadingalertcontroller, animated: true, completion: nil)  return loadingalertcontroller } 

then when you're calling alert:

loadingalertcontroller.displayloadingalert(self) 

alternatively: rename method displayloadingalert loadingalert

remove line:

self.presentviewcontroller(loadingalertcontroller, animated: true, completion: nil) 

then when calling insidethe showalert() method

let loadingalertcontroller = loadingalertcontroller.loadingalert() self.presentviewcontroller(loadingalertcontroller, animated: true, completion: nil) 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -