1

スレーブクラスのテーブルセルテキストラベルデータを目的のクラスに渡したいと思います。迅速なクラスで、私は次のようでしたが、通知を使用してスウィフトから目的のcクラスへの文字列データの受け渡し方法

class NewsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 
@IBOutlet weak var newsTableView: UITableView! 

var myVC:NewsDetailsViewController! 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    tableView.deselectRow(at: indexPath, animated: true) 
    print("selected index :",indexPath.row) 

    let selectedCell = tableView.cellForRow(at: indexPath) 
    print("did select and the text is \(selectedCell?.textLabel?.text)") 
    myVC.passedValue = selectedCell?.textLabel?.text 
    print("text label value: ", myVC.passedValue) 
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "PostQuestion"), object: myVC.passedValue) 
    self.present(myVC, animated: true , completion: nil) 

} 

今、私はObjective Cのクラスで私の他のコントローラと、この文字列データを受信したいです。親切にガイドします。

+0

func processNotification(notification: NSNotification) { let userInfo = notifcation.userInfo if let value = userInfo?["value"] as? String { ... } } 

または同じようなもので、通知にそれを処理する必要があり、文字列を渡しますそうでなければ 'NSUserDefaults'を使うことができます –

+0

Am not usi NSUserDefaultsは私のプロジェクトでは制限されています。だから私は通知センターに欲しい。 – user579911

+0

データを渡すには 'userInfo'辞書を使い、' object'のパラメータとして 'self'(送信者)を使うべきです。 'myVC.passedValue'は正しい値ですか、' myVC.passedValue'を使わないのですか? – clemens

答えて

4

あなたは

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "PostQuestion"), 
    object: self, 
    userInfo: ["value": myValue] as Dictionary<AnyHashable, Any>) 

であなたの通知を送信し、可能な場合のObjective-C

- (void)processNotification:(NSNotification *)notification { 
    NSString *value = [notifcation.userInfo objectForKey:@"value"] 

    if(value) { 
     ... 
    } 
} 
+0

私の客観的なクラスでそれを受け取る方法? – user579911

+0

投稿を更新しました。 – clemens

+0

このプロセスを持つ必要はありません。 – user579911

関連する問題