Subclassing NSControl, IBAction is not called in Swift - swift

I've subclassed an NSSlider that behaves differently whether the option key is pressed. In order to do that, I overrode the mouseDown func. It seems to do the job.
The thing is, I've noticed the connected #IBAction in my ViewController is only triggered when the option key is unpressed (i.e. when the mouseDown method is passed to super). What am I missing in order to allow the #IBAction to perform?
Many thanks
Besides the issue, improvement advices on the code are welcome... :-)
Josh
class ViewController: NSViewController {
#IBOutlet weak var theSlider: MySlider!
#IBAction func moveSlider(sender: NSSlider) {
print(sender.floatValue) //works only with optionKey unpressed
}
}
class MySlider: NSSlider { //Implemented in another source file
#IBInspectable var multiplier: Float = 0.5
private var modifierKeys = NSEventModifierFlags.AlternateKeyMask
private var optionKeyPressed = false
private var previousSliderPosition: Float = 0.0
//MARK: Init with NSCoder
required init?(coder: NSCoder) {
super.init(coder: coder)
Swift.print("init Coder called")
self.continuous = true
NSEvent.addLocalMonitorForEventsMatchingMask(.FlagsChangedMask) { (theEvent) -> NSEvent? in
self.flagsChanged(theEvent)
return theEvent
}
}
//MARK: Mouse tracking
override func mouseDown(theEvent: NSEvent) {
if optionKeyPressed {
var keepOn = true
previousSliderPosition = self.floatValue * Float(self.bounds.width) / Float(self.maxValue)
while keepOn {
if let nextEvent = self.window?.nextEventMatchingMask(Int(NSEventMask.LeftMouseUpMask.rawValue) | Int(NSEventMask.LeftMouseDraggedMask.rawValue))
{
switch nextEvent.type
{
case .LeftMouseDragged:
let mouseInnerLocationX = Float(self.convertPoint(nextEvent.locationInWindow, fromView: self.superview).x)
let mouseDelta = mouseInnerLocationX - previousSliderPosition
let newSliderPosition = previousSliderPosition + (mouseDelta) * multiplier
self.floatValue = newSliderPosition * Float(self.maxValue) / Float(self.bounds.width)
break
case .LeftMouseUp:
keepOn = false
break
default:
break
}
}
}
} else {
super.mouseDown(theEvent)
}
}
//MARK: Option key handling
override func flagsChanged(theEvent: NSEvent) {
if (theEvent.modifierFlags.rawValue & NSEventModifierFlags.DeviceIndependentModifierFlagsMask.rawValue) == NSEventModifierFlags.AlternateKeyMask.rawValue {
optionKeyPressed = true
} else {
optionKeyPressed = false
}
}
}

If you're not calling super.mouseDown, you need to send the action yourself:
sendAction(action, to: target)
sendAction(_:to:), action and target are existing members of NSControl.

Related

how to init() a swift view controller properly?

I'm attempting to initialize this ViewController class. I am not using the MVC design strategy so ignore the bad conventions used (if any).
How do I initialize this class properly?
Error: 'required' initializer 'init(coder:)' must be provided by subclass of 'UIViewController'
Context: This is a calculator app that when any of the buttons are pressed. It will go find the senders title and simply put if one of the three vars are nil, it will store it in that optional.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBOutlet weak var answerLabel: UILabel!
//global vars for all funcs
var selection1: Int? {
didSet { answerLabel.text = String(selection1!) }
}
var selection2: String? {
didSet { answerLabel.text = selection2! }
}
var selection3: Int? {
didSet { answerLabel.text = String(selection3!) }
}
var answer: Int {
didSet { answerLabel.text = String(answer) }
}
init() {
}
#IBAction func touchButton(_ sender: UIButton) {
if selection1 == nil {
selection1 = Int(sender.currentTitle!)
print("Selection set in first pos.")
} else if selection2 == nil {
selection2 = sender.currentTitle
} else if selection3 == nil {
selection3 = Int(sender.currentTitle!)
} else {
calculate(firstNum: selection1!, operation: selection2!, secondNum: selection3!)
}
}
func calculate(firstNum: Int, operation: String, secondNum: Int) {
switch operation {
case "+":
answer = firstNum + secondNum
case "-":
answer = firstNum - secondNum
case "x":
answer = firstNum * secondNum
case "/":
answer = firstNum / secondNum
default:
answerLabel.text = "Something went wrong!"
}
}
}
Initialization depends on a couple of condition.
If you are using storyboard, you can just remove the init and your VC will have default initializer. Make sure either all of your properties have default value or they are optional.
If you are using xib or just creating view programmatically you can have custom convenience initializer where you pass some extra data this way.
class MyViewController: ViewController {
var answer: Int
convenience init(answer: Int) {
self.init()
self.answer = answer
// Do other setup
}
}
Your controller is being instantiated from the storyboard. A safe place to configure initial views is during the controller's call to viewDidLoad, ie:
override func viewDidLoad() {
super.viewDidLoad()
// configure your views and subviews here
}

Change search field's icon

I try to implement search behavior like in Xcode: if you enter something in search field, icon changes color.
I delegate both searchFieldDidStartSearching and searchFieldDidEndSearching to controller and change the image.
The problem is icon's image changes only when window lose it's focus.
class ViewController: NSViewController {
#IBOutlet weak var searchField: NSSearchField!
func searchFieldDidStartSearching(_ sender: NSSearchField) {
print("\(#function)")
(searchField.cell as! NSSearchFieldCell).searchButtonCell?.image = NSImage.init(named: "NSActionTemplate")
}
func searchFieldDidEndSearching(_ sender: NSSearchField) {
print("\(#function)")
(searchField.cell as! NSSearchFieldCell).searchButtonCell?.image = NSImage.init(named: "NSHomeTemplate")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
Thanks in advance for any ideas/suggestions.
Although I don't know the reason, it works:
NSApp.mainWindow?.resignMain()
NSApp.mainWindow?.becomeMain()
Here is the whole code:
class MyViewController: NSViewController {
private lazy var searchField: NSSearchField = {
let searchField = NSSearchField(string: "")
if let searchButtonCell = searchField.searchButtonCell {
searchButtonCell.setButtonType(.toggle)
let filterImage = #imageLiteral(resourceName: "filter")
searchButtonCell.image = filterImage.tinted(with: .systemGray)
searchButtonCell.alternateImage = filterImage.tinted(with: .systemBlue)
}
searchField.focusRingType = .none
searchField.bezelStyle = .roundedBezel
searchField.delegate = self
return searchField
}()
...
}
extension MyViewController: NSSearchFieldDelegate {
func searchFieldDidStartSearching(_ sender: NSSearchField) {
sender.searchable = true
}
func searchFieldDidEndSearching(_ sender: NSSearchField) {
sender.searchable = false
}
}
extension NSSearchField {
var searchButtonCell: NSButtonCell? {
(self.cell as? NSSearchFieldCell)?.searchButtonCell
}
var searchable: Bool {
get {
self.searchButtonCell?.state == .on
}
set {
self.searchButtonCell?.state = newValue ? .on : .off
self.refreshSearchIcon()
}
}
private func refreshSearchIcon() {
NSApp.mainWindow?.resignMain()
NSApp.mainWindow?.becomeMain()
}
}
extension NSImage {
func tinted(with color: NSColor) -> NSImage? {
guard let image = self.copy() as? NSImage else { return nil }
image.lockFocus()
color.set()
NSRect(origin: NSZeroPoint, size: self.size).fill(using: .sourceAtop)
image.unlockFocus()
image.isTemplate = false
return image
}
}
I was having the same issue. A simple override fixed this issue for me
extension NSSearchField{
open override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
}
}
As you can see when you click inside the view it's still focussed on the search text field(as you can still type in it after you clicked underneath it). Since the change image is on when it loses focus, you should check if you clicked outside of the text field.
Solve problem by subclassing NSSearchFieldCell and assign this class to field's cell.
You don't even need to subclass NSSearchFieldCell.
When you create your NSSearchField from code, you can do something like this:
if let searchFieldCell = searchField.cell as? NSSearchFieldCell {
let image = NSImage(named: "YourImageName")
searchFieldCell.searchButtonCell?.image = image
searchFieldCell.searchButtonCell?.alternateImage = image // Optionally
}
If you're using storyboards, you can do the same in didSet of your #IBOutlet.

How to access properties outside of an enum

How can I access my class properties from within an enum contained in this class?
But as soon as I instantiate multiple MyClass, the _toggledOn is the same for all instances.
I hope there is some clean and swifty workaround I don't know. Anyways thank you in advance!
#IBDesignable
class MyClass: UIView, SomeProtocols {
// MARK: - ENUM
enum ToggleState {
case on
case off
var color: UIColor {
switch self {
case .on:
return _onColor
default:
return _offColor
}
}
}
// MARK: - STATICS
private static var _onColor: UIColor = #colorliteral
private static var _offColor: UIColor = #colorliteral
// MARK: - IBSTUFF
#IBInspectable var toggledOffColor: UIColor = #colorliteral {
didSet {
MyClass._offColor = toggledOffColor
updateUI()
}
}
#IBInspectable var toggledOnColor: UIColor = #colorliteral {
didSet {
MyClass._onColor = toggledOnColor
updateUI()
}
}
#IBOutlet weak var background: UIView!
#IBAction func buttonPressed(_ sender: UIButton) {
toggleState = toggleState == .off ? .on : .off
}
// MARK: - PROPERTIES
var toggleState: ToggleState = .off {
didSet { toggle() }
}
// MARK: - METHODS
func updateUI() {
background.backgroundColor = toggleState.color
background.layer.addShadow()
}
func toggle() {
background.backgroundColor = toggleState.color
}
}
Your _onColor and _offColor are static, so there will not be a separate _onColor and _offColor for each instance of your class.\
You should make _onColor and _offColor non-static, and remove the color property of ToggleState, and instead add such a method in your class:
func color(forToggleState state: ToggleState) {
switch state {
case .on:
return _onColor
case .off:
return _offColor
}
}
Instead of toggleState.color, you should write:
color(forToggleState: toggleState)
color shouldn't be a property of ToggleState because to compute it, you need information from another object.

Setting values of a class for example UIButton

I am lacking some basic understandings.
Today I wanted to subclass some UIView and I looked in to the UIButton definition, but I can not figure out how it works.
In the UIButton definition are properties like:
open var adjustsImageWhenHighlighted: Bool
open var adjustsImageWhenDisabled: Bool
When using an UIButton it does not matter when the values of the UIButton get set, it always gets configured the correct way same with tableView or any other UIKit classes.
I made a example:
class customView: UIView {
var shouldSetupConstraints = true
var addAdditionalSpacing = true
var elements: [String]? {
didSet{
if addAdditionalSpacing == false {
doSomething()
}
}
}
func doSomething() {
}
override init(frame: CGRect) {
super.init(frame: frame)
setUpLayout()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func updateConstraints() {
if(shouldSetupConstraints) {
// AutoLayout constraints
shouldSetupConstraints = false
}
super.updateConstraints()
}
func setUpLayout() {
}
}
Using CustomView:
lazy var customV: CustomView = {
let v = CustomView()
v.addAdditionalSpacing = true
v.elements = ["One","Two"]
return v
}()
lazy var customV2: CustomView = {
let v = CustomView()
v.elements = ["One","Two"]
v.addAdditionalSpacing = true
return v
}()
So if I am using CustomView it makes a difference in which order I set it up, I understand why but I do not understand in which way I have to design my classes so I can set the values whenever I want, except with different if didSet configurations. Why do the properties in UIButton do not have any setters, how do the values get set in that case?
Any links to documentations are appreciated as well.
First of all, in a UIButton you can take control of the didSet property observer for existing properties like this:
override var adjustsImageWhenDisabled: Bool {
didSet {
doStuff()
}
}
Secondly, in your class scenario you might consider passing the parameters in the constructor:
init(shouldSetupConstraints: Bool = true, var addAdditionalSpacing = true)
This way the properties will be available for you whenever you need them.
Let me know if this helps.

WKCrownSequencer not working after going back to main interface controller

I have a WKCrownSequencer that triggers an action in my pushed interface controller and the first time through everything works fine. When I go back to root interface controller regardless of the method (pop or reloadRootcontrollers) the digital crown no longer works in first interface controller nor the second one. The StartInterfaceController is the rootInterfaceController and the MidWorkoutInterfaceController is the pushed one.
import WatchKit
import Foundation
class StartInterfaceController:
WKInterfaceController,CLLocationManagerDelegate {
override func awake(withContext context: Any?) {
super.awake(withContext: context)
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
#IBAction func start() {
WKInterfaceController.reloadRootControllers(
withNames: ["midWorkout"], contexts: []
)
}
The second interface controller is below.
import WatchKit
import Foundation
class MidWorkoutInterfaceController: WKInterfaceController, WKCrownDelegate {
override func awake(withContext context: Any?) {
super.awake(withContext: context)
print("viewdidAwake")
print("ViewWillActivate")
crownSequencer.delegate = self
crownSequencer.focus()
WKInterfaceDevice.current().play(.success)
currentPhase = 0
let workoutType = UserDefaults.standard.object(forKey: "CurrentType") as? [String] ?? [ "Swimming", "T1"]
orderOfEventsSetter = workoutType
updateCurrentPhase()
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
crownSequencer.focus()
}
var clockTimer: Timer!
func workoutStarted(){
clockTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] timer in
self?.totalTimeOutlet.setText( String(describing: -1 * Int(self!.startDate!.timeIntervalSinceNow)))
self?.splitTimeOutlet.setText( String(describing: -1 * Int(self!.currentStartDate!.timeIntervalSinceNow)))
}
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
var startDate: Date?
var currentStartDate: Date?
//Outlets
#IBOutlet var currentPhaseOutlet: WKInterfaceLabel!
#IBOutlet var totalTimeOutlet: WKInterfaceLabel!
#IBOutlet var splitTimeOutlet: WKInterfaceLabel!
#IBOutlet var currentPaceOutlet: WKInterfaceLabel!
#IBOutlet var totalDistanceOutlet: WKInterfaceLabel!
var orderOfEventsSetter: Array<String>{
get{
return orderOfEvents
}
set{
var tempArray = ["GPS Locking In"]
for phase in newValue {
tempArray.append(phase)
}
orderOfEvents = tempArray
}
}
private var orderOfEvents: [String] = []
var currentPhase = 0 {
didSet{
if !orderOfEvents.isEmpty {
updateCurrentPhase()
}
}
}
func updateCurrentPhase(){
currentPhaseOutlet.setText(orderOfEvents[currentPhase])
}
//timing for location requests
//Corelocation Section
//CoreMotion Section
///crown control
var currentDialRotation = 0.0
let dialRotationRange = Range(uncheckedBounds: (lower: -Double.pi / 4, upper: Double.pi / 4))
let constantForTimer: TimeInterval = 0.1
var justTransitioned = false
func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double) {
currentDialRotation += rotationalDelta
if !dialRotationRange.contains(currentDialRotation){
currentDialRotation = 0.0
justTransitioned = true
makeTransition()
//make so two transitions cannot happen right after each other
}
print(currentDialRotation)
}
func crownDidBecomeIdle(_ crownSequencer: WKCrownSequencer?) {
print(String(describing: orderOfEvents[currentPhase]))
print("crown stopped")
}
func makeTransition(){
print(currentPhase)
print(orderOfEvents.count)
if (currentPhase) == orderOfEvents.count - 1 {
endWorkout()
}
else if (currentPhase == 0){
WKInterfaceDevice.current().play(.start)
let dateFormat = DateFormatter()
dateFormat.dateFormat = "mm/dd/yyyy"
startDate = Date()
currentStartDate = Date()
workoutStarted()
currentPhase += 1
}
else{
WKInterfaceDevice.current().play(.start)
print("transitioning to " + String(describing: orderOfEvents[currentPhase + 1]))
currentStartDate = Date()
stopTimers()
currentPhase += 1
}
}
#IBAction func endWorkoutButton() {
endWorkout()
}
func endWorkout(){
stopTimers()
clockTimer.invalidate()
alerts()
}
func alerts(){
let saveAction = WKAlertAction(title: "Save",
style: WKAlertActionStyle.default) {
self.goToStartScreen()
}
let discardAction = WKAlertAction(title: "Discard Workout",
style: WKAlertActionStyle.cancel) {
self.goToStartScreen()
}
presentAlert(withTitle: "Workout Complete",
message: "Would you like to save the workout?",
preferredStyle: WKAlertControllerStyle.alert,
actions: [saveAction, discardAction])
}
func goToStartScreen(){
crownSequencer.resignFocus()
self.popToRootController()
}
func stopTimers(){
if orderOfEvents[currentPhase] == "Running"{
}
if orderOfEvents[currentPhase] == "Biking"{
}
if currentPhase == orderOfEvents.count {
clockTimer.invalidate()
}
}
}
According to Apple's document here:
"...Only one object in your interface can have focus at any given time, so if your interface also contains picker objects or has scrollable scenes, you must coordinate changes in focus accordingly. For example, calling the sequencer's focus method causes any picker objects or interface controllers to resign focus. When the user taps on a picker object, the currently active sequencer resigns focus, and the selected picker object gains the focus... "
And you are to lose the focus at any time unpredictable...
"...If the user taps a picker object or a scrollable scene in your interface, the system automatically removes the focus from any active crown sequencer..."