2016-10-03 62 views
0

我有一個UIAlertController在我的項目,該項目是通過UIButton.My代碼連接運行工作ok.But,每次當我按下按鈕的Xcode控制檯如下打印警報和我的經歷所有類似的問題都在stackoverflow中被詢問,但沒有回答。 Attempt to present UIAlertController on UIViewController whose view is not in the window hierarchy警告 - 試圖提出UIAlertController空斯威夫特

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函數。 –