How to connect outlets and actions in Swift / MacOS programaticly - swift

I have a simple example.
I connected the left button1 and label1 with ctrl-draging it to the contollerclass.
How can I do the same for the right button2 label2 programaticly (without ctrl-draging)
That´s my code:
class ViewController: NSViewController {
#IBOutlet weak var label1: NSTextField! //connected with ctrl-drag
#IBOutlet weak var button1: NSButton! //connected with ctrl-drag
#IBOutlet weak var label2: NSTextField! //not yet connected
#IBOutlet weak var button2: NSButton! //not yet connected
#IBAction func button1Pressed(_ sender: Any) //connected with ctrl-drag
{ label1.stringValue = "button-I"
button1.title = "pressed"
}
#IBAction func button2Pressed(_ sender: Any) //not yet connected
{ label2.stringValue = "button-II"
button2.title = "pressed"
}
override func viewDidLoad() {
super.viewDidLoad()
}
}

If you want to use storyboard and lay out all your elements there, you can't really avoid the ctrl drag (or just drag if you open the connections inspector on the right).
You could however create your second button programmatically in code and not use the storyboard at all for it. Then programmatically add your constraints too.
You can also add the action of the button programmatically (using addTarget) if you would like but that would require at least the IBOutlet to be setup in order to have a reference to the button.

Related

How to resize the detailsView after cross button is clicked after the closing view is hidden?

[![when the cross button is clicked, the closingView is hidden.][1]][1]
import UIKit
import IBAnimatable
class ViewController: UIViewController {
#IBOutlet weak var closingView: AnimatableView!
#IBOutlet weak var detailsView: AnimatableView!
#IBOutlet weak var btnClose: UIButton!
#IBOutlet weak var closeViewBtn: UIView!
#IBOutlet weak var scrollView: UIScrollView!
var isCloseButtonTapped = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func btnCloseAction(_ sender: Any) {
isCloseButtonTapped = !isCloseButtonTapped
if isCloseButtonTapped{
closingView.isHidden = true
}else{
//
}
}
}
[1]: https://i.stack.imgur.com/CpH2A.png
You can add closingView and detailsView inside a UIStackView. When the closingView is hidden the detailsView will fill up the entire space in the parent UIStackView.

Setting XIB inside custom cell tableView issue

I have followed this this tutorial
How to visualize reusable xibs in storyboards using IBDesignable
I have added a label inside the view to change the label and I'm calling the xib inside the custom cell for tableView. So I'm facing this issue,
fatal error: unexpectedly found nil while unwrapping an Optional value
While I’ve added this code inside the view and linked it
#IBOutlet weak var priceLabel: UILabel!
This is how I'm calling it inside the custom cell.
TableViewcell code:
class ProductListBigTableViewCell: UITableViewCell {
#IBOutlet weak var productImage: UIImageView!
#IBOutlet weak var productName: UILabel!
#IBOutlet weak var productQuantity: UILabel!
#IBOutlet weak var productAddButton: BaseButton!
#IBOutlet weak var prodcutPriceView: PriceView!
override func awakeFromNib() {
super.awakeFromNib()
productFirstName.text = "first product"
productFirstQuantity.text = "4 p"
prodcutPriceView.priceLabel.text = ""
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
I can figure that the view is not initialised yet so it is nil. I could not find a way or resource how to change a value inside XIB inside the UITableViewCell. So how I can change it and initial the view for the XIB?

Swift 3 OSX app textfield and pop button to the clipboard

I'm new with Swift, but what I would like to do basically is
now what the user selected for Q1 : Q 4,
if for example the user selected Yes at Q1, no to Q2 then I would like to add a string to the clipboard :
Q1 : Question one is there : Yes
Q2 : Question two is here : No
and last thing from the button "done"
I would like to added to the previous clipboard and copy what ever the text is on the first textfield and put all together into the clipboard
// Case Questions
#IBOutlet weak var qUestionOne: NSPopUpButton!
#IBOutlet weak var qUestionTwo: NSPopUpButton!
#IBOutlet weak var qUestionTree: NSPopUpButton!
#IBOutlet weak var qUestionFor: NSPopUpButton!
// CaseNote
#IBOutlet weak var cAseNoteTextField: NSTextField!
// Draft
#IBOutlet weak var dRaftNote: NSTextField!
// buttons
#IBAction func dOneButton(_ sender: Any) {
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}

Xcode 10 button to create mad lips story will not run, I'm new to Xcode

I'm just starting out with Xcode 10 and cannot get this button to work for my mad libs creation. this is just for fun but I would love to know how to fix it. Please look over my code to see why the "create story" button is not active.
// ViewController.swift
// Field Button Fun
//
// Created by Audrey Chao on 8/3/16.
// Copyright © 2016 Maddie Chao. All rights reserved.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var thePlace: UITextField!
#IBOutlet weak var theVerb: UITextField!
#IBOutlet weak var theNumber: UITextField!
#IBOutlet weak var theTemplate: UITextView!
#IBOutlet weak var theStory: UITextView!
#IBAction func createStory(sender: AnyObject) {
theStory.text = theTemplate.text
theStory.text = theStory.text.stringByReplacingOccurrencesOfString("<place>", withString: thePlace.text!)
theStory.text = theStory.text.stringByReplacingOccurrencesOfString("<verb>", withString: theVerb.text!)
theStory.text = theStory.text.stringByReplacingOccurrencesOfString("<number>", withString: theNumber.text!)
}
override func viewDidLoad() {
super.viewDidLoad()
//thePlace.resignFirstResponder()
// 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.
}
}
Have you wired up your button to your IBAction? If they aren't wired up, Xcode won't be able to tell what your button is supposed to do.

UITextfield value for each indexpath.row

My uitableview consists of 10 cells and each cell has 2 uitextfield's.
I need to take values from each uitextfield in each cell and add it to an array
// my custom cell
class MatchTableViewCell: UITableViewCell, UITextFieldDelegate {
#IBOutlet weak var Team2Score: UITextField!
#IBOutlet weak var Team1Score: UITextField!
#IBOutlet weak var Team2: UILabel!
#IBOutlet weak var Image2: UIImageView!
#IBOutlet weak var Image1: UIImageView!
#IBOutlet weak var Team1: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Make sure in the cell initializer for tableviewcontroller that your using your custom cell name and that you've set your identifier correctly. Then just initalize a cell and say something to the effect of
cell.Team2Score.text = "100"
you will need a global array to hold the strings. So..
var wordArray: [String] = []
you will need to add a button within the cell so when you hit the button it will add (append in the input of string (letters) to the array.
You need to add a tag to the button to know which cell it was clicked in.
button.tag = indexPath.row
#IBaction button(sender: UIButton){
var team2Words = Team2Score.text
wordArray.append(team2Words)
// do the samething for Team1Score
// you can use and if statement to check if one or the other one is empty
}
This will give you a lead