Swift - 在ViewControllers之间切换并不起作用

时间:2016-07-31 21:03:31

标签: ios swift uiviewcontroller

我有两个ViewControllers。从ViewController推送到。{ 我的EffectsMenuViewController没有问题。我正在使用的代码是动作如下:

 @IBAction func openMenu(sender: AnyObject) {
    let mainStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
    let effectsViewController : EffectsMenuViewContoller = mainStoryboard.instantiateViewControllerWithIdentifier("effectsMenu") as! EffectsMenuViewContoller
    presentViewController(effectsViewController, animated: true, completion: nil)
}

我的presented EffectsMenuViewController包含一个UITableView,其中包含一个名为TableViewCell的自定义CategoryRow。这个类的代码如下:

class CategoryRow:UITableViewCell {
    @IBOutlet weak var collectionView:UICollectionView!
    weak var delegate:CategoryRowDelegate?
}

extension CategoryRow:UICollectionViewDataSource {  
 /*snipped code */   
func collectionView(collectionView: UICollectionView,didSelectItemAtIndexPath indexPath: NSIndexPath) {
    ImageObjectSingleton.imageName = DataSource.maskArray.objectAtIndex(indexPath.row) as! String
    ImageObjectSingleton.isEyeColorImage = false
    ImageObjectSingleton.isMaskImage = true
    EffectsMenuViewContoller.callstaticMethod()
} }

在我的EffectsMenuController我有一个名为calledstaticMethod的方法,由collectionView(collectionView: UICollectionView,didSelectItemAtIndexPath indexPath: NSIndexPath)调用:

 static func callstaticMethod() {
    let c = EffectsMenuViewContoller()
    c.didselectItemAtIndexPath()
}

这种方法不仅仅是调用我的didselectItemAtIndexPath方法,它应该解雇当前的ViewController:

func didselectItemAtIndexPath() {
    let mainStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
    let viewController : ViewController = mainStoryboard.instantiateViewControllerWithIdentifier("view") as! ViewController
    self.navigationController?.popViewControllerAnimated(true)
    self.navigationController?.pushViewController(viewController, animated: true)
    self.dismissViewControllerAnimated(true, completion: nil)
}

正如您所看到的,我尝试了不同类型的代码语句。但它们实际上都不起作用。但是,当我在self.dismissViewControllerAnimated(true, completion: nil) - 方法中使用IBAction时,它的工作方式就像我想要的那样,但我的方式与上面的代码完全一样......

有什么想法吗? 最好的祝福, Nazar Medeiros

1 个答案:

答案 0 :(得分:0)

在呈现视图控制器时,需要从呈现的视图控制器中调用dismissViewController方法。

此外,您确定要一个视图控制器的静态实例,其范围在一个方法中吗?