解释
我试图在单独的swift文件中构建一个简单的电子邮件代码(在本例中:GameViewController.swift
和MailViewController.swift
)。
应该怎么办:我必须触摸GameViewController
中的屏幕才能呼叫MailViewController
,当取消或发送电子邮件时,它将返回GameViewController
。
正在发生的事情:它调用MailViewController
并在触摸Cancel
或Send
时返回,但只有一次。当我再次尝试时(即使再次关闭和打开应用程序),当尝试返回GameViewController
时,也会出现一个黑色屏幕。
代码
您可以单击here下载下面的代码。
游戏视图控制器
import UIKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Set view size.
let scene = GameScene(size: view.bounds.size)
// Configure the view.
let skView = view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .ResizeFill
skView.presentScene(scene)
//Register mail observer (so I can call from GameScene)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.openMailViewController), name: "openMailViewController", object: nil)
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return .Landscape
} else {
return .Landscape
}
}
}
extension GameViewController{
func openMailViewController(){
self.performSegueWithIdentifier("openMailViewController", sender: self)
}
}
邮件视图控制器
import UIKit
import SpriteKit
import MessageUI
class MailViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.openMailController()
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return .AllButUpsideDown
} else {
return .All
}
}
}
//Mail
extension MailViewController: MFMailComposeViewControllerDelegate {
func openMailController() {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self //extremely important to set the mailComposeDelegate property, not the delegate property
self.presentViewController(mailComposerVC, animated: true, completion: nil)
}
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
//dismiss mail
self.dismissViewControllerAnimated(true, completion: nil)
//go back to the game
backToGameViewController()
}
}
extension MailViewController{
func backToGameViewController(){
self.performSegueWithIdentifier("backToGameViewController", sender: self)
}
}
主故事板
提前谢谢你,
路易斯。
最佳答案
如果你替换self.performSegueWithIdentifier("backToGameViewController", sender: self)
具有self.dismissViewControllerAnimated(true, completion: nil)
你的代码可以工作,但我不认为这是你真正想做的。
这是因为您的游戏控制器显示的是MailViewController,然后显示的是mfmailcomposiewcontroller。因为你一次展示两个,所以你得到了一个奇怪的双动画,一个空白视图在邮件视图之前向上滑动。为了完全关闭邮件视图,您需要调用dismissViewController两次。
一个更好的方法是直接从GameController显示MFMailComposeController。
关于ios - Storyboard仅加载一次,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39420784/