When I click run app, the app doesn't place images when I click on the designated buttons. (I am relatively new to Swift and Xcode so bear with me)
I am going to remove the UIImageViews and use buttons instead. I'll let you know if I need more help. Thank you.
class ViewController: UIViewController {
#IBOutlet var TicTacImg1: UIImageView!
#IBOutlet var TicTacImg2: UIImageView!
#IBOutlet var TicTacImg3: UIImageView!
#IBOutlet var TicTacImg4: UIImageView!
#IBOutlet var TicTacImg5: UIImageView!
#IBOutlet var TicTacImg6: UIImageView!
#IBOutlet var TicTacImg7: UIImageView!
#IBOutlet var TicTacImg8: UIImageView!
#IBOutlet var TicTacImg9: UIImageView!
#IBOutlet var TicTacButton1: UIButton!
#IBOutlet var TicTacButton2: UIButton!
#IBOutlet var TicTacButton3: UIButton!
#IBOutlet var TicTacButton4: UIButton!
#IBOutlet var TicTacButton5: UIButton!
#IBOutlet var TicTacButton6: UIButton!
#IBOutlet var TicTacButton7: UIButton!
#IBOutlet var TicTacButton8: UIButton!
#IBOutlet var TicTacButton9: UIButton!
#IBOutlet var ResetButton: UIButton!
#IBOutlet var UserMessage: UILabel!
var plays = Dictionary <Int, Int> ()
#IBAction func UIButtonClicked(sender:UIButton) {
UserMessage.hidden = true
if plays[sender.tag] == nil && !aiDeciding && !done {
setImageForSpot(sender.tag, player:1)
}
}
func setImageForSpot (spot:Int, player:Int) {
var playerMark = player == 1 ? "x" : "o"
plays[spot] = player
switch spot {
case 1:
TicTacImg1.image = UIImage(named: playerMark)
case 2:
TicTacImg2.image = UIImage(named: playerMark)
case 3:
TicTacImg3.image = UIImage(named: playerMark)
case 4:
TicTacImg4.image = UIImage(named: playerMark)
case 5:
TicTacImg5.image = UIImage(named: playerMark)
case 6:
TicTacImg6.image = UIImage(named: playerMark)
case 7:
TicTacImg7.image = UIImage(named: playerMark)
case 8:
TicTacImg8.image = UIImage(named: playerMark)
case 9:
TicTacImg9.image = UIImage(named: playerMark)
default:
TicTacImg5.image = UIImage(named: playerMark)
}
}
Your UIButtonClicked function needs to look like this...
#IBAction func UIButtonClicked(sender:UIButton) {
ticTacLabel.hidden = true
if plays[sender.tag] == nil && !aiDeciding && !done {
setImageForSpot(sender.tag, player:1)
}
Hope this helps!
Related
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)
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
Scenario: processing a button action originating from a UICollectionViewCell in its hosting UIViewController.
The Cell:
Here's my code:
protocol CellProtocol {
func doSomething()
}
...
class ItemCollectionCell:UICollectionViewCell {
var delegate:CellProtocol?
var itemData:ItemData?
#IBOutlet weak var descLabel: UILabel!
#IBOutlet weak var priceLabel: UILabel!
#IBOutlet weak var imageView: UIImageView!
#IBAction func addToCartAction() {
delegate?.doSomething()
print("Add to Cart")
}
}
class ShopViewController:UIViewController, CellProtocol {
let kCellID = "ItemCell"
#IBOutlet weak var featuredItemLabel: UILabel!
#IBOutlet weak var featuredItemPriceLabel: UILabel!
#IBOutlet weak var featuredItemImageView: UIImageView!
#IBOutlet weak var collectionView: UICollectionView!
override var prefersStatusBarHidden: Bool {
return true
}
func doSomething() {
print("Howdy Batman!")
}
// -----------------------------------------------------------------------------------
// MARK: - Action Methods
#IBAction func addToCartAction(_ sender: UIButton) {
}
}
This isn't working.
I know it has something to do with delegation.
I merely sent 'self' to my custom UICollectionViewCell class during the cell-loading phase.
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
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
}