2016-04-28 22 views
0

UITableビューから行を削除する前にアラートを表示しようとしています。テーブルビューから削除する前に確認アラートを削除するデータソース

しかし、私はどのようにデータソースに私のビューコントローラのインターフェイスを渡さずにこれを行うことができます。

class BaseTableDataSource: NSObject, UITableViewDataSource { 

     func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
      if editingStyle == .Delete { 

      //1. Show alert and delete the block 

      ??? But how can you show the alert with out View controller reference?? 
      //viewcontroller.presentViewController(alertController, animated: true, completion: nil) 
      } 
     } 
    } 

答えて

-1

これを実行する方法はいくつかあります。

1)Ugly one:BaseTableDataSourceからNSNotificationを送信し、ViewController側でキャッチします。 NSNotificationのオブジェクトとして、 "AlertActionViewModel"またはUIAlertActionControllerを構成するものを渡すことができます。

2)デリゲートまたはクロージャを使用してコールバックを作成できます。しかし、最後にはBaseTableDataSourceのアイデアがすべて失われるため、データソースだけではありません。

希望すると、これが役立ちます。

+0

1.醜いものを使用したくない場合は、データソースをまったく使用しないでください。 2.データソースを作成した理由のすべてが失われています。 – user431791

+0

-1が必要でないときは、 'NSNotification'に対して-1を返します。このリンクをチェックしてください:https://www.andrewcbancroft.com/2015/07/16/uitableview-swipe-to-delete-workflow-in-swift/ – kakubei

0

通常、私はあなたがこの方法を試すことができ、私の警告を処理するためにsharedInstance通知マネージャを作成するprefeerが、オンザフライ:

let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert) 
alert.delegate = self 
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil)) 
let window = UIWindow(frame: UIScreen.mainScreen().bounds) 
window.rootViewController?.presentViewController(alert, animated: true,completion: nil) 

はUPDATE:あなたも行うことができます。コードから基づい

if let tableViewController = tableView.delegate { 
     tableViewController.presentViewController(alert, animated: true,completion: nil) 
} 
+0

理由を投稿する理由は、やっているエレガントな方法を見つけることですそれ。他の誰かがより良い答えを出すかどうかわかります。 – user431791

+0

私のコードを更新しました。この解決策を試してみてください。 –

関連する問題