I have a problem with my buttons:
Buttons A and B look good with any connection (#IBOutlet):
Button B looks bad when is connected:
import Cocoa
class ButtonsC: NSViewController {
#IBOutlet var b: NSButton!
override func viewDidLoad() {
super.viewDidLoad()
}
}
and if I connect the button A, button B looks nice and A looks bad
import Cocoa
class ButtonsC: NSViewController {
#IBOutlet var a: NSButton!
#IBOutlet var b: NSButton!
override func viewDidLoad() {
super.viewDidLoad()
}
}
How can I fix it? or what kind of code in my project may be causing that?
Related
I am trying to call the entire menu with Month title from Mozharovsky/CVCalendar but I'm not sure what function will call this out?
This is the current code I am using:
import UIKit
import CVCalendar
class ViewController: UIViewController, CVCalendarViewDelegate, CVCalendarMenuViewDelegate {
#IBOutlet weak var calendarView: CVCalendarView!
#IBOutlet weak var menuView: CVCalendarMenuView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.navigationController?.navigationBar
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
calendarView.commitCalendarViewUpdate()
menuView.commitMenuViewUpdate()
}
func presentationMode() -> CalendarMode {
return CalendarMode.MonthView
}
func firstWeekday() -> Weekday {
return Weekday.Sunday
}
}
This is the what is rendered when compiled:
If you want to see the month title also you need to create a UIlabel and UIlabel outlet as such:
#IBOutlet weak var monthLabel: UILabel!
I got this answer by copying the code from Mozharovsky/CVCalendar Demo ViewController.swift file
This code compiles OK, but the ComboBox (cbxColors) is empty - not populated from the Data Source (array: COLORS_OF). Uses Data Source is checked in IB.
func numberOfItemsInComboBox() returns the correct result: 5.
func comboBox() is not doing its job.
What am I missing?
EDITED: Now working.
import Cocoa
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSComboBoxDelegate, NSComboBoxDataSource {
#IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(aNotification: NSNotification) {
cbxColors.dataSource = self
numberOfItemsInComboBoxCell(cbxColors)
comboBoxCell(cbxColors, objectValueForItemAtIndex: 0)
}
func applicationWillTerminate(aNotification: NSNotification) {
}
#IBOutlet weak var cbxColors: NSComboBox!
#IBOutlet weak var txtResult: NSTextField!
#IBAction func actColors(sender: NSComboBox) {
// display User selected item in 'txtResult'
}
func numberOfItemsInComboBoxCell(aComboBox: NSComboBox) -> Int {
return(COLORS_OF.count)
}
func comboBoxCell(aComboBox: NSComboBox, objectValueForItemAtIndex index: Int) -> AnyObject {
return(COLORS_OF[index])
}
let COLORS_OF = [ "Blue", "Green", "Purple", "Red", "Yellow" ]
}
You probably forgot to check Uses Data Source or you have to delete datasource connection and reconnect it again (weird bug of Xcode).
Other then that if your outlets are correctly hooked your code works.
Is it possible to change a value in Cocoa depending on the direction of the trackpad scroll/ magic mouse 'wheel' scroll etc.
For example
var value = 0
func startsScrolling() {
if direction == up {
value += 5
} else if direction == down {
value -= 5
}
}
This would continuous so as you scroll up on the trackpad the direction is down and so it takes 5 off every time the fingers move up, and vice versa.
Thanks and sorry for the bad pseudo code.
You can create a custom window and override scrollWheel NSEvent. You can check the scrollingDeltaX and scrollingDeltaY properties.
class CustomWindow: NSWindow {
override func scrollWheel(theEvent: NSEvent) {
println( "Mouse Scroll scrollingDeltaX = \(theEvent.scrollingDeltaX)" )
println( "Mouse Scroll scrollingDeltaY = \(Int(theEvent.scrollingDeltaY))" )
}
}
If you are using storyboards you have to override scrollWheel NSEvent of your viewController:
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var strDeltaX: NSTextField!
#IBOutlet weak var strDeltaY: NSTextField!
#IBOutlet weak var strScrollingDeltaX: NSTextField!
#IBOutlet weak var strScrollingDeltaY: NSTextField!
#IBOutlet weak var strValueX: NSTextField!
#IBOutlet weak var strValueY: NSTextField!
var xValue:CGFloat = 0
var yValue:CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
}
override var representedObject: AnyObject? {
didSet {
}
}
override func scrollWheel(theEvent: NSEvent) {
strDeltaX.stringValue = "deltaX: \(theEvent.deltaX)"
strDeltaY.stringValue = "deltaY: \(theEvent.deltaY)"
strScrollingDeltaX.stringValue = "scrollingDeltaX: \(theEvent.scrollingDeltaX)"
strScrollingDeltaY.stringValue = "scrollingDeltaY: \(theEvent.scrollingDeltaY)"
xValue += theEvent.scrollingDeltaX
yValue += theEvent.scrollingDeltaY
strValueX.stringValue = "xValue: \(xValue)"
strValueY.stringValue = "yValue: \(yValue)"
}
}
First you need to add a scroll view to your ViewController and link it. You can make it transparent if you don't want it to show. If you have buttons on your ViewController you can send it to back. I've converted code from objective-c. More power to you :)
var pointNow: CGPoint?
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
pointNow = scrollView.contentOffset
}
func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView.contentOffset.y < pointNow!.y {
println("down")
}
else {
if scrollView.contentOffset.y > pointNow!.y {
println("up")
}
}
}
My first question here. I'm making the jump from Delphi and Pascal to Xcode and Swift and feeling totally overwhelmed by the change.
I'm simply attempting to change the text of a label when a button is clicked. Here is my code from ViewController.swift
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var Label1: NSTextField!
#IBOutlet weak var ChangeText: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func ButtonPressed(sender: AnyObject) {
Label1.stringValue = "Hello World"
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
When I run the application as soon as I click the button the application hangs.
I've also tried exchanging stringValue with text but NSTextField does not use this. I can't seem to make the label show up as UITextField.
What am I doing wrong?
Im simply trying to make it so when I click on a UIButton (for which it currently shows the image of a shell), the image changes into something else (in this case, a coin).
This is what i tried so far and have not had any success. I cant find anything to do this for Swift.Thanks.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var lblOutput: UILabel!
#IBOutlet weak var lblWin: UILabel!
#IBOutlet weak var lblLost: UILabel!
#IBOutlet weak var lblWinsAmt: UILabel!
#IBOutlet weak var lblLossesAmt: UILabel!
let coin = UIImage(named: "penny_head") as UIImage;
override func viewDidLoad() {
super.viewDidLoad()
//imgShell1.hidden = true //doesnt work
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func btnStart(sender: UIButton) {
}
#IBAction func btnShell1(sender: UIButton) {
sender.setImage(coin,forState: UIControlState.Highlighted);
}
The way you're setting up the control is incorrect. Assuming you have a button property named btnShell (and it's the button you want to setup) change your viewDidLoad() method to:
override func viewDidLoad()
{
super.viewDidLoad()
btnShell.setImage(imgShell1, forState:.Normal);
btnShell.setImage(coin, forState:.Highlighted);
}
And then remove the setImage(_:forState:) call from the action method:
#IBAction func btnShell1(sender: UIButton) {
sender.setImage(coin,forState: UIControlState.Highlighted);
}
To permanently change the button image on tap, you have to use the .Normal enum case and not .Highlighted for the control state:
sender.setImage(coin,forState: UIControlState.Normal)
Setting the image for the .Highlighted state makes the new image appear only when the button is in that state, i.e. when it is tapped.
If you are looking to change the UIButton's background image permanently you have to use Antonio's method:
sender.setImage(coin,forState: UIControlState.Normal)
This won't change the UIKit's default highlighting when you tap the button. When you don't want the button to be highlighted when you touch it or have different appearances for different states, then you might be better off using an UIImageView.
In viewDidLoad():
imgView?.image = imgOne
imgView?.userInteractionEnabled = true
imgView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "changeImage:"))
The function that changes the image:
func changeImage(recognizer: UITapGestureRecognizer){
self.imgView?.image = imgTwo
}
You can also use isSelected.
button.setImage(image2, for: .normal)
button.setImage(image1, for: .selected)