通过编程方式解除ViewController我遇到了问题。 我连接了两个控制器,如果按下按钮,将显示控制器。 这是我的prepareForSegue方法:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let VC = VC()
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
let blurredView = UIVisualEffectView(effect: blurEffect)
blurredView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height + 64)
VC.view.frame = self.view.bounds
VC.view.backgroundColor = UIColor.clearColor()
VC.view.addSubview(blurredView)
VC.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
var masterViewOfVC = (segue.destinationViewController as! SubjectSelectionViewController).masterView
blurredView.addSubview(masterViewOfVC)
masterViewOfVC.setTranslatesAutoresizingMaskIntoConstraints(false)
let views = ["masterView": masterViewOfVC]
let horizontalConstraintsForMasterView = NSLayoutConstraint.constraintsWithVisualFormat("H:|[masterView]|", options: .AlignAllCenterY, metrics: nil, views: views)
blurredView.addConstraints(horizontalConstraintsForMasterView)
let verticalConstraintsForMasterView = NSLayoutConstraint.constraintsWithVisualFormat("V:|[masterView]|", options: .AlignAllCenterX, metrics: nil, views: views)
blurredView.addConstraints(verticalConstraintsForMasterView)
(segue.destinationViewController as! VC).viewDidLoad()
self.presentViewController(VC, animated: true, completion: nil)
}
所以,如果我试图解雇它,它什么都不做。我已经检查过按钮是否正常工作了。
以下是来自其他控制器的代码:
// viewDidLoad中
cancelButton.setTitle("Abbrechen", forState: UIControlState.Normal)
cancelButton.tintColor = UIColor.whiteColor()
cancelButton.addTarget(self, action: Selector("buttonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
cancelButton.enabled = true
//我的功能
func buttonAction(sender:UIButton!) {
VC().dismissViewControllerAnimated(true, completion: nil)
}
更新
我在Storyboard中删除了segue,并在我的Button的IBAction中创建了一个模态segue。我也在这个动作中初始化了我的“关闭按钮”。
以下是来自parentViewController的代码:
@IBAction func SubjectSelctionButtonPressed(sender: UIBarButtonItem) {
let subjectSelectionViewController = SubjectSelectionViewController()
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
let blurredView = UIVisualEffectView(effect: blurEffect)
blurredView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height + 64)
subjectSelectionViewController.view.frame = self.view.bounds
subjectSelectionViewController.view.backgroundColor = UIColor.clearColor()
subjectSelectionViewController.view.addSubview(blurredView)
subjectSelectionViewController.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
var masterViewOfSubjectSelection = SubjectSelectionViewController().masterView
var buttonOfSubjectSelection = SubjectSelectionViewController().cancelButton
buttonOfSubjectSelection.setTitle("Abbrechen", forState: UIControlState.Normal)
buttonOfSubjectSelection.tintColor = UIColor.whiteColor()
buttonOfSubjectSelection.addTarget(self, action: Selector("buttonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
blurredView.addSubview(masterViewOfSubjectSelection)
masterViewOfSubjectSelection.addSubview(buttonOfSubjectSelection)
masterViewOfSubjectSelection.setTranslatesAutoresizingMaskIntoConstraints(false)
let views = ["masterView": masterViewOfSubjectSelection]
let horizontalConstraintsForMasterView = NSLayoutConstraint.constraintsWithVisualFormat("H:|[masterView]|", options: .AlignAllCenterY, metrics: nil, views: views)
blurredView.addConstraints(horizontalConstraintsForMasterView)
let verticalConstraintsForMasterView = NSLayoutConstraint.constraintsWithVisualFormat("V:|[masterView]|", options: .AlignAllCenterX, metrics: nil, views: views)
blurredView.addConstraints(verticalConstraintsForMasterView)
self.presentViewController(subjectSelectionViewController, animated: true, completion: nil)
}
,按钮的操作如下所示:
func buttonAction(sender:UIButton!) {
dismissViewControllerAnimated(true, completion: nil)
}
现在好了,当控制器出现时,除了我在动作中创建的元素之外什么也没有。调用我的呈现控制器的viewDidLoad()方法,但不会调用约束和格式...
这是我的modalViewController的viewDidLoad():
titleLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
cancelButton.setTranslatesAutoresizingMaskIntoConstraints(false)
separatorLine.setTranslatesAutoresizingMaskIntoConstraints(false)
subjectsTableView.setTranslatesAutoresizingMaskIntoConstraints(false)
subjectsTableView.delegate = self
titleLabel.text = "Unterrichtsfächer"
titleLabel.textColor = UIColor.whiteColor()
titleLabel.font = UIFont(name: "HelveticaNeue-Medium", size: 17)
separatorLine.backgroundColor = UIColor.whiteColor()
subjectsTableView.backgroundColor = UIColor.clearColor()
masterView.addSubview(titleLabel)
masterView.addSubview(cancelButton)
masterView.addSubview(separatorLine)
masterView.addSubview(subjectsTableView)
let views = ["line": separatorLine, "title": titleLabel, "button": cancelButton, "table": subjectsTableView]
let verticalConstraintsForElements = NSLayoutConstraint.constraintsWithVisualFormat("V:|-64-[line(2)][table]|", options: .AlignAllCenterX, metrics: nil, views: views)
masterView.addConstraints(verticalConstraintsForElements)
let horizontalConstraintsForSeparatorLine = NSLayoutConstraint.constraintsWithVisualFormat("H:|[line]|", options: .AlignAllCenterY, metrics: nil, views: views)
masterView.addConstraints(horizontalConstraintsForSeparatorLine)
let horizontalConstraintsForTableView = NSLayoutConstraint.constraintsWithVisualFormat("H:|[table]|", options: .AlignAllCenterY, metrics: nil, views: views)
masterView.addConstraints(horizontalConstraintsForTableView)
let verticalConstraintsForTitleLabel = NSLayoutConstraint.constraintsWithVisualFormat("V:[title(21)]-11-[line]", options: .AlignAllCenterX, metrics: nil, views: views)
masterView.addConstraints(verticalConstraintsForTitleLabel)
let horizontalAlignmentForTitleLabel = NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: masterView, attribute: .CenterX, multiplier: 1, constant: -18)
masterView.addConstraint(horizontalAlignmentForTitleLabel)
let widthForTitleLabel = NSLayoutConstraint(item: titleLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 137)
masterView.addConstraint(widthForTitleLabel)
let verticalConstraintsForCancelButton = NSLayoutConstraint.constraintsWithVisualFormat("V:[button(30)]-6-[line]", options: .AlignAllCenterX, metrics: nil, views: views)
masterView.addConstraints(verticalConstraintsForCancelButton)
let horizontalConstraintsForCancelButton = NSLayoutConstraint.constraintsWithVisualFormat("H:[button(90)]-7-|", options: .AlignAllCenterY, metrics: nil, views: views)
masterView.addConstraints(horizontalConstraintsForCancelButton)
任何人都可以帮助我吗? 非常感谢!!!
答案 0 :(得分:0)
我还没有真正检查过您的所有代码,但要快速修复它,您可以将VC
的实例设为类属性。
所以它看起来像这样:
class MainViewController: UIViewController {
var VC = UIViewController() // I would use an optional here but I've been down voted for suggesting people to use optionals.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
VC = VC()
// Your code
}
func buttonAction(sender:UIButton!) {
VC.dismissViewControllerAnimated(true, completion: nil)
}
}
我假设您正在使用某种主 - 详情视图,因此当您转到buttonAction
时仍可访问VC
。
如果buttonAction
是VC
本身的一种方法,那么只需在我自己的dissmissViewControllerAnimated()
上调用:
func buttonAction(sender:UIButton!) {
dismissViewControllerAnimated(true, completion: nil)
}
无论哪种方式,您都必须在dissmissViewControllerAnimated()
的实例上调用VC
。使用VC()
,您只需创建一个新实例,其中没有"没有"与已经存在的那个有关。
<强>更新强>:
如果您在故事板中创建了SubjectSelectionViewController()
,那么您可能希望将其实例化为:
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let subjectSelectionViewController = storyboard.instantiateViewControllerWithIdentifier("YourIdentifier") as! SubjectSelectionViewController
答案 1 :(得分:0)
你的问题是你没有推动其他视图忽略它,你只是在另一个视图的顶部添加新的视图控制器 你需要替换这一行
blurredView.addSubview(masterViewOfVC)
对于
presentViewController(masterViewOfVC, animated: true, completion: nil)
现在,当您调用dismiss视图控制器时,所有应该正常工作