NSButton action not working from custom NSView - swift

I have my Push button in subview called AddContactViewController
And I'm showing it in ScrollView in another ViewController called AddContactScrollViewController.
I'm setting action for my button in AddContactViewController
But, when I click my button nothing happens.
Here are the screenshots my IB. And below is the code.
Main view controller
SubView
import Cocoa
class AddContactScrollViewController: NSViewController {
#IBOutlet weak var scrollView: NSScrollView!
var contentView: NSView?
override func viewDidLoad() {
super.viewDidLoad()
contentView = NSView(frame: NSRect(x: 0, y: 0, width: self.view.frame.width, height: 1123))
contentView!.wantsLayer = true
contentView!.layer?.backgroundColor = NSColor.clear.cgColor
let tempVC = self.storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "addAddress")) as! AddAddressViewController
vc.view.setFrameOrigin(NSPoint(x: 0, y: 0))
vc.view.setFrameSize(NSSize(width: 1200, height: 1123))
vc.view.wantsLayer = true
contentView!.addSubview(vc.view)
scrollView.documentView = contentView
// scroll to the top
if let documentView = scrollView.documentView {
documentView.scroll(NSPoint(x: 0, y: documentView.bounds.size.height))
}
}
override func viewDidAppear() {
}
}
import Cocoa
class AddContactVeiwController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func addPhoneButtonClicked(_ sender: Any) {
print("phone button clicked")
}

Related

On mouse hover on NSButton Change Cursor (for OSX)

I made a subclass of NSButton
class Button: NSButton {
var cursor = NSCursor()
override func resetCursorRects() {
super.resetCursorRects()
addCursorRect(bounds, cursor: .pointingHand)
}
}
on my VC class i have added
let tempBtn = Button()
to call subclass but still no use. What am i doing wrong.
This works for me.
class ViewController: NSViewController {
class Button: NSButton {
override func resetCursorRects() {
super.resetCursorRects()
addCursorRect(bounds, cursor: .pointingHand)
}
}
override func viewDidLoad() {
super.viewDidLoad()
let button = Button(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
view.addSubview(button)
}
}

I cannot set background picture (Swift)

I made a simple timer app and I want to add a background picture or video in the background. However, I cannot do it and I do not know why.
Can someone tell me why the picture does not show up in the background? I set the background in the viewDidLoad method. Maybe the #IBOutlet var backgroundView: UIView! part is wrong? I connected it with the given UIView when I made this Xcode project.
class ViewController: UIViewController, UITableViewDelegate {
let mainStopwatch = Stopwatch()
let lapStopwatch = Stopwatch()
var isPlay: Bool = false
var laps: [String] = []
//UI components
#IBOutlet weak var lapRestButton: UIButton!
#IBOutlet weak var playPauseButton: UIButton!
#IBOutlet weak var lapTimerLabel: UILabel!
#IBOutlet weak var timerLabel: UILabel!
#IBOutlet weak var lapsTableView: UITableView!
#IBOutlet var backgroundView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.view.addBackground(name: "nathan-dumlao-iXXyrEwZgiU-unsplash")
lapRestButton.isEnabled = false
lapsTableView.dataSource = self
lapsTableView.delegate = self
}
//UI Settings
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait
}
//Actions
#IBAction func playPauseTimer(_ sender: Any) {
lapRestButton.isEnabled = true
changeButton(lapRestButton, title: "Lap", color: .black)
}
}
And this is another swift file for setting the background picture.
extension UIView {
func addBackground(name: String) {
let width = UIScreen.main.bounds.size.width
let height = UIScreen.main.bounds.size.height
let imageViewBackground = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
imageViewBackground.image = UIImage(named: name)
imageViewBackground.contentMode = .scaleAspectFill
self.addSubview(imageViewBackground)
self.sendSubviewToBack(imageViewBackground)
}
var timer = Timer()
var counter = 0 // the counter is use for the index of array in below code
var imageArr = ["image","image1","image2"] //Image arr
override func viewDidLoad() {
super.viewDidLoad()
self.addBackground(name: "image") // set default image on your view
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true) // timer will start and it will change view background image after one min you can change time according to your requirement.
}
// called every time interval from the timer
#objc func timerAction() {
self.addBackground(name: self.imageArr[counter]) // here we are change our view background image when timer function is run on every min
counter += 1 // every-time counter will increment by one so the image array index will also increment and we will get our new image
}
// that's your code only you need to change one line that last one
func addBackground(name: String) {
let width = UIScreen.main.bounds.size.width
let height = UIScreen.main.bounds.size.height
let imageViewBackground = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
imageViewBackground.image = UIImage(named: name)
imageViewBackground.contentMode = .scaleAspectFit
self.viewref.addSubview(imageViewBackground) // here we will set the image view to our view
}
hope this will help you for change your background of view.
Thanks.
Seems like you've don't using your backgroundView to anywhere in class code , So first
Remove backgroundView from storyBoard and also remove code from class file
viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
self.addBackground(name: "nathan-dumlao-iXXyrEwZgiU-unsplash") //Update this line
lapRestButton.isEnabled = false
lapsTableView.dataSource = self
lapsTableView.delegate = self
}
Update your extension code to below one
extension YourViewController {
func addBackground(name: String) {
let width = self.view.frame.size.width
let height = self.view.frame.size.height
let imageViewBackground = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
imageViewBackground.image = UIImage(named: name)
imageViewBackground.contentMode = .scaleAspectFill
self.view.addSubview(imageViewBackground)
// self.sendSubviewToBack(imageViewBackground) //Comment this line
}
}

Event scrolling (scrollViewDidScroll) never called - Swift 3

I would like to detect when the user scroll, I used a UIScrollView, I implemented the UIScrollViewDelegate in my ViewController and tried scrollViewDidScroll, scrollViewDidEndScrollingAnimation and all the others but these events are never called.
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
let xOrigin = self.view.frame.width
let view1 = View1(frame: CGRect(x: 0, y: 0, width:self.view.frame.width, height: self.view.frame.height))
let view2 = View2(frame: CGRect(x: xOrigin, y: 0, width: self.view.frame.width, height: self.view.frame.height))
let view3 = View3(frame: CGRect(x: xOrigin * 2, y: 0, width: self.view.frame.width, height: self.view.frame.height))
scrollView.addSubview(view1)
scrollView.addSubview(view2)
scrollView.addSubview(view3)
self.scrollView.contentSize = CGSize(width: self.view.frame.width * 3, height: self.view.frame.height)
// hide the scrol bar.
scrollView?.showsHorizontalScrollIndicator = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("end scroll")
}
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
print("end scroll")
}
}
You just have to add the delegate to your viewDidLoad() func:
override func viewDidLoad() {
super.viewDidLoad()
//add this line
self.scrollView.delegate = self
}

Segue background flicker upon first load in Swift

I am getting a flicker (likely view controller not fully loaded) for a split second upon first load on my second view controller. I've tried to preset through a segue as well as setting it in a custom initializer prior to load. The affect that I've created is to keep the same background for each view controller. Note, that this only occurs upon first transition over to the view controller. It doesn't happen after the first segue.
Help is greatly appreciated. I'm including the applicable code below.
First View Controller:
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate, UIScrollViewDelegate
{
// Background Image
var backgroundImage: UIImageView!
var backgroundImages: [String] = ["canyon.jpeg","channel.jpeg","glacier.jpeg","northernlights.jpeg", "jungle"]
var currentBackground = 0
init(_ coder: NSCoder? = nil)
{
// Logic Goes Here
if let coder = coder
{
super.init(coder: coder)!
} else
{
super.init(nibName: nil, bundle: nil)
}
}
required convenience init(coder: NSCoder)
{
self.init(coder)
}
override func viewDidLoad()
{
setUpView()
super.viewDidLoad()
}
func setUpView()
{
backgroundImage = UIImageView(frame: CGRect( x: 0 , y: 0, width: ScreenWidth, height: ScreenHeight))
backgroundImage.image = UIImage(named: backgroundImages[currentBackground])
backgroundImage.alpha = 1
let playButtonPercent:CGFloat = 0.75
playButton = UIButton(frame: CGRect(x: (ScreenWidth * 0.5) - (ScreenWidth * playButtonPercent / 2), y: (ScreenHeight * (3 / 4)) - (ScreenHeight * (15/480)), width: (ScreenWidth * playButtonPercent), height: ScreenHeight * (30/480)))
playButton.setTitle("PLAY", for: UIControlState())
playButton.addTarget(self, action: #selector(self.playRelease), for: .touchUpInside)
self.view.addSubview(backgroundImage)
self.view.addSubview(playButton)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if (segue.identifier == "Play")
{
if let nextViewController = (segue.destination as? GameListViewController)
{
nextViewController.currentBackground = currentBackground
nextViewController.backgroundImage.image = UIImage(named: self.backgroundImages[currentBackground])
}
}
}
func playRelease()
{
// Segue
performSegue(withIdentifier: "Play", sender: self)
}
}
View Controller 2:
class GameListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{
let backgroundImages = ["canyon.jpeg","channel.jpeg","glacier.jpeg","northernlights.jpeg", "jungle"]
var currentBackground = 0
var backgroundImage: UIImageView!
override func viewDidLoad()
{
backgroundImage = UIImageView(frame: CGRect( x: 0 , y: 0, width: ScreenWidth, height: ScreenHeight))
super.viewDidLoad()
setUpView()
}
}
func setUpView()
{
backgroundImage.image = UIImage(named: backgroundImages[currentBackground])
backgroundImage.alpha = 1
self.view.addSubview(backgroundImage)
view.sendSubview(toBack: backgroundImage)
}
}

How can I draw a simple line?(OS X)

I tried this:
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var butt: NSButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
#IBAction func buttPressed(sender: AnyObject) {
let myPath = NSBezierPath()
myPath.moveToPoint(CGPoint(x: 20, y: 20))
myPath.lineToPoint(CGPoint(x: 100, y: 100))
myPath.stroke()
}
}
It compiles and runs but doesn't draw anything when the button is clicked.
I tried setting the color: NSColor.redColor().set(), setting the width: myPath.lineWidth = 20 and using NSPoint instead of CGPoint but still the same thing happens. What is it that I'm missing?
Thank you for your answers in advance and sorry for the elementary question.
Try doing it inside drawRect (NSView):
import Cocoa
final class Line: NSView {
override func drawRect(dirtyRect: NSRect) {
let myPath = NSBezierPath()
myPath.moveToPoint(CGPoint(x: 20, y: 20))
myPath.lineToPoint(CGPoint(x: 100, y: 100))
myPath.stroke()
}
}
final class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
let frame = CGRect(x: 0, y: 0, width: 100, height: 100)
let line = Line(frame: frame)
view.addSubview(line)
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
}