In my swift app, I have a view controller with several buttons. When the user interacts with these buttons, they trigger code that may have some dependencies. It can look something like this:
class ViewController: UIViewController {
#IBOutlet weak var button1: UIButton!
#IBAction func button1Down(sender: UIButton) {
// Do some stuff
SomeUtil().doSomething()
}
}
This is a major problem for testing. I'd like to test the interface for the view controller, IE the button1Down. This button1Down is coupled to a dependency that I would like to mock in a unit test. This implementation leaves no possibility of mocking.
Now, I've found two ways to do dependency injection, both with default parameters. I can (1) set the view controller up like this:
class ViewController: UIViewController {
#IBOutlet weak var button1: UIButton!
#IBAction func button1Down(sender: UIButton) {
// Do some stuff
somemethod()
}
func somemethod(util: SomeUtil = SomeUtil()) {
util.doSomething()
}
}
This works, but I'm not really testing the view controller's interface. I'm testing some implementation specific method to get around the fact that I can't add a default parameter to an overridden function.
I can (2) set the dependency injection up like this:
class ViewController: UIViewController {
#IBOutlet weak var button1: UIButton!
var util: SomeUtil!
#IBAction func button1Down(sender: UIButton) {
// Do some stuff
util.doSomething()
}
func injectDeps(util: SomeUtil = SomeUtil()) {
self.util = util
}
override func viewDidLoad() {
super.viewDidLoad()
injectDeps()
}
}
This method is great for utilities that I use in multiple methods, as it is reasonable to save the utility as an instance variable. This is not ideal for utilities that only get used in one method. These utility instances have no reason to be associated as a variable on the instance, other than to make testing easier.
As far as mocking goes, I usually just do something like this (using quick framework):
var viewController: MyApp.ViewController!
class SomeUtilMock: SomeUtil {
override func doSomething() {
// do something test specific
}
}
var someUtil: SomeUtilMock!
beforeEach {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
viewController =
storyboard.instantiateViewControllerWithIdentifier(
"MainView") as! MyApp.ViewController
someUtil = SomeUtilMock()
viewController.beginAppearanceTransition(true, animated: false)
viewController.endAppearanceTransition()
viewController.injectDeps(someUtil)
}
Has anyone found a better way to do dependency injection and mocking in swift? Neither of these dependency injection methods are ideal.
Thanks in advance!
Related
I created an outlet in ViewController class and I'd like to modify it.
In the ViewController.swift file I have
import Cocoa
class ViewController: NSViewController {
#IBOutlet var LabelText: NSTextFieldCell?
override func viewDidLoad() {
super.viewDidLoad()
}
//other things
}
I'd like to change the background color of the label. How can I do that from AppDelegate?
At first I thought I could solve this problem using a function in ViewController and calling it in AppDelegate
func changeBackground() {
LabelText.textColor = NSColor.red
}
But soon I realised that it wasn't possible unless I used a static function. Then I tried to modify the code in ViewController like that
static func changeBackground() {
LabelText.textColor = NSColor.red
}
and call this function in AppDelegate like that
ViewController.changeBackground()
In this way I can access to changeBackground() function from AppDelegate, but in ViewController it gives me an error: Instance member 'LabelText' cannot be used on type 'ViewController'
I understood that this cannot be possible because somehow I'm calling "LabelText" before it's initialised (or something like that).
I don't know much about Swift and I'm trying to understand how it works. I've been searching for the answer to my question for hours, but still I don't know how to solve this.
Solution
As Rob suggested, the solution is to use NotificationCenter.
A useful link to understand how it works: https://www.appypie.com/notification-center-how-to-swift
Anyway, here how I modified the code.
In ViewController:
class ViewController: NSViewController {
#IBOutlet var label: NSTextFieldCell!
let didReceiveData = Notification.Name("didReceiveData")
override func viewDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(onDidReceiveData(_:)), name: didReceiveData, object: nil)
super.viewDidLoad()
}
#objc func onDidReceiveData(_ notification: Notification) {
label.textColor = NSColor.red
}
}
And then, in AppDelegate:
let didReceiveData = Notification.Name("didReceiveData")
NotificationCenter.default.post(name: didReceiveData, object: nil)
I am making an iMessage extension that uses the didTransition(to:). However, the function won't run when I resize the iMessage extension in the simulator. Am I doing something wrong?
This is the code I have:
import UIKit
import Messages
class EditorViewController: MSMessagesAppViewController {
#IBOutlet weak var input: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
input.text = "not changed"
}
// This part isn't working:
override func didTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
input.text = "changed"
}
}
When I resize it in the simulator, nothing happens. The input.text changes the UITextView's text in the viewDidLoad() function, but not in the didTransition(to) function because it never runs.
Am I doing something wrong?
The EditorViewController is a view controller presented by the show (e.g. Push) segue, and has a NavigationController attached to it.
Here is a gif of it not changing:
The input's text never changes
How can I fix this?
EDIT: The willTransition and didTransition functions don't run when the View Controller is embedded in a Navigation Controller. Is there a way to fix this? (It works without the Navigation Controller, but I need the Navigation Controller for this project).
As pointed out in this answer, the entry point of a iMessage App need to be a subclass of MSMessagesAppViewController, so you can not use a NavigationViewController directly as root controller, until Apple adds support for this behavior.
But as suggested, you could solve this with a workaround like this:
import UIKit
import Messages
class MyRootVC: MSMessagesAppViewController {
var navVC: UINavigationViewController!
var editorVC: EditorViewController!
func viewDidLoad() {
super.viewDidLoad()
editorVC = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as! EditorViewController
navVC = UINavigationController(rootViewController: editorVC)
self.addChild(navVC)
self.view.addSubview(navVC.view)
navVC.didMove(toParent: self)
}
override func didTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
editorVC.input.text = "changed"
}
}
class EditorViewController: UIViewController {
#IBOutlet weak var input: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
input.text = "not changed"
}
}
I made a customized button type(class) that inherits from NSButton and has some additional methods as well, but when I try to access the methods that I declared myself, I get a run-time error. Here's my code:
import Cocoa
class MCButton: NSButton {
func testFunc()->Bool {
return true
}
}
class ViewController: NSViewController {
#IBOutlet weak var button: MCButton!
override func viewDidLoad() {
super.viewDidLoad()
if button.testFunc() { //Thread 1: EXC_BAD_ACCESS(code=2, address=0x608000264600)
button.title = "Hi!"
}
}
}
Note that I don't have any problems when I only use the methods declared in the superclass(NSButton). What's the problem? What should I do to fix it?
You have to correctly set the button's class in the InterfaceBuilder. It probably has the predefined value set, which is NSButton. You have to set it to MCButton instead.
Only then you actually get a reference to a correct instance of one of those MCButtons.
I'm learning how to program and am playing with a Swift project in Xcode. The main storyboard has two view controllers. The first view controller is simply called ViewController and the second view controller is called HelpScreenViewController.
In ViewController I have a "help" button that switches the user to HelpScreenViewController. This button uses a segue called "goToHelpScreenSegue".
In HelpScreenViewController I have three buttons:
"Close" button to dismisses the view (no segue used)
"Send Feedback" button to generate a new email in the Mail app (no segue used)
"Reset Game" button to call a function that is coded within the first ViewController. This third button uses a segue called "resetGameSegue".
What I'm trying to do is...
...Get the "Reset Game" button on the HelpScreenViewController to reset the game by calling a function that's coded within the first view controller.*
To try and get this to work the way I want, I've used the following code:
WITHIN first main ViewController
import UIKit
import iAd
import AdSupport
import AVFoundation //audio
import GameplayKit
class ViewController: UIViewController, ADBannerViewDelegate, MyResetGameProtocol {
#IBOutlet weak var Banner: ADBannerView!
#IBOutlet weak var buttonA: UIButton!
#IBOutlet weak var buttonB: UIButton!
#IBOutlet weak var buttonC: UIButton!
#IBOutlet weak var buttonD: UIButton!
#IBOutlet weak var labelQuestion: UILabel!
#IBOutlet weak var labelScore: UILabel!
#IBOutlet weak var labelTotalQuestionsAsked: UILabel!
#IBOutlet weak var labelFeedback: UILabel!
#IBOutlet weak var buttonNext: UIButton!
var score :Int! = 0
var totalquestionsasked :Int! = 0
var allEntries : NSArray!
var shuffledQuestions: [AnyObject]!
var nextQuestion = -1
var currentCorrectAnswerIndex : Int = 0
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.Banner?.delegate = self
self.Banner?.hidden = true
LoadAllQuestionsAndAnswers()
if #available(iOS 9.0, *) {
shuffledQuestions = GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(allEntries as [AnyObject])
nextQuestion++
LoadQuestion(nextQuestion)
// Fallback on earlier versions
}else{
let randomNumber = Int(arc4random_uniform(UInt32(allEntries.count)))
LoadQuestionPreiOS9(randomNumber)
}
LoadScore()
AdjustInterface()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let viewController = segue.destinationViewController as! HelpScreenViewController
viewController.controller = self
}
func ResetGame() {
PlaySoundReset()
score = 0
totalquestionsasked = 0
SaveScore()
LoadScore()
}
func PlaySoundReset()
{
let alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("pcbeep", ofType: "wav")!)
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: alertSound)
} catch {
}
audioPlayer.prepareToPlay()
audioPlayer.play()
}
func SaveScore()
{
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setInteger(score, forKey: "Score")
defaults.setInteger(totalquestionsasked, forKey: "Out of")
}
func LoadScore()
{
let defaults = NSUserDefaults.standardUserDefaults()
score = defaults.integerForKey("Score")
totalquestionsasked = defaults.integerForKey("Out of")
labelScore.text = "Score: \(score)"
labelTotalQuestionsAsked.text = "out of \(totalquestionsasked)"
}
and so on....
WITHIN the second HelpScreenViewController
import UIKit
protocol MyResetGameProtocol {
func ResetGame()
}
class HelpScreenViewController: UIViewController, MyResetGameProtocol {
var controller: MyResetGameProtocol? // reference to the delegate alias First Controller
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
#IBAction func SendFeedback(sender: AnyObject) {
UIApplication.sharedApplication().openURL(NSURL(string: "mailto:feedback#felice.ws?")!)
}
#IBAction func DismissView(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil) }
#IBAction func buttonResetGame(sender: AnyObject) {
controller.ResetGame()
}
}
Now, at the moment with the above code what happens is that if the user taps the "help" button in the first main ViewController (i.e. goToHelpScreenSegue), not only does it take the user to the help screen, but it also calls the function I want activated when the user taps on the "Reset Game" button instead. That is, at the moment, it's the "help" button that resets the game before taking the user to the help screen.
Now, within the help screen, the first two buttons work normally (but they're not using segues). Tapping on the third button (the Reset Game one) simply returns the user back to the main screen. It doesn't call the function, doesn't reset the game.
I've lost count of the times I've changed the code around to try and get it to work right, but I've obviously missed something really obvious.
In particular, I've tried using the following code instead within the main ViewController:
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
if segue?.identifier == "resetGameSegue" {
let viewController = segue!.destinationViewController as! HelpScreenViewController
viewController.controller = self
}
However, this results in nothing happening. What I mean is that the button on the main screen works properly (taking the user to the help screen and not incorrectly calling the resetGame function). And, within the help screen the first two buttons work as expected, but the "Reset Game" button just returns the user to the first screen but without calling the ResetGame function.
I also tried removing the IBActions from both my code and the connections inspector for the "Reset Game" button, but that made no difference either.
Any assistance would be most appreciated as I'm just not getting it! :(
I'm agree with MikeG, that you should probably learn about how delegates should be implemented. But the thing you're doing wrong inside this code is that you're not actually calling ResetGame() function on your delegate. Try to implement your #IBAction function in this way:
#IBAction func buttonResetGame(sender: AnyObject) {
controller?.ResetGame()
}
And yeah, if I understand your logic correctly your HelpScreenViewController should not implement MyResetGameProtocol cause your ViewController is the one who's implementing it.
Sorry in advance that I can’t explain myself very well. I’m really new to programming and the topic of delegation still eludes me. I had some great help with this once before, but now I am trying to use a delegate in a different situation and I can’t get it right. I pieced together a bit of code that doesn’t work, and no matter how much I search I can’t find a way to fix it.
I have a view controller (MainController) with and embedded view controller (EmbeddedController) in a container view. I am trying to have a button in the embedded controller manipulate the container view (containerView).
EmbeddedController:
protocol ControllerDelegate {
func hideContainerView()
}
class EmbeddedController: UIViewController {
var delegate: VControllerDelegate?
#IBAction func button(sender: AnyObject) {
delegate?.hideContainerView()
}
}
MainController:
class MainController: UIViewController, ControllerDelegate {
#IBOutlet var containerView: UIView!
func hideContainerView() {
containerView.hidden = true
}
override func viewDidLoad() {
super.viewDidLoad()
var vc = EmbeddedController()
vc.delegate = self
}
}
Does anyone have any idea what I am doing wrong? And why this isn’t working?
What I ended up doing is adding this to the MainController:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "mySegue") {
let vc = segue.destinationViewController as! EmbeddedController
vc.delegate = self
}
}
In storyboard I selected the segue from the MainController to the EmbeddedController, and set the identifier to "mySegue".
Without the code above the delegate kept returning nil. I didn't look into this solution at first as I thought segues were only for transitioning between view controllers, and in my mind I didn't see the embedded controller as a transition. Maybe someone more knowledgable than me (which is practically anyone on here at this point) can explain how this is all fitting together.
In any case, this is how I solved my issue and hopefully someone else can benefit from this as well :)
First of all, to avoid strong reference cycles:
protocol ControllerDelegate: class {
func hideContainerView()
}
class EmbeddedController: UIViewController {
weak var delegate: ControllerDelegate?
And you haven't added your newly instantiated VC view to container view, and not added it as a child VC:
let vc = EmbeddedController()
vc.delegate = self
containerView.addSubview(vc.view)
self.addChildViewController(vc)
vc.didMoveToParentViewController(self)