2017-01-19 62 views
0

我在UITableView中使用UIAlertController。它在tableview.delegate = self附近返回nil值。這裏alertC是提供警報的功能。我在按下按鈕時從tableViewCell調用alertC函數。UITableView中的UIAlertController返回無

class AdminPlaces: UIViewController, UITableViewDelegate, UITableViewDataSource { 

@IBOutlet weak var tableView: UITableView! 
private var placeNames = [String]() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.getDataFromFirebase { 
     self.tableView.delegate = self 
     self.tableView.dataSource = self 
     self.tableView.reloadData() 
    } 

    func alertC() { 
    let ac = UIAlertController(title: "Delete", message: "Are you sure to delete the place", preferredStyle: .alert) 
    let yesAction = UIAlertAction(title: "Yes", style: .default, handler: nil) 

    let noAction = UIAlertAction(title: "No", style: .cancel, handler: nil) 
    ac.addAction(yesAction) 
    ac.addAction(noAction) 
    self.present(ac, animated: true, completion: nil) 
} 

} 


class AdminPlacesCell: UITableViewCell { 

@IBOutlet weak var placeName: UILabel! 

@IBOutlet weak var delete: UIButton! 
@IBOutlet weak var edit: UIButton! 

override func awakeFromNib() { 
    edit.alpha = 0 
    delete.alpha = 0 
} 
func configure(place: String) { 
    placeName.text = place 
} 

func showButtons() { 
    UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseIn, animations: { 
     self.edit.alpha = 1 
     self.delete.alpha = 1 
    }, completion: nil) 

} 



@IBAction func onEditClicked(_ sender: Any) { 
} 
@IBAction func onDeleteClicked(_ sender: Any) { 
    let adminPlaces = AdminPlaces() 
    adminPlaces.alertC() 
} 
} 

我不明白是什麼問題。爲什麼它返回零附近tableView.delegate = self?什麼是待辦事項的正確方法?

在此先感謝

+0

請檢查此鏈接,因爲它解決了我的問題。 [鏈接](http://stackoverflow.com/a/34911172/6223683) –

回答

0

這裏有幾個問題。 1. alertC存在於ViewDidLoad內部。將此功能移出此處。 2.以下代碼:

let adminPlaces = AdminPlaces() 
adminPlaces.alertC() 

說創建一個全新的AdminPlaces實例並在新的實例上調用alertC()。這與持有tableView的AdminPlaces不是同一個單元格。新的還沒有出現,因此不會顯示警報。 3.你的警報存在於ViewController上,你的單元沒有直接的訪問方式。

使用UITableViewDelegate方法tableView:commit:editingStyle。當用戶從編輯模式中刪除單元格時,將調用此方法。從這裏調用你的alertC()方法。

+0

謝謝你的回覆。但是這只是我沒有在viewDidLoad中寫入alertC()函數。這只是我的複製粘貼中的一個錯誤。任何感謝你。 –

+0

我從這個鏈接中得到了答案。任何有同樣問題的人都可以參考這個。 [鏈接](http://stackoverflow.com/a/34911172/6223683) –

相關問題