カスタムセルの上に置いたボタンを長押しでアラートを表示するものを書いたのですが、
TableViewController.swift
swift
1import UIKit 2 3class TableViewController: UITableViewController { 4 5 func longPressView(sender: UILongPressGestureRecognizer) 6 { 7 print("長押し") 8 let actionSheet = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: UIAlertControllerStyle.actionSheet) 9 10 let action1 = UIAlertAction(title: "アクション1", style: UIAlertActionStyle.default, handler: { 11 (action: UIAlertAction!) in 12 print("アクション1をタップした時の処理") 13 }) 14 15 let cancel = UIAlertAction(title: "キャンセル", style: UIAlertActionStyle.cancel, handler: { 16 (action: UIAlertAction!) in 17 print("キャンセルをタップした時の処理") 18 }) 19 20 actionSheet.addAction(action1) 21 actionSheet.addAction(cancel) 22 23 self.present(actionSheet, animated: true, completion: nil) 24 25 } 26 27 28 private let myItems: NSArray = ["TEST1", "TEST2", "TEST3"] 29 override func viewDidLoad() { 30 super.viewDidLoad() 31 32 33 34 35 36 } 37 38 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 39 print("Num: \(indexPath.row)") 40 print("Value: \(myItems[indexPath.row])") 41 } 42 43 /* 44 Cellの総数を返す. 45 */ 46 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 47 return myItems.count 48 } 49 50 /* 51 Cellに値を設定する 52 */ 53 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 54 // 再利用するCellを取得する. 55 let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell") as! TableViewCell 56 57 // Cellに値を設定する. 58 cell.longbutton() 59 60 return cell 61 } 62} 63
TableViewCell.swift
swift
1import UIKit 2 3class TableViewCell: UITableViewCell { 4 @IBOutlet weak var testbutton: UIButton! 5 6 var foo = TableViewController() 7 func longbutton() { 8 let longPressGesture = UILongPressGestureRecognizer(target: foo, action: #selector(TableViewController.longPressView(sender:))) 9 longPressGesture.minimumPressDuration = 0.1 10 longPressGesture.allowableMovement = 30 11 testbutton.addGestureRecognizer(longPressGesture) 12 13 14 } 15 override func awakeFromNib() { 16 super.awakeFromNib() 17 // Initialization code 18 } 19 20 override func setSelected(_ selected: Bool, animated: Bool) { 21 super.setSelected(selected, animated: animated) 22 // Configure the view for the selected state 23 } 24 25} 26
長押しするとエラーが出て、アラートが表示されません。
長押し
2017-08-24 15:15:57.719 tab[11050:155509] Warning: Attempt to present <UIAlertController: 0x7fe8c751b5e0> on <tab.TableViewController: 0x7fe8c7413e50> whose view is not in the window hierarchy!
対処法を教えていただけないでしょうか?
エラーではなく警告なのですが、AlertControllerは画面に表示されているのでしょうか?
回答1件
あなたの回答
tips
プレビュー