具有不同方向的多个视图控制器

时间:2016-08-14 19:01:31

标签: ios swift

说明

我有一个横向运行的游戏,我也试图以纵向模式发送电子邮件。

基本上

  • 如果实际视图为GameViewController = .Landscape;
  • 如果实际视图为MailViewController = .AllButUpsideDown

演示代码

我在下面构建了这段代码,但我遇到了问题:

  • 触摸屏幕时未调用邮件。


您可以下载此代码here

GameScene

import SpriteKit

class GameScene: SKScene {

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        /* Called when a touch begins */

        //Mail
        NSNotificationCenter.defaultCenter().postNotificationName("openMail", object: nil)

    }
}

GameViewController

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)
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
            return .Landscape
        } else {
            return .Landscape
        }
    }
}

MailViewController

import UIKit
import SpriteKit
import MessageUI

class MailViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //Register mail observer (so I can call from GameScene)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MailViewController.openMailController), name: "openMail", object: nil)
    }

    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?) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
}


谢谢你的帮助,
路易斯。

1 个答案:

答案 0 :(得分:2)

您提供的代码问题是MailViewController类视图中的观察者没有调用load方法。因此Observer未设置为接收任何通知。

你也没有在你的代码中的任何地方调用segue来调用MailViewController。

解决方案:使用委托方法

GameScene课程已更新:

    import SpriteKit

    // Protocol 
    protocol  gameSceneDelegate  {
        func openMail()
    }

    // Protocol 


    class GameScene: SKScene {

        // Delegate
        var sceneDelegate : gameSceneDelegate?

        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
            /* Called when a touch begins */

            //Mail
    //        NSNotificationCenter.defaultCenter().postNotificationName("openMail", object: nil)

            // delegate method called on receiving touches
            self.sceneDelegate?.openMail()

        }
    }

GameViewController类已更新:

    import UIKit
    import SpriteKit

    class GameViewController: UIViewController {

        override func viewDidLoad() {
            super.viewDidLoad()

            // Set view size.
            let scene = GameScene(size: view.bounds.size)

            // Delegate Confirmation

            scene.sceneDelegate = self

            // 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)
        }

        override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
            if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
                return .Landscape
            } else {
                return .Landscape
            }
        }
    }

    // Delegate Method Implementation

    extension GameViewController : gameSceneDelegate{
        func openMail(){
            self.performSegueWithIdentifier("openMail", sender: self);
        }
    }

邮件视图控制器:

    import UIKit
    import SpriteKit
    import MessageUI

    class MailViewController: UIViewController {

        override func viewDidLoad() {
            super.viewDidLoad()

            //Register mail observer (so I can call from GameScene)
    //        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MailViewController.openMailController), name: "openMail", object: nil)

            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?) {
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    }

故事板中的最后一次更改:

点击segue连接GameViewController和MailViewController并将标识符更改为 - openMail或任何你想保留的内容