簡體   English   中英

具有不同方向的多個視圖控制器

[英]Multiple view controllers with different orientations

說明

我有一個橫向運行的游戲,我也嘗試以縱向模式發送電子郵件。

基本上:

  • 如果實際視圖是GameViewController = .Landscape ;
  • 如果實際視圖是MailViewController = .AllButUpsideDown

演示代碼

我在下面構建了此代碼,但是遇到了問題:

  • 觸摸屏幕時未調用郵件。


您可以在此處下載此代碼。

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


謝謝你的幫助,
路易斯。

您提供的代碼的問題是MailViewController類視圖中的觀察者確實未調用load方法。 因此,觀察者未設置為接收任何通知。

另外,您也沒有在代碼的任何地方調用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或您想要保留的任何內容

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM