Xcode 14 IBDesignable preview crashes, however IBInspectable properties appear and work as expected - xcode

I am making a reusable view to draw a playing card. The reusable view (which consists of an xib and swift files). The screen with this reusable view build & runs on the simulator without any issues, however in the XIB file, the preview doesn't work. In attributes inspector it says "Designables: Error", and inside the content view, where I want to preview the reusable view, it says "NameOfMyReusableView ‼️Designable".
Here's the reusable view code:
#IBDesignable class PlayingCardView: UIView {
#IBOutlet var contentView: UIView!
#IBOutlet private weak var cardBackground: UIImageView!
#IBOutlet private weak var topLeadingLabel: UILabel!
#IBOutlet private weak var bottomTrailingLabel: UILabel!
#IBInspectable var rank: String = "" {
didSet {
topLeadingLabel.text = rank
bottomTrailingLabel.text = rank
}
}
#IBInspectable var suit: String = "" {
didSet {
topLeadingLabel.text?.append("\n\(suit)")
bottomTrailingLabel.text?.append("\n\(suit)")
}
}
private var isUpsideDown: Bool = false {
didSet {
if isUpsideDown {
topLeadingLabel.alpha = 0
bottomTrailingLabel.alpha = 0
cardBackground.alpha = 1
} else {
topLeadingLabel.alpha = 1
bottomTrailingLabel.alpha = 1
cardBackground.alpha = 0
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
loadNibFile()
setup()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
loadNibFile()
setup()
}
func setup() {
isUpsideDown = false
contentView.layer.cornerRadius = 16
topLeadingLabel.text = "5\n❤️"
bottomTrailingLabel.text = "5\n❤️"
bottomTrailingLabel.transform = CGAffineTransform.identity.rotated(by: CGFloat.pi)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(flipCard(_:)))
contentView.addGestureRecognizer(tapGesture)
}
private func loadNibFile() {
Bundle.main.loadNibNamed("PlayingCardView", owner: self)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
contentView.clipsToBounds = true
}
#objc private func flipCard(_ sender: UITapGestureRecognizer) {
isUpsideDown.toggle()
}
}
I tried clean build, but obviously it didn't help.

Related

GeoTag Images from image picker swift 3

I want to get geotag location from image which is selected from image picker. I am using this code
if picker.sourceType == UIImagePickerControllerSourceType.PhotoLibrary
{
if let currentLat = pickedLat as CLLocationDegrees?
{
self.latitude = pickedLat!
self.longitude = pickedLong!
}
else
{
var library = ALAssetsLibrary()
library.enumerateGroupsWithTypes(ALAssetsGroupAll, usingBlock: { (group, stop) -> Void in
if (group != nil) {
println("Group is not nil")
println(group.valueForProperty(ALAssetsGroupPropertyName))
group.enumerateAssetsUsingBlock { (asset, index, stop) in
if asset != nil
{
if let location: CLLocation = asset.valueForProperty(ALAssetPropertyLocation) as CLLocation!
{ let lat = location.coordinate.latitude
let long = location.coordinate.longitude
self.latitude = lat
self.longitude = lat
println(lat)
println(long)
}
}
}
} else
{
println("The group is empty!")
}
})
{ (error) -> Void in
println("problem loading albums: \(error)")
}
}
}
i want to know to covert this code in swift 3 .I am new in coding with swift 3 .It will be very helpful
After hours of searching i got my ans
import UIKit
import Photos
class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var locationLabel: UILabel!
#IBOutlet weak var timeLabel: UILabel!
#IBOutlet weak var logi: UILabel!
#IBOutlet weak var lati: UILabel!
var lat = String()
var log = String()
var location = String()
var timeTaken = "Not Known"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func imagegeo(_ sender: Any) {
let imagePicker = UIImagePickerController()
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var chosenImage:UIImage?
if let URL = info[UIImagePickerControllerReferenceURL] as? URL {
print("We got the URL as \(URL)")
let opts = PHFetchOptions()
opts.fetchLimit = 1
let assets = PHAsset.fetchAssets(withALAssetURLs: [URL], options: opts)
print(assets)
for assetIndex in 0..<assets.count {
let asset = assets[assetIndex]
location = String(describing: asset.location)
log = String(describing: asset.location?.coordinate.longitude)
lat = String(describing: asset.location?.coordinate.latitude)
timeTaken = (asset.creationDate?.description)!
print(log)
print(location)
print(lat)
}
}
if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
chosenImage = editedImage
} else if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
chosenImage = selectedImage
}
dismiss(animated: true) {
DispatchQueue.main.async {
self.imageView.image = chosenImage
self.timeLabel.text = self.timeTaken
self.locationLabel.text = self.location
self.lati.text = self.lat
self.logi.text = self.log
}
}
}
}

How to create a NSTableview with custom viewcells

I tried many different ways to create a NSTableview with custom NSTableCellView but I could not make it work. The question is, how can I do that thing?
Here is the last thing I tried:
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
var cellIdentifier: String = ""
if tableColumn == tableView.tableColumns[0] {
cellIdentifier = "CellID"
if let cell = tableView.makeViewWithIdentifier(cellIdentifier, owner: self ) as? MyTableCellView {
cell.identifier = cellIdentifier
// array is an array that contains NSView with layers with different colors
cell.myView = array[row]
return cell
}
}
return nil
}
After adding a label:
And the full code:
class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
tableview.setDelegate(self)
tableview.setDataSource(self)
let view = NSView(frame: NSRect(x: 0, y: 0, width: 200, height: 200))
view.layer?.backgroundColor = NSColor.blueColor().CGColor
array.append(view)
let view2 = NSView(frame: NSRect(x: 0, y: 0, width: 200, height: 200))
view2.layer?.backgroundColor = NSColor.greenColor().CGColor
array.append(view2)
array2label.append("bu")
array2label.append("buu")
tableview.reloadData()
// Do any additional setup after loading the view.
}
override func viewWillAppear() {
//tableview.reloadData()
laView.layer?.backgroundColor = NSColor.greenColor().CGColor
}
#IBOutlet weak var laView: NSView!
#IBOutlet weak var tableview: NSTableView!
var array = [NSView]()
var array2label = [String]()// = ["bu","buu"]
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
if (tableView.identifier == "Taula") {
return array.count
//return taulaGrafics.count
} else {
return 0
}
}
#IBOutlet weak var viewDeProva: NSView!
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
print ( "Preparem la TableView" )
var cellIdentifier: String = ""
if tableColumn == tableView.tableColumns[0] {
cellIdentifier = "CellID"
if let cell = tableView.makeViewWithIdentifier(cellIdentifier, owner: self ) as? MyTableCellView {
print ( "aqui" )
print(array)
print(array2label)
cell.identifier = cellIdentifier
cell.myView = array[row]
cell.label.stringValue = array2label[row]
return cell
}
}
return nil
}
#IBAction func afegir(sender: NSButton) {
let view = NSView(frame: NSRect(x: 0, y: 0, width: 200, height: 200))
view.layer?.backgroundColor = NSColor.yellowColor().CGColor
array.append(view)
array2label.append("buLabel")
tableview.reloadData()
}
#IBAction func treure(sender: NSButton) {
array.removeLast()
tableview.reloadData()
}
}
if let cell = tableView.makeViewWithIdentifier(cellIdentifier, owner: self ) as? MyTableCellView {
print ( "aqui" )
print(array)
print(array2label)
cell.identifier = cellIdentifier
cell.myView = array[row]
cell.myView.wantsLayer = true
cell.label.stringValue = array2label[row]
return cell
}
With this changes the view show the color that is expected to be:
class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
print("DidLoad")
tableview.setDelegate(self)
tableview.setDataSource(self)
}
override func viewWillAppear() {
print("WillAppear")
array.append(NSColor.blueColor().CGColor)
array2label.append("buIni")
tableview.reloadData()
laView.layer?.backgroundColor = NSColor.greenColor().CGColor
}
#IBOutlet weak var laView: NSView!
#IBOutlet weak var tableview: NSTableView!
var array = [CGColor]()
var array2label = [String]()
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
if (tableView.identifier == "Taula") {
return array.count
//return taulaGrafics.count
} else {
return 0
}
}
#IBOutlet weak var viewDeProva: NSView!
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
print ( "Preparem la TableView" )
var cellIdentifier: String = ""
if tableColumn == tableView.tableColumns[0] {
cellIdentifier = "CellID"
if let cell = tableView.makeViewWithIdentifier(cellIdentifier, owner: self ) as? MyTableCellView {
print ( "Here" )
print(array)
print(array2label)
cell.identifier = cellIdentifier
cell.myView.layer?.backgroundColor = array[row]
cell.label.stringValue = array2label[row]
return cell
}
}
return nil
}
#IBAction func afegir(sender: NSButton) {
let color = NSColor.yellowColor().CGColor
array.append(color)
array2label.append("buLabel")
tableview.reloadData()
}
#IBAction func treure(sender: NSButton) {
array.removeLast()
array2label.removeLast()
tableview.reloadData()
}
}
The problem was that the NSView inside the NSTableViewCell can't be replaced by another view, because what you do doing that is changing the prototype cell. So if you replace the view the NSTableViewCell doesn't recognize that NewView layer (I'm not sure 100% why). Looks like just some information is shared.
So, how can we pass info to the tableview cell? How can we show a layer there?? Well the answer is that just modifying the prototype cell NSTableCellView added on the interface builder or its subclass (like my case, see bellow the cell). Modifying its content, but not the NSView!, inside the viewForTableColumn function. See in this piece of code:
if let cell = tableView.makeViewWithIdentifier(cellIdentifier, owner: self ) as? MyTableCellView {
cell.identifier = cellIdentifier // that is to identify the cell
cell.myView.layer?.backgroundColor = array[row] // see that the array contains CGCOLORS not NSViews
// The program was not showing the colors because the
// view copied doesn't copy its layer, or at least
// doesn't show it.
// Even though, if you say: *******---This is incorrect:
cell.myView = arrayOfNSViews[row] //the program will
// crash when removing 2 rows, myView = nil !!!!.
// That is because when you remove the item of the array
// somehow you are removing myView too, because it make a
// copy or a reference to it (Not sure what exactly).
// Here continues the correct program: ******---
cell.label.stringValue = array2label[row]
return cell
}
Also see that in the textField case: cell.label.stringValue = array2label[row], you change the string value of the textfield, not the whole NSTextfield.
So guys remember and repeat my words: "I'm not going to change the view of the cell, just its properties". I just spend 4 days to find that...
Here is the NStableCellView promised:
class MyTableCellView: NSTableCellView {
#IBOutlet weak var myView: NSView!
#IBOutlet weak var label: NSTextField!
}
One Image of the view hierarchy:

custom datasource method is not calling in swift 2.0

here is my BorderView and The task I'm trying to do is to hookup the custom object (Border) to the main storyboard.
import UIKit
protocol BorderViewDataSource: class{
func colorForBorderView(sender:BorderView) -> String? }
class BorderView: UIView {
#IBOutlet weak var topLeftImage: UIImageView!
#IBOutlet weak var topRightImage: UIImageView!
#IBOutlet weak var bottomLeftImage: UIImageView!
#IBOutlet weak var bottomRightImage: UIImageView!
var view: UIView!
weak var dataSource:BorderViewDataSource?
override init(frame: CGRect) {
super.init(frame: frame)
xtraSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xtraSetup()
}
func xtraSetup() {
view = loadViewFromNib()
// use bounds not frame or it'll be offset
view.frame = bounds
// Make the view stretch with containing view
view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
// Adding custom subview on top of our view (over any custom drawing > see note below)
addSubview(view)
let borderColor = dataSource?.colorForBorderView(self) ?? "red"
applyColor(borderColor)
}
// this is an actual load from nib module
func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "BorderView", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
return view
}
func applyColor(borderColor:String){
var imageName = "border-topleft-\(borderColor)"
topLeftImage.image = UIImage(named: imageName)
imageName = "border-topright-\(borderColor)"
print("Border color is \(borderColor), and Last image name is \(imageName)")
}
}
So when i open the app it will show the image of different color depending on what the value of the color is set in the ViewContorller.swift and here is my viewcontroller. swift code
class ViewController: UIViewController, BorderViewDataSource {
var borderType = 2
#IBOutlet weak var borderView: BorderView!{
didSet{
borderView.dataSource = self
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func colorForBorderView(sender: BorderView) -> String? {
var borderColor = "blue"
switch borderType {
case 1: borderColor = "blue"
case 2: borderColor = "purple"
case 3: borderColor = "red"
default: break
}
print("Border Type is \(borderType), and border color is \(borderColor)")
return borderColor
}
}
but colorForBorderView method is not calling when i running the app
It seems when the view is initialized, the dataSource property of that view is nil. And you only call that method during initialization of your view. You can apply the color after viewDidLoad() in your view controller
From the documentation:
When you assign a default value to a stored property, or set its
initial value within an initializer, the value of that property is set
directly, without calling any property observers.
That's the reason why the datasource is not set. Put the line borderView.dataSource = self into viewDidLoad()

Is it possible to change value by scrolling OSX trackpad (Swift)?

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")
}
}
}

How to use public NSURL Variable

I'm new to Swift and Xcode overall,
I want to create a new public NSURL variable which I want to use later in my code to give it value and use that value later from it. like below :
class AddUrlViewController: NSViewController {
#IBOutlet weak var txtPath: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func btnFileDialog(sender: NSButton) {
let myFiledialog:NSOpenPanel = NSOpenPanel()
myFiledialog.canChooseDirectories = true
myFiledialog.canChooseFiles = false
var returnCode:NSInteger = myFiledialog.runModal()
if (returnCode == NSOKButton) {
/////////// Use it here
var directoryUrl = myFiledialog.URL?.absoluteURL
}
}
#IBAction func btnStart(sender: NSButton) {
//////////// Use it here
}
}
How should I achieve this the best way ?
Simply define the variable as a property outside of the function scope -
class AddUrlViewController: NSViewController {
#IBOutlet weak var txtPath: NSTextField!
var directoryUrl:NSURL?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func btnFileDialog(sender: NSButton) {
let myFiledialog:NSOpenPanel = NSOpenPanel()
myFiledialog.canChooseDirectories = true
myFiledialog.canChooseFiles = false
var returnCode:NSInteger = myFiledialog.runModal()
if (returnCode == NSOKButton) {
/////////// Use it here
self.directoryUrl = myFiledialog.URL?.absoluteURL
}
}
#IBAction func btnStart(sender: NSButton) {
if (self.directoryUrl != nil) {
//////////// Use it here
}
}
}

Resources