如何将字符串数据从swift传递到使用通知的客户端c类
我想将表格单元格文本标签数据从swift类传递到客户端c类。在迅速类我做了以下内容,如何将字符串数据从swift传递到使用通知的客户端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)
}
现在我想用我的另一个控制器,它是一个客观的C类接收该字符串数据。请指导。
你应该
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "PostQuestion"),
object: self,
userInfo: ["value": myValue] as Dictionary<AnyHashable, Any>)
发送您的通知,并与像
func processNotification(notification: NSNotification) {
let userInfo = notifcation.userInfo
if let value = userInfo?["value"] as? String {
...
}
}
或在同一进程通知它的Objective-C
- (void)processNotification:(NSNotification *)notification {
NSString *value = [notifcation.userInfo objectForKey:@"value"]
if(value) {
...
}
}
如何在我的目标c课中接收它? – user579911
我已更新我的帖子。 – clemens
发送数据时无需具有此processNotification方法名称? – user579911
如果可能的话再通过串数据与'prepareForSegue'方法,否则你可以使用'NSUserDefaults' –
我不usi任何segue和NSUserDefaults在我的项目中都受到限制。所以我想在通知中心。 – user579911
您应该使用'userInfo'字典来传递数据,并使用'self'(sender)作为'object'的参数。 myVC.passedValue是正确的值,还是不使用'myVC.passedValue'? – clemens