我使用didSelectRowAtIndexPath
和prepareForSegue
从表格视图中以模态方式呈现视图控制器作为自定义弹出窗口。但是,下一个视图控制器上的数据返回nil。我已经将问题缩小到通过协议本身传递数据。下面我将包括视图控制器和日志的相关代码。
First View Controller:
protocol barTableViewControllerDelegate {
func acceptData(data: AnyObject!)
}
class barTableViewController: UITableViewController, CLLocationManagerDelegate {
var delegate: barTableViewControllerDelegate?
var data: AnyObject?
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("yes")
let classIndexPath = self.tableView.indexPathForSelectedRow!
print(classIndexPath.row)
print("yes2")
let selectedClass = names[classIndexPath.row]
print("yes3")
print(names[classIndexPath.row])
self.delegate?.acceptData(selectedClass)
self.performSegueWithIdentifier("showAd", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "showAd") {
/* if let objIndexPath = self.tableView.indexPathForSelectedRow?.row {
let obj = queryArray[objIndexPath]
let navVC = segue.destinationViewController as! UINavigationController
let detailVC = navVC.topViewController as! DetailViewController
detailVC.post = obj */
print("is segue working")
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("adPopUp") as! PopUpViewController
self.addChildViewController(popOverVC)
popOverVC.view.frame = self.view.frame
self.view.addSubview(popOverVC.view)
popOverVC.didMoveToParentViewController(self)
print("segue working")
}
}
第二个视图控制器:
class PopUpViewController: UIViewController {
func acceptData(data: AnyObject!) {
self.post = data! as! String
}
override func viewDidLoad() {
super.viewDidLoad()
print("here")
print(post)
print(self.post)
print("here2")
}
}
打印的最后一个日志是here.
停靠在print(post)
。
我不认为我必须在第二个视图控制器中声明数据吗?
答案 0 :(得分:0)
如果您要前进,则不需要该协议。您只需在prepareForSegue
class barTableViewController: UITableViewController, CLLocationManagerDelegate {
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let classIndexPath = self.tableView.indexPathForSelectedRow!
let selectedClass = names[classIndexPath.row]
self.performSegueWithIdentifier("showAd", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "showAd") {
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("adPopUp") as! PopUpViewController
popOverVC.data = selectedClass // <-- Set the variable in the next VC
self.addChildViewController(popOverVC)
popOverVC.view.frame = self.view.frame
self.view.addSubview(popOverVC.view)
popOverVC.didMoveToParentViewController(self)
}
}
}
class PopUpViewController: UIViewController {
var data: String!
override func viewDidLoad() {
super.viewDidLoad()
if let passedData = data {
self.post = passedData
}
}
}
答案 1 :(得分:0)
你使用segue的方式是错误的,你不应该初始化popOverVC
中的prepareForSegue
,也不应该在{setFrame
,addSubview
中查看过渡(prepareForSegue
,UIStoryboardSegue
) {1}}也是。 destinationViewController
有一个名为popOverVC
的属性,在您的情况下是barTableViewController
。如果您想在源(popOverVC
)和目标(UIStoryboardSegue
)之间进行自定义转换,则需要子类化perform
,覆盖其SELECT
方法,并执行所有操作那里。