NSButton object in NSViewController not work - swift

I am learning to write a MacOS Program without xib, I have written JavaScript before.
ViewControllerTest.swift
class ViewControllerTest: NSViewController {
lazy var button2 = NSButton(frame: NSMakeRect(455, 100, 50, 20))
override func loadView() {
...
button2.title = "2"
self.view.addSubview(button2)
button2.target = self
button2.action = #selector(self.button2Action)
}
#objc public func button2Action () {
NSLog("Button 2")
}
}
window.swift
class Window: NSWindow {
#objc public func button1Action () {
NSLog("button 1")
}
init() {
...
// Button 1
let button1 = NSButton(frame: NSMakeRect(455, 400, 50, 20))
button1.title = "1"
self.contentView!.addSubview(button1)
button1.target = self
button1.action = #selector(self.button1Action)
// add view
let viewController = ViewControllerTest()
self.contentView!.addSubview(viewController.view)
}
}
class WindowController: NSWindowController, NSWindowDelegate {
override func windowDidLoad() {
super.windowDidLoad()
self.window!.delegate = self
}
}
I click button 1, console output "Button 1", but button 2 does not work. Any ideas why?
I am have been searching for a long time on net. But no use. Any ideas about how to achieve this?

I was in the same boat but I think I figured it out. Put your
let viewController = ViewControllerTest()
outside of the init() and it should work now

Related

NSButton's #objc action not being triggered in Swift for MacOS app

I have an NSButton in my ViewController class that is not triggering its click function (closeButtonPressed) when clicked.
I would test with a Button but UIKit isn't available for my project, as it's a non-Catalyst app for Mac.
As this has been a challenging and time-consuming issue, I am grateful for your time and expertise in helping me resolve it.
Minimal reproducible example
import SwiftUI
#main
struct MinimalReproducibleExampleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
import Cocoa
import SwiftUI
struct ViewControllerWrapper: NSViewRepresentable {
func makeNSView(context: Context) -> NSView {
return ViewController().view
}
func updateNSView(_ nsView: NSView, context: Context) {
// You can add any logic you need here to update the view as needed.
}
}
class ViewController: NSViewController {
let closeButton = NSButton(title: "X", target: nil, action: nil)
override func loadView() {
self.view = NSView(frame: NSRect(x: 0, y: 0, width: 300, height: 300))
self.view.wantsLayer = true
self.view.layer?.backgroundColor = NSColor.black.cgColor
}
override func viewDidLoad() {
super.viewDidLoad()
view.wantsLayer = true
view.layer?.backgroundColor = NSColor.cyan.cgColor
closeButton.target = self
closeButton.action = #selector(closeButtonPressed)
closeButton.frame = CGRect(x: 100, y: 100, width: 50, height: 30)
closeButton.bezelColor = NSColor.gray
view.addSubview(closeButton)
}
#objc func closeButtonPressed() {
print("Close button pressed")
}
}
struct ContentView: View {
private var viewControllerWrapper: ViewControllerWrapper
init(){
self.viewControllerWrapper = ViewControllerWrapper()
}
var body: some View {
VStack {
viewControllerWrapper
}
}
}
(MacOS 12.6.3, Xcode 14.2)
As you are using a NSViewController you should use NSViewControllerRepresentable instead of using NSViewRepresentable.
NSViewControllerRepresentable is explicitly for use with NSViewController while NSViewRepresentable is for use with NView.
If you can change your ViewControllerWrapper to the following:
struct ViewControllerWrapper: NSViewControllerRepresentable {
func makeNSViewController(context: Context) -> ViewController {
ViewController()
}
func updateNSViewController(_ nsViewController: ViewController, context: Context) {
}
}
This should allow the button to work.

Swift: Create Unit Test for Private Functions in ViewController

I'm trying to test my viewcontroller, but I'm not sure how to test the private functions in there. It says is private. Obviously I could make it public but this seems to defeat the purpose...
import Quick
import Nimble
#testable import Recur
class ProfileDetailVCSpec: QuickSpec {
class TestProfileDetailVC: ProfileDetailVC {
var isProfileUpdated = false
override func updateProfile() {
isProfileUpdated = true
}
func pressDone() {
doneButtonPressed() //COMPILER WON'T ALLOW, BECAUSE IT'S PRIVATE
}
}
override func spec() {
var testProfileDetailVC: TestProfileDetailVC!
beforeEach {
testProfileDetailVC = TestProfileDetailVC()
}
describe("edit profile") {
context("user makes changes to name") {
it("should call updateProfile") {
testProfileDetailVC.nameTextFieldValidInputEntered(ProfileEditNameView(), "TestFirst", "TestLast")
testProfileDetailVC.pressDone()
expect(testProfileDetailVC?.isProfileUpdated).to(equal(true))
}
}
context("user makes changes to photo") {
it("should call updateProfile") {
testProfileDetailVC.nameTextFieldValidInputEntered(ProfileEditNameView(), "TestFirst", "TestLast")
testProfileDetailVC.pressDone()
expect(testProfileDetailVC?.isProfileUpdated).to(equal(true))
}
}
context("user doesn't make any changes") {
it("should not call updateProfile") {
testProfileDetailVC.pressDone()
expect(testProfileDetailVC?.isProfileUpdated).to(equal(false))
}
}
}
}
}
Here is the viewcontroller. Some of the logic my coworker is still working on, but it's mostly there. I just can't seem to be able to call the private functions in swift, so I can't run these tests
class ProfileDetailVC: UIViewController {
private let doneButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Done", for: .normal)
button.titleLabel?.font = UIFont(name: "SFCompactRounded-Semibold", size: 16)
button.tintColor = .recurBlue
button.addTarget(self, action: #selector(doneButtonPressed), for: .touchUpInside)
return button
}()
let profileNameEditView = ProfileEditNameView()
let errorLabel: UILabel = {
let label = UILabel()
label.textColor = .red
label.font = .regularSubtitle
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
loadProfileImage()
setupUI()
profileNameEditView.delegate = self
}
func updateProfile() {
}
private func loadProfileImage() {
if let profile = Profile.currentProfile {
profileImage.configure(with: profile, imageSize: CGSize(width: 120, height: 120))
}
}
#objc private func doneButtonPressed() {
updateProfile()
}
extension ProfileDetailVC: ProfileEditNameViewDelegate {
func nameTextFieldNonValidInputEntered(_: ProfileEditNameView) {
errorLabel.text = "First and last name required"
}
func nameTextFieldValidInputEntered(_: ProfileEditNameView, _ firstNameText: String, _ lastNameText: String) {
errorLabel.text = ""
}
}
There is no way to access a private func for testing. The #testable attribute you are using will allow you to use an internal func in a test however. So you can drop the private keyword and the func will default to internal as the class is internal.

"Extensions must not contain stored properties" preventing me from refactoring code

I have a 13 lines func that is repeated in my app in every ViewController, which sums to a total of 690 lines of code across the entire project!
/// Adds Menu Button
func addMenuButton() {
let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
let menuImage = UIImage(named: "MenuWhite")
menuButton.setImage(menuImage, for: .normal)
menuButton.addTarget(self, action: #selector(menuTappedAction), for: .touchDown)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: menuButton)
}
/// Launches the MenuViewController
#objc func menuTappedAction() {
coordinator?.openMenu()
}
for menuTappedAction function to work, I have to declare a weak var like this:
extension UIViewController {
weak var coordinator: MainCoordinator?
But by doing this I get error Extensions must not contain stored properties
What I tried so far:
1) Removing the weak keyword will cause conflicts in all my app.
2) Declaring this way:
weak var coordinator: MainCoordinator?
extension UIViewController {
Will silence the error but the coordinator will not perform any action. Any suggestion how to solve this problem?
You can move your addMenuButton() function to a protocol with a protocol extension. For example:
#objc protocol Coordinated: class {
var coordinator: MainCoordinator? { get set }
#objc func menuTappedAction()
}
extension Coordinated where Self: UIViewController {
func addMenuButton() {
let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
let menuImage = UIImage(named: "MenuWhite")
menuButton.setImage(menuImage, for: .normal)
menuButton.addTarget(self, action: #selector(menuTappedAction), for: .touchDown)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: menuButton)
}
}
Unfortunately, you can't add #objc methods to class extensions (see: this stackoverflow question), so you'll still have to setup your view controllers like this:
class SomeViewController: UIViewController, Coordinated {
weak var coordinator: MainCoordinator?
/// Launches the MenuViewController
#objc func menuTappedAction() {
coordinator?.openMenu()
}
}
It'll save you some code, and it will allow you to refactor the bigger function addMenuButton(). Hope this helps!
For it to work in an extension you have to make it computed property like so : -
extension ViewController {
// Make it computed property
weak var coordinator: MainCoordinator? {
return MainCoordinator()
}
}
You could use objc associated objects.
extension UIViewController {
private struct Keys {
static var coordinator = "coordinator_key"
}
private class Weak<V: AnyObject> {
weak var value: V?
init?(_ value: V?) {
guard value != nil else { return nil }
self.value = value
}
}
var coordinator: Coordinator? {
get { (objc_getAssociatedObject(self, &Keys.coordinator) as? Weak<Coordinator>)?.value }
set { objc_setAssociatedObject(self, &Keys.coordinator, Weak(newValue), .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
}
}
This happens because an extension is not a class, so it can't contain stored properties. Even if they are weak properties.
With that in mind, you have two main options:
The swift way: Protocol + Protocol Extension
The nasty objc way: associated objects
Option 1: use protocol and a protocol extension:
1.1. Declare your protocol
protocol CoordinatorProtocol: class {
var coordinator: MainCoordinator? { get set }
func menuTappedAction()
}
1.2. Create a protocol extension so you can pre-implement the addMenuButton() method
extension CoordinatorProtocol where Self: UIViewController {
func menuTappedAction() {
// Do your stuff here
}
}
1.3. Declare the weak var coordinator: MainCoordinator? in the classes that will be adopting this protocol. Unfortunately, you can't skip this
class SomeViewController: UIViewController, CoordinatorProtocol {
weak var coordinator: MainCoordinator?
}
Option 2: use objc associated objects (NOT RECOMMENDED)
extension UIViewController {
private struct Keys {
static var coordinator = "coordinator_key"
}
public var coordinator: Coordinator? {
get { objc_getAssociatedObject(self, &Keys.coordinator) as? Coordinator }
set { objc_setAssociatedObject(self, &Keys.coordinator, newValue, .OBJC_ASSOCIATION_ASSIGN) }
}
}
You can do it through subclassing
class CustomVC:UIViewController {
weak var coordinator: MainCoordinator?
func addMenuButton() {
let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
let menuImage = UIImage(named: "MenuWhite")
menuButton.setImage(menuImage, for: .normal)
menuButton.addTarget(self, action: #selector(menuTappedAction), for: .touchDown)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: menuButton)
}
/// Launches the MenuViewController
#objc func menuTappedAction() {
coordinator?.openMenu()
}
}
class MainCoordinator {
func openMenu() {
}
}
class ViewController: CustomVC {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
Use a NSMapTable to create a state container for your extension, but make sure that you specify to use weak references for keys.
Create a class in which you want to store the state. Let's call it ExtensionState and then create a map as a private field in extension file.
private var extensionStateMap: NSMapTable<TypeBeingExtended, ExtensionState> = NSMapTable.weakToStrongObjects()
Then your extension can be something like this.
extension TypeBeingExtended {
private func getExtensionState() -> ExtensionState {
var state = extensionStateMap.object(forKey: self)
if state == nil {
state = ExtensionState()
extensionStateMap.setObject(state, forKey: self)
}
return state
}
func toggleFlag() {
var state = getExtensionState()
state.flag = !state.flag
}
}
This works in iOS and macOS development, but not on server side Swift as there is no NSMapTable there.

Creating a selector with variable of function type

I am working on two views that are subclassing subclass of UITableViewCell. In the base one (subclass of UITableViewCell) I am trying to setup gesture recognizer in a way that each of super class could change the behavior (eventually call didTapped method on it's delegate) of the tap.
I have written following code. I can use #selector(tap), however I think that using a variable instead of overriding a tap method in each super class is a much cleaner way. Is it even possible to use something like #selector(tapFunc)? If no what would be the cleanest and best from engineering point of view solution?
class BaseCell: UITableViewCell {
#objc var tapFunc: () -> () = { () in
print("Tapped")
}
#objc func tap() {
print("TEST")
}
func setupBasicViews(withContent: () -> ()) {
let tapGestureRecoginzer = UITapGestureRecognizer(target: self, action: #selector(tapFunc))
contentView.isUserInteractionEnabled = true
contentView.addGestureRecognizer(tapGestureRecoginzer)
}
}
And then two views that are building on top of this one:
class ViewA: BaseCell {
//don't want to do this
override func tap() {
//do stuff
}
func setup {
//setup everything else
}
class ViewB: BaseCell {
var delegate: ViewBProtocool?
func setup {
tapFunc = { () in
delegate?.didTapped(self)
}
//setup everything else
}
You're not too far off. Make the following changes:
class BaseCell: UITableViewCell {
var tapFunc: (() -> Void)? = nil
// Called by tap gesture
#objc func tap() {
tapFunc?()
}
func setupBasicViews(withContent: () -> ()) {
let tapGestureRecoginzer = UITapGestureRecognizer(target: self, action: #selector(tap))
contentView.isUserInteractionEnabled = true
contentView.addGestureRecognizer(tapGestureRecoginzer)
}
}
class ViewA: BaseCell {
func setup() {
//setup everything else
}
}
class ViewB: BaseCell {
var delegate: ViewBProtocol?
func setup() {
tapFunc = {
delegate?.didTapped(self)
}
//setup everything else
}
}
Now each subclass can optionally provide a closure for the tapFunc property.
I show above that tapFunc is optional with no default functionality in the base class. Feel free to change that to provide some default functionality if desired.

Swift function textfield got focus OSX

Currently I am having multiple textfields in a view. If the user taps at one of them there should be a function responding to the event. Is there a way on how to do react (if a textfield got the focus)? I tried it with the NSTextFieldDelegate method but there is no appropriate function for this event.
This is how my code looks at the moment:
class ViewController: NSViewController, NSTextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let textField = NSTextField(frame: CGRectMake(10, 10, 37, 17))
textField.stringValue = "Label"
textField.bordered = false
textField.backgroundColor = NSColor.controlColor()
view.addSubview(textField)
textField.delegate = self
let textField2 = NSTextField(frame: CGRectMake(30, 30, 37, 17))
textField2.stringValue = "Label"
textField2.bordered = false
textField2.backgroundColor = NSColor.controlColor()
view.addSubview(textField2)
textField2.delegate = self
}
func control(control: NSControl, textShouldBeginEditing fieldEditor: NSText) -> Bool {
print("working") // this only works if the user enters a charakter
return true
}
}
The textShouldBeginEditing function only handles the event if the user tries to enter a character but this isn't what I want. It has to handle the event if he clicks on the textfield.
Any ideas, thanks a lot?
Edit
func myAction(sender: NSView)
{
print("aktuell: \(sender)")
currentObject = sender
}
This is the function I want to call.
1) Create a subclass of NSTextField.
import Cocoa
class MyTextField: NSTextField {
override func mouseDown(theEvent:NSEvent) {
let viewController:ViewController = ViewController()
viewController.textFieldClicked()
}
}
2) With Interface building, select the text field you want to have a focus on. Navigate to Custom Class on the right pane. Then set the class of the text field to the one you have just created.
3) The following is an example for ViewController.
import Cocoa
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
func textFieldClicked() -> Void {
print("You've clicked on me!")
}
}
4) Adding text fields programmatically...
import Cocoa
class ViewController: NSViewController {
let myField:MyTextField = MyTextField()
override func viewDidLoad() {
super.viewDidLoad()
//let myField:MyTextField = MyTextField()
myField.setFrameOrigin(NSMakePoint(20,70))
myField.setFrameSize(NSMakeSize(120,22))
let textField:NSTextField = NSTextField()
textField.setFrameOrigin(NSMakePoint(20,40))
textField.setFrameSize(NSMakeSize(120,22))
self.view.addSubview(myField)
self.view.addSubview(textField)
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
func textFieldClicked() -> Void {
print("You've clicked on me!")
}
}
I know it’s been answered some while ago but I did eventually find this solution for macOS in Swift 3 (it doesn’t work for Swift 4 unfortunately) which notifies when a textfield is clicked inside (and for each key stroke).
Add this delegate to your class:-
NSTextFieldDelegate
In viewDidLoad() add these:-
imputTextField.delegate = self
NotificationCenter.default.addObserver(self, selector: #selector(textDidChange(_:)), name: Notification.Name.NSTextViewDidChangeSelection, object: nil)
Then add this function:-
func textDidChange(_ notification: Notification) {
print("Its come here textDidChange")
guard (notification.object as? NSTextView) != nil else { return }
let numberOfCharatersInTextfield: Int = textFieldCell.accessibilityNumberOfCharacters()
print("numberOfCharatersInTextfield = \(numberOfCharatersInTextfield)")
}
Hope this helps others.