解决了这个问题:我忘记在VC1中声明tvx.selectionDelegate = self,这就是为什么它从未执行的原因
我搜索了很长时间,并在网上为我的问题提供了两个类似的答案:here
但是,所有这些方法都是通过解雇,然后提出另一个观点来完成的,我想驳回并推动另一个ViewC。
我有3个ViewController:
VC1让我们称之为: DownloadsViewController
VC2让我们称之为: SelectActionController
VC3让我们称之为: fileInfoView
VC1经常提出VC2,然后VC2应该解散,VC1应该立即推动VC3。
我试过:
VC2:
self.present(vx, animated: true, completion: nil) 并将推送动画完成{},但它会崩溃。
接下来,我尝试做一个委托,如果我按下VC2上的一个按钮,它会调用VC1来拒绝VC2并按VC3。就像这里:
VC2:
protocol PushViewControllerDelegate: class {
func pushViewController(_ controller: UIViewController)
}
class SelectActionController: UIViewController, UITableViewDelegate, UITableViewDataSource {
weak var delegate: PushViewControllerDelegate?
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
if self.delegate != nil {
self.delegate?.pushViewController(self)}VC1:
func pushViewController(_ controller: UIViewController) {
controller.dismiss(animated: true) { () -> Void in
self.performSegue(withIdentifier: self.segue2, sender: nil)
//let vx = self.storyboard?.instantiateViewController(withIdentifier: "FileInfo") as! fileInfoView
//self.navigationController?.pushViewController(vx, animated: false)
}
}我尝试的第三件事是创建一个全局变量,如果它不存在,只需将Bool变量设置为true,VC1中的一个函数就会识别它(我的意图)。唯一的问题是,它不会将其重新定位为ViewDidAppear或类似的东西。
所以这些都没有用。
有人对此有什么想法吗?
发布于 2017-09-29 16:30:30
根据你的档案,我想你忘了申报
tvx.selectionDelegate = self在
func imagePressed发布于 2017-09-29 14:29:16
在OP注释后编辑了
用协议就行了。让您的第一个控制器采用它,并添加所需的功能。在第二个控制器中设置一个协议变量,该变量以第一个控制器为参考,并在退出第二个控制器时调用该函数。你现在可以使用解散函数来做你想做的任何事情,传递数据,发送给另一个控制器.
第一视图控制器:
protocol CustomProtocol {
func dismissed()
}
class AViewController: UIViewController, CustomProtocol {
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func toVC2(_ sender: UIButton) {
let VC = self.storyboard!.instantiateViewController(withIdentifier: "BViewController") as! BViewController
VC.customProtocol = self
VC.view.backgroundColor = .white
self.present(VC, animated: true, completion: nil)
}
func dismissed() {
let yourViewController = UIViewController()
yourViewController.view.backgroundColor = .red
guard let navigationController = self.navigationController else {return}
navigationController.pushViewController(yourViewController, animated: true)
}
}第二视图控制器:
class BViewController: UIViewController {
var customProtocol: CustomProtocol?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func quit(_ sender: UIButton) {
self.dismiss(animated: true) {
guard let proto = self.customProtocol else {return}
proto.dismissed()
}
}
}发布于 2017-09-29 14:36:11
我希望你需要对你的
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath:在"SelectActionController“里,对吗?
在这种情况下,你必须这样做,
公共函数tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
if self.delegate != nil {
dismiss(animated: true) {
self.delegate?.pushViewController(self)}
}
}我已经分享了一个与你的情况有关的例子项目,请参考。https://github.com/Bharath-MallowTech/StackoverflowSolutions/tree/master/viewcontrollerDismissAndPush
https://stackoverflow.com/questions/46490739
复制相似问题