2016-10-03 3 views
0

У меня есть UIAlertController, работающий в моем проекте, который подключен через UIButton. Мой код работает нормально. Но каждый раз, когда я нажимал кнопку консоли Xcode, выводил предупреждение, как показано ниже, и я прошел все аналогичные вопросы задавали в stackOverflow, но ответа не было. Attempt to present UIAlertController on UIViewController whose view is not in the window hierarchyПредупреждение - попытка представить UIAlertController null в Swift

2016-10-03 19:14:35.854 xxxxxx[85186:7214348] Warning: Attempt to present<UIAlertController: 0x7f8d99450820> on <yyyyyyy.ViewController: 0x7f8d9a025200> which is already presenting (null) 

Мой код ниже:

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.moreButton.frame = CGRect(x:view.frame.width/2, y: view.frame.height/2, width: 100, height: 40) 

    moreButton.center = view.center 
    moreButton.backgroundColor = UIColor.darkText.withAlphaComponent(0.4) 
    moreButton.layer.masksToBounds = true 
    moreButton.setTitle("More", for: UIControlState()) 
    moreButton.setTitleColor(UIColor.white, for: UIControlState()) 
    moreButton.showsTouchWhenHighlighted = true 
    moreButton.layer.cornerRadius = 5.0 
    moreButton.addTarget(self, action: #selector(ViewController.moreButtonAction(_:)), for: .allTouchEvents) 

    self.view.addSubview(self.moreButton) 
    } 


    func moreButtonAction(_ sender: UIButton) { 

    DispatchQueue.main.async(execute: { 

    let myAlert = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet) 
    myAlert.view.tintColor = UIColor.purple.withAlphaComponent(0.8) 

    let myAction_1 = UIAlertAction(title: " Title 1", style: UIAlertActionStyle.default, handler: { 
     (action: UIAlertAction!) in 

    }) 

    let myAction_2 = UIAlertAction(title: " Title 2", style: UIAlertActionStyle.default, handler: { 
     (action: UIAlertAction!) in 

    }) 

    let myAction_3 = UIAlertAction(title: " Title 3", style: UIAlertActionStyle.default, handler: { 
     (action: UIAlertAction!) in 

    }) 

    let myAction_4 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: { 
     (action: UIAlertAction!) in 
    }) 

    myAlert.addAction(myAction_1) 
    myAlert.addAction(myAction_2) 
    myAlert.addAction(myAction_3) 
    myAlert.addAction(myAction_4) 

    self.present(myAlert, animated: true, completion: nil) 

    }) 
} 

Заранее спасибо

+2

показать нам код, представляющий предупредительного контроллер. – ozgur

+0

@ozgur Я только что загрузил свой код .... спасибо – Joe

ответ

1

ошибка говорит вам, что вы пытаетесь представить alertController, который уже активный. В противном случае вы можете проверить, имеет ли ваш контроллер активное предупреждение. Это приведет к удалению предупреждения. Таким образом, в основном переместить self.present row со следующим:

if !(self.navigationController?.visibleViewController?.isKind(of: UIAlertController.self))! { 
    self.present(myAlert, animated: true, completion: nil) 
} 

Заменить moreActionButton функцию следующим:

func moreButtonAction(_ sender: UIButton) { 
    let myAlert = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet) 
    myAlert.view.tintColor = UIColor.purple.withAlphaComponent(0.8) 

    let myAction_1 = UIAlertAction(title: " Title 1", style: UIAlertActionStyle.default, handler: { 
     (action: UIAlertAction!) in 

    }) 

    let myAction_2 = UIAlertAction(title: " Title 2", style: UIAlertActionStyle.default, handler: { 
     (action: UIAlertAction!) in 

    }) 

    let myAction_3 = UIAlertAction(title: " Title 3", style: UIAlertActionStyle.default, handler: { 
     (action: UIAlertAction!) in 
     print("3") 

    }) 

    let myAction_4 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: { 
     (action: UIAlertAction!) in 
    }) 

    myAlert.addAction(myAction_1) 
    myAlert.addAction(myAction_2) 
    myAlert.addAction(myAction_3) 
    myAlert.addAction(myAction_4) 

    if !(self.navigationController?.visibleViewController?.isKind(of: UIAlertController.self))! { 
     self.present(myAlert, animated: true, completion: nil) 
    } 
} 
+0

@Joe, проверьте обновление. –

+0

где я могу поставить этот код. я попытался в моем alertView, но я получаю крах ... спасибо – Joe

+0

@Joe, проверьте обновление. Поэтому просто замените функцию moreActionButton той, которую я добавил в ответ. –

Смежные вопросы