I have set you my UI for both screens and I don't want to do the connect the screens in main.storyboard. I want to click a button and it will take to that screen, I will do the fade animations and stuff like that later. I just need to know how to go to one screen to the next screen when I press a button. I can give an example of what I am talking about down below.
Example: In flappy bird before you start the game it tell you to tap on the screen, then when you tap the text fades out. That is what I want to do with my game. I am not sure if that is another screen or if that is 1 view that makes certain labels disappear once the screen is tapped.
My MainMenuViewController:
import UIKit
import SpriteKit
class MainMenuViewController: UIViewController {
var mainMenuScene: MainMenuScene!
#IBOutlet weak var playButton: UIButton!
#IBAction func playButtonPressed(sender: AnyObject) {
mainMenuScene.playButtonPressed(sender)
}
#IBAction func playerButtonPressed(sender: AnyObject) {
println("player")
}
#IBAction func LeaderboardButtonPressed(sender: AnyObject) {
println("leaderboard")
}
#IBAction func shopButtonPressed(sender: AnyObject) {
println("shop")
}
override func viewDidLoad() {
super.viewDidLoad()
//set up game screens
// Configure the view
// store a ref
// Create and configure the scene
mainMenuScene?.scaleMode = .AspectFill
// Present the scene
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> Int {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
} else {
return Int(UIInterfaceOrientationMask.All.rawValue)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
My MainMenuScene:
import Foundation
import UIKit
import SpriteKit
class MainMenuScene: SKScene {
var movingGround: PPMovingGround!
var square1: PPSquare1!
var wallGen: PPWallGen!
var isStarted = false
var playButton: UIButton!
func playButtonPressed(sender: AnyObject) {
start()
}
func start() {
isStarted = true
square1.stop()
movingGround.start()
wallGen.startGenWallsEvery(1)
}
override func didMoveToView(view: SKView) {
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
}
override func update(currentTime: CFTimeInterval) {
}
}
I want to connect this code to my GameScene File. How do I do it?
I think I know what you're aiming for, since you don't want to do work with the Main.storyboard directly, you could do this:
presentViewController(YourNextViewController, animated: true, completion: nil)
you would call that in the ViewController you are currently in to get to the next one.
Let's say you are on the main screen, as you said in Flappy Bird, when you tap the button, it could change to a different View Controller
My tip would be to nest this method in a function, so you can call it in a gameStart function, like so:
func changeViewControllers() {
presentViewController(GameViewController, animated: true, completion: nil)
}
then call this in your gameStart():
func gameStart() {
changeViewControllers()
//other code
}
//=====================================================================
Alright, I'm going to help you get to another view when you tap your play button, add the following code:
#IBAction func playButtonPressed(sender: AnyObject) {
mainMenuScene.playButtonPressed(sender)
}
Related
I want to trigger Navigation controller to some other screen when i press the button in UIView class. How can i do this?
//Code for UIView Class in Which Button Iboutlet is created
import UIKit
protocol ButtonDelegate: class {
func buttonTapped()
}
class SlidesVC: UIView {
var delegate: ButtonDelegate?
#IBAction func onClickFinish(_ sender: UIButton) {
delegate?.buttonTapped()
}
#IBOutlet weak var imgProfile: UIImageView!
}
//ViewController Class code in Which Button Protocol will be entertained
class SwipingMenuVC: BaseVC, UIScrollViewDelegate {
var slidesVC = SlidesVC()
override func viewDidLoad() {
super.viewDidLoad()
slidesVC = SlidesVC()
// add as subview, setup constraints etc
slidesVC.delegate = self
}
extension BaseVC: ButtonDelegate {
func buttonTapped() {
self.navigationController?.pushViewController(SettingsVC.settingsVC(),
animated: true)
}
}
A more easy way is to use typealias. You have to write code in 2 places. 1. your viewClass and 2. in your View Controller.
in your SlidesView class add a typealias and define param type if you need otherwise leave it empty.
class SlidesView: UIView {
typealias OnTapInviteContact = () -> Void
var onTapinviteContact: OnTapInviteContact?
#IBAction func buttonWasTapped(_ sender: UIButton) {
if self.onTapinviteContact != nil {
self.onTapinviteContact()
}
}
}
class SwipingMenuVC: BaseVC, UIScrollViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let slidesView = SlidesView()
slidesView.onTapinviteContact = { () in
// do whatever you want to do on button tap
}
}
You can use the delegate pattern to tell the containing ViewController that the button was pressed and let it handle whatever is needed to do next, The view doesn't really need to know what happens.
A basic example:
protocol ButtonDelegate: class {
func buttonTapped()
}
class SomeView: UIView {
var delegate: ButtonDelegate?
#IBAction func buttonWasTapped(_ sender: UIButton) {
delegate?.buttonTapped()
}
}
class ViewController: UIViewController {
var someView: SomeView
override func viewDidLoad() {
someView = SomeView()
// add as subview, setup constraints etc
someView.delegate = self
}
}
extension ViewController: ButtonDelegate {
func buttonTapped() {
self.showSomeOtherViewController()
// or
let vc = NewViewController()
present(vc, animated: true)
}
}
I have a button in a subview called UIViewControllerB. Someone who taught me how to capture click event in UIViewControllerB will add a new view to UIView in UIViewcontroller A under string as my code.
This is the button in ViewControllerB displayed in UIView in UIViewController A
#IBAction func button_complate(sender: AnyObject) {
NSLog("aaa", "bbb")
}
Here is the UIViewControllerA containing UIView
#IBOutlet weak var addview: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
try code:
class UIViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
func addView(/*param */) {
// do something
}
// when push to UIViewControllerB set install for viewcontrollerA
private func goToScreenB(){
let viewB = UIViewControllerB()
viewB.viewcontrollerA = self
self.navigationController?.pushViewController(viewB, animated: true)
}
}
class UIViewControllerB: UIViewController {
// In UIViewControllerB create a install of UIViewControllerA
var viewcontrollerA : UIViewControllerA?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func button_complate(sender: AnyObject) {
self.viewcontrollerA?.addView()
}
}
My IOS app need to handle a connected bluetooth keyboard to do some function in the app, I tried this method, but the xcode cannot found the Cocoa
Then I tried this method, this time xcode cannot find the canBecomeFirstResponder in super class.
How can I capture keyboard event in my app?
import UIKit
class ViewController: UIViewController {
var keys = [UIKeyCommand]()
override func viewDidLoad() {
super.viewDidLoad()
for digit in "1234567890abcdefghijklmnopqrstuvwxyz"
{
keys.append(UIKeyCommand(input: String(digit), modifierFlags: [], action: Selector(("keyPressed:"))))
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func canBecomeFirstResponder() -> Bool {
return true
}
override var keyCommands: [AnyObject]? {
get {
return keys
}
}
func keyPressed(command: UIKeyCommand) {
print("user pressed \(command.input)")
}
}
Ok so I am trying to make it when the user flips the cheat mode switch it will disable the answerButton. I did try to set the button to disable near the bottom however it does not appear to do anything. I added the print("") to see if it would print in the console however nothing was printed. I a unsure what is wrong here.
I have pasted my code below.
import Foundation
import UIKit
class SettingsController: UITableViewController {
#IBOutlet weak var cheatSwitch: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
cheatSwitch.isOn = UserDefaults.standard.bool(forKey: "switchState")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func saveSettings(_ sender: UISwitch) {
UserDefaults.standard.set(cheatSwitch.isOn, forKey: "switchState")
UserDefaults.standard.synchronize()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let mainView : ViewController = segue.destination as! ViewController
if cheatSwitch.isOn == true {
print("turn off button")
mainView.btnAnswer.isEnabled = false;
}
else {
print("turn on button")
mainView.btnAnswer.isEnabled = true;
}
}
}
You are saving the value UISwitch's state in UserDefaults so you can use its value in ViewController's method viewDidApper.
override func viewDidAppear(_ animated: Bool) {
self.btnAnswer.isEnabled = !UserDefaults.standard.bool(forKey: "switchState")
}
Note: There is no need to add prepareForSegue in your SettingsController because it will never called when you press back button of NavigationController so consider to remove it.
I dragged and dropped progressindicator from main.xib on to a webview .
I need to show NSProgressIndicator on top of webview , instead of showing on the same webview.
The main idea is to stop the enduser to access webview until and unless NSProgressIndicator.stopAnimation(self) is done.
Can someone help me on this .
//In order to show a modelpanel on top of appdelegate.swift we need to //follow two steps.
//step1: create a cocoa class from NSWindowController as below.
import Cocoa
class ManuModalController: NSWindowController
{
#IBOutlet weak var progressIndicator1: NSProgressIndicator!
var mainW: NSWindow = NSWindow()
override init()
{
super.init()
}
override init(window: NSWindow!)
{
super.init(window: window)
}
required init?(coder: (NSCoder!))
{
super.init(coder: coder);
}
override func windowDidLoad()
{
super.windowDidLoad()
}
func beginSheet(mainWindow: NSWindow)
{
self.mainW = mainWindow;
NSApp.beginSheet(self.window!, modalForWindow: self.mainW, modalDelegate: self, didEndSelector:nil, contextInfo: nil);
progressIndicator1.startAnimation(self);
}
#IBAction func btnClicked(sender: AnyObject)
{
self.endSheet();
}
func endSheet()
{
NSApp.endSheet(self.window!);
self.window!.orderOut(mainW);
}
}
//step2 : create the object of modalcontroller in appdelegate.swift as below
var mdlwin = ManuModalController(windowNibName: "ManuModalController");
mdlwin.beginSheet(self.window);//displays the modalpanel
mdlwin.endSheet(); //closes the modal panel