NSPrintOperation View Is blank - macos

Im still new to OSX / Swift 3.1 but Im a little stumped, Ive build an epos system and its work all fine, Im just stuck with printing out the receipt.
I have build a XIB View and the corresponding swift file for it show below
class Receipt: NSView {
let currentOrder = NewOrder.instance
#IBOutlet var view: NSView!
#IBOutlet weak var facebookString: NSTextField!
#IBOutlet weak var websiteString: NSTextField!
#IBOutlet weak var totalString: NSTextField!
#IBOutlet weak var vatString: NSTextField!
#IBOutlet weak var subTotalString: NSTextField!
#IBOutlet weak var CashierName: NSTextField!
#IBOutlet weak var orderNumber: NSTextField!
#IBOutlet weak var date: NSTextField!
#IBOutlet weak var itemList: NSTextField!
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
Bundle.main.loadNibNamed("ReceiptView", owner: self, topLevelObjects: nil)
let contentFrame = NSMakeRect(0, 0, frame.size.width, frame.size.height)
self.view.frame = contentFrame
self.addSubview(self.view)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
subTotalString.stringValue = "Hello World"
view.draw(dirtyRect)
subTotalString.draw(dirtyRect)
}
}
I have added the nib name to the XIB reference and loaded a new Receipt() class and added the view to the NSPrintOperation.
let order = NewOrder.instance
let sview = Receipt()
let printer = NSPrinter(name: "EPSON TM-T88V");
let printData = NSPrintInfo()
printData.printer = printer!
let printerName = printer?.name
let docSize = NSMakeSize(CGFloat(204.0), CGFloat(200));
let res = "180x180dpi";
let speed = "Auto";
let blank = "Off";
let paperCut = "DocFeedCut";
let chashDrwr1 = "After";
let chashDrwr2 = "Off";
let buzzerControl = "Off";
let buzzerPattern = "Internal";
let buzzerRepeat = "1";
let data = TMPrintSupport.tmSetJobTicket(printData, printerName: printerName, documentSize: docSize, resolution: res, speed: speed, blank: blank, paperCut: paperCut, chashDrwr1: chashDrwr1, chashDrwr2: chashDrwr2, buzzerControl: buzzerControl, buzzerPattern: buzzerPattern, buzzerRepeat: buzzerRepeat)
let po = NSPrintOperation(view: sview.view, printInfo: printData)
// po.showsPrintPanel = false
po.canSpawnSeparateThread = true
po.cleanUp()
return po.run()
When the print view is shown it is blank and does not contain the view and is blank, I've also test printed and the print is just blank.
Any suggestions welcome, thanks

Related

Firebase, UIImageView and Kingfisher cache - issue with converting value

Im trying to implement Kingfisher image cache, with images hosted in firebase for my UITableView. However I'm having issues with converting the value types of imageView.kf.setImage..
The error I get is "Cannot convert value of type 'RetrieveImageTask' to type 'UIImageView' in coercion" this is related to the " let profileImage = imageView.kf.setImage(with: imageURL) as UIImageView" line of code below.
I don't seem to understand what imageView.kf.setImage is... I expected it to be an image within in an image view... but seems like it is something else..
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "feedCell") as? feedCell else
{print("dequeueResuableCell return an else"); return UITableViewCell() }
// data fro userdataArray
let imageURL = URL(string: userdataArray[indexPath.row].profileImage)
let imageView = UIImageView()
let profileImageView = imageView.kf.setImage(with: imageURL) as UIImageView
let profileName = userdataArray[indexPath.row].name
// data from activityArray
let activityDate = "8 October"
let distance = "8 km"
let activityName = "test"
let sightings = "20"
let kills = "10"
// Border styling
// cell.addBottomBorderWithColor(color: UIColor.lightGray, width: 0.5)
cell.clipsToBounds = true
// Shadow styling
cell.layer.shadowColor = UIColor.darkGray.cgColor
cell.layer.shadowOpacity = 0.25
cell.layer.shadowRadius = 1
cell.layer.shadowOffset = CGSize(width: 0, height: 1) // shadow on the bottom right
cell.layer.masksToBounds = false // important to show the shadow
cell.configureCell(profileImageView: profileImageView, profileName: profileName, activityDate: activityDate, ActivityName: activityName , distance: distance, sightings: sightings, kills: kills)
return cell
}
configureCell is in a separate view file
import UIKit
class feedCell: UITableViewCell {
#IBOutlet weak var profileImage: UIImageView!
#IBOutlet weak var profileName: UILabel!
#IBOutlet weak var activityDate: UILabel!
#IBOutlet weak var name: UILabel!
#IBOutlet weak var distance: UILabel!
#IBOutlet weak var sightings: UILabel!
#IBOutlet weak var kills: UILabel!
func configureCell(profileImageView: UIImageView, profileName: String, activityDate: String, ActivityName: String, distance: String, sightings: String, kills: String) {
self.profileImage = profileImageView
self.profileName.text = profileName
self.activityDate.text = activityDate
self.name.text = ActivityName
self.distance.text = distance
self.sightings.text = sightings
self.kills.text = kills
}
}
You can't declare profileImageView with "imageView.kf.setImage(with: imageURL) as UIImageView" because It doesn't return anything.
You need to do like this;
let profileImageView = UIImageView()
profileImageView.kf.setImage(with: imageURL)

Fatal error editing core data objects

I am newbie to programming. With CoreData, I am trying to create the function to edit the data in input text field to edit the data to be saved to core data. I am having fatal error:
unexpectedly found nil while unwrapping an Optional value
#IBAction func update(sender: AnyObject) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let manageContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "Coursework")
do {
let results = try manageContext.executeFetchRequest(fetchRequest)
let attribute = results[0] as! NSManagedObject
detailItem?.value = modulename.text
attribute.setValue(courseworkname.text, forkey: "courseworkname")
attribute.setValue(dueDateLabel.text, forkey: "duedate")
attribute.setValue(level.text, forkey: "level")
attribute.setValue(mark.text, forkey: "mark")
attribute.setValue(modulename.text, forkey: "modulename")
attribute.setValue(notes.text, forkey: "notes")
attribute.setValue(progressbar.text, forkey: "progressbar")
attribute.setValue(reminder.text, forkey: "reminder")
attribute.setValue(value.text, forkey: "value")
try manageContext.save()
}catch let error as NSError {
}
}
Full code:
#IBOutlet weak var detailDescriptionLabel: UILabel!
#IBOutlet weak var dueDateLabel: UITextField!
#IBOutlet weak var value: UITextField!
#IBOutlet weak var courseworkname: UITextField!
#IBOutlet weak var modulename: UITextField!
#IBOutlet weak var level: UITextField!
#IBOutlet weak var mark: UITextField!
#IBOutlet weak var reminder: UITextField!
#IBOutlet weak var notes: UITextField!
#IBAction func edit(sender: AnyObject) {
modulename.userInteractionEnabled = true
modulename.enabled = true
dueDateLabel.userInteractionEnabled = true
value.userInteractionEnabled = true
modulename.userInteractionEnabled = true
level.userInteractionEnabled = true
mark.userInteractionEnabled = true
reminder.userInteractionEnabled = true
notes.userInteractionEnabled = true
//Interaction
value.enabled = true
dueDateLabel.enabled = true
courseworkname.enabled = true
modulename.enabled = true
level.enabled = true
mark.enabled = true
reminder.enabled = true
notes.enabled = true
}
#IBAction func update(sender: AnyObject) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let manageContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "Coursework")
do {
let results = try manageContext.executeFetchRequest(fetchRequest)
let attribute = results[0] as! NSManagedObject
detailItem?.value = modulename.text
attribute.setValue(courseworkname.text, forKey: "courseworkname")
attribute.setValue(dueDateLabel.text, forKey: "duedate")
attribute.setValue(level.text, forKey: "level")
attribute.setValue(mark.text, forKey: "mark")
attribute.setValue(modulename.text, forKey: "modulename")
attribute.setValue(notes.text, forKey: "notes")
attribute.setValue(progressbar.text, forKey: "progressbar")
attribute.setValue(reminder.text, forKey: "reminder")
attribute.setValue(value.text, forKey: "value")
try manageContext.save()
}catch let error as NSError{
}
}
var detailItem: Coursework?
var detailItem2: Task?
{
didSet {
// Update the view.
self.configureView()
}
}
func configureView() {
// Update the user interface for the detail item.
if let detail = self.detailItem {
if let label = self.detailDescriptionLabel {
label.text = detail.courseworkname
}
if let label = self.dueDateLabel {
label.text = detail.duedate
}
if let label = self.value {
label.text = detail.value
}
if let label = self.courseworkname {
label.text = detail.courseworkname
}
if let label = self.modulename {
label.text = detail.modulename
}
if let label = self.level {
label.text = detail.level
}
if let label = self.mark {
label.text = detail.mark
}
if let label = self.reminder{
label.text = detail.reminder
}
if let label = self.notes{
label.text = detail.notes
}
}
Is it a new attribute? You have to uninstall and reinstall the app when you're changing your CoreData models.
Also just saw that you don't have an IBOutlet defined for progressbar, if you link it it should work.

Change textcolor for UITextField

I am trying to change the textColor.Though I enter the numbers correctly into the textfields,it is working only for "else" condition but not for 'if'. Here is my code.
Can anyone fix this issue. Thanks
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var questionOne: UILabel!
#IBOutlet weak var fieldOne: UITextField! // textFields should be set as labels
#IBOutlet weak var questionTwo: UILabel!
#IBOutlet weak var fieldTwo: UITextField!
#IBOutlet weak var questionThree: UILabel!
#IBOutlet weak var fieldThree: UITextField!
#IBOutlet weak var questionFour: UILabel!
#IBOutlet weak var fieldFour: UITextField!
#IBAction func submitButton(sender: UIButton) {
fieldOneColorChange()
fieldTwoColorChange()
fieldThreeColorChange()
fieldFourColorChange()
}
#IBAction func resetButton(sender: UIButton) {
}
// Text color changes
func fieldOneColorChange() {
if fieldOne == 1 {
questionOne.textColor = UIColor.redColor()
} else {
questionOne.textColor = UIColor.blueColor()
}
}
func fieldTwoColorChange() {
if fieldTwo == 2 {
questionTwo.textColor = UIColor.brownColor()
} else {
questionTwo.textColor = UIColor.redColor()
}
}
func fieldThreeColorChange(){
if fieldThree == 3 {
questionThree.textColor = UIColor.blueColor()
} else {
questionThree.textColor = UIColor.purpleColor()
}
}
func fieldFourColorChange(){
if fieldFour == 4 {
questionFour.textColor = UIColor.blueColor()
} else {
questionFour.textColor = UIColor.redColor()
}
}
Change your if statement like this
if fieldOne.text=="1"
so on for every if statement respectively

iterate through objects/variables Swift

How can I iterate through objects/variables in Swift. Can I create an array or dictionary when I have objects attached so that I don't have to write code for each button. I'm a nubie so please speak to me like I'm four. Thanks for the help in advance.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var button1: UIButton!
#IBOutlet weak var button2: UIButton!
#IBOutlet weak var button3: UIButton!
...
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.button1.alpha = 0.0
self.button2.alpha = 0.0
self.button3.alpha = 0.0
...
}
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var button1: UIButton!
#IBOutlet weak var button2: UIButton!
#IBOutlet weak var button3: UIButton!
var arrayOfButtons = [UIButton]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
arrayOfButtons = [button1, button2, button3]
for button in arrayOfButtons {
button.alpha = 0.0
}
}
In addition to Tim's answer, you can also create buttons programmatically, and deal with them in your code!
override func viewDidLoad() {
for i in 1...10 {
let button = UIButton()
button.frame = CGRectMake((CGFloat(i-1)*50), 0, 50, 50)
button.targetForAction("buttonClick:", withSender: self)
button.tag = i
self.view.addSubview(button)
}
}
func buttonClick(sender:AnyObject) {
let tag = sender.tag!
//click logic here
}

Optional() in my text view

For one of my static labels on my main story board, it prints out "Optional("United States"). However, I would like it to print out "United States". So my question is, how do I get rid of the "Optional" part? I've already tried doing:
if let p = placemarks!.first{
self.addressLabel.text = "\(p.country)"
}
I think the exclamation mark is supposed to "unwrap" some value right? However, even if I do p = placemarks!.first, it prints out "Optional("United States").
Below is the rest of my code just in case you would like some context:
//
// ViewController.swift
// Map Demo Rob 2
//
// Created by Jae Hyun Kim on 8/17/15.
// Copyright © 2015 Jae Hyun Kim. All rights reserved.
//
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var latitudeLabel: UILabel!
#IBOutlet weak var longitudeLabel: UILabel!
#IBOutlet weak var courseLabel: UILabel!
#IBOutlet weak var speedLabel: UILabel!
#IBOutlet weak var altitudeLabel: UILabel!
#IBOutlet weak var addressLabel: UILabel!
var manager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
// 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 locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
let userLocation: CLLocation = locations[0]
self.latitudeLabel.text = "\(userLocation.coordinate.latitude)"
self.longitudeLabel.text = "\(userLocation.coordinate.longitude)"
self.courseLabel.text = "\(userLocation.course)"
self.speedLabel.text = "\(userLocation.speed)"
self.altitudeLabel.text = "\(userLocation.altitude)"
CLGeocoder().reverseGeocodeLocation(userLocation, completionHandler: {(placemarks, error) -> Void in
print(userLocation)
if error != nil {
print(error)
return
}
else {
if let p = placemarks?.first{
self.addressLabel.text = "\(p.country)"
}
}
})
}
}
In
if let p = placemarks!.first{
self.addressLabel.text = "\(p.country)"
}
p.country is an Optional<String>. You need to unwrap that aswell in order to output only it's content (if it exists).
if let country = placemarks?.first?.country {
self.addressLabel.text = country
}

Resources