I'm taking an iPhone app and making it available for Mac.
When I run the app on the Mac, the UIPicker displays nothing.
It places the cursor inside of the button and allows me to type, which is not the behavior I want (want to allow selecting from a list of options). Video in action.
I've tried putting Print statements in each of those functions (since removed) and they won't print so for whatever reason it seems like the functions themselves don't even run.
What approach(es) should I take to make a picker work on the Mac?
What am I screwing up?
What other information should I provide?
UI Picker (using UIKit):
// Mortgage Length Picker
#IBOutlet weak var mortgageLengthButton: UITextField!
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return mortgageLengthData.count
}
func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return mortgageLengthData[row]
}
func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
mortgageLengthButton.text = mortgageLengthData[row]
let mortgageArray = mortgageLengthData[row].components(separatedBy: " ")
mortgageLengthYears = Int(mortgageArray[0]) ?? 30
calculateMortgage()
calculateCashFlow()
}
OS 10.15.6
xCode 12.0.1
Related
I'm trying to figure out why when i switch to a different language it doesn't save my value that I selected in interface. I'm trying to figure out why selLangpickerView is not getting the right values when I select from pickview? here is a sample code below..
var langs = ["English (United States)","Chinese (China)","Chinese (Hong Kong SAR China)","Chinese (Taiwan)","English (Australia)","English (Ireland)","English (South Africa)","English (United Kingdom)"]
var selLangpickerView: String!
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return langs.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return langs[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
switch row {
case 0:
selLangpickerView = "en-US"
break
case 1:
selLangpickerView = "zh-CN"
break
case 2:
selLangpickerView = "zh-HK"
break
case 3:
selLangpickerView = "zh-TW"
break
case 4:
selLangpickerView = "en-AU"
break
case 5:
selLangpickerView = "en-IE"
break
case 6:
selLangpickerView = "en-ZA"
break
case 7:
selLangpickerView = "en-GB"
break
default:
selLangpickerView = "en-US"
}
}
I am trying to download an image from my web server, using Xcode 8 and Swift 3. It looks like this. The request returns nil. Any suggestions why?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var view1: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func load(_ sender: UIButton) {
let url = URL(string:"http://www.example.com/folder2/pic3-1.jpg")
let task = URLSession.shared.dataTask(with: url!) { data, response,
error in
guard let data = data, error == nil else { return }
DispatchQueue.main.sync() {
self.view1.image = UIImage(data: data)
}
}
task.resume()
}
#IBAction func reset(_ sender: UIButton) {
self.view1.image="blankU.png"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
Any idea why the image is returning nil from the server?
The Xcode8, Swift3 coding is okay. The server is blocked.
Hi I am brand new to swift and need some help
Every time I run my code my app crashes and spits out a " the above error
Can anyone please tell me what I need to do or what I need to replace in my code?
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{
#IBOutlet weak var picker: UIPickerView!
var employeeNames = ["John","Jane","Jack","James"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.picker.delegate = self
self.picker.dataSource = self
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return employeeNames.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return employeeNames [row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print(employeeNames[row])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Your code is absolutely fine, so the only alternative is that you haven't hooked up your pickerView outlet. Ctrl click on your picker view in the storyboard and see if your outlet 'picker' shows up. If it doesn't then ctrl drag from the pickerView to your outlet to hook it up.
I have a swift project with a custom cell from a tableview, ans inside it I have a label to display the value of a UIPicker. The UiPicker appear in the keyboard when you press the label, I wan't to be able to set default values to my UIPicker, I tried a lot of differents technics with the 2 methods :
selectRow(), without success, here is my CustomCell code :
import UIKit
class CustomCell: UITableViewCell, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet weak var answer: UITextField!
var delegate: QuestionSelectorCellDelegate?
var pickOption: [String] = []
var pickerView:UIPickerView = UIPickerView()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
#IBAction func editBeginQuestionSelectorCell(sender: UITextField) {
pickerView = UIPickerView()
pickerView.delegate = self
sender.inputView = pickerView
}
func displayBlock(block: Block){
if block.answers != nil {
pickOption = block.answers! // block.answers! = ["blue","red","green"] for example
}
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickOption.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickOption[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if row < pickOption.count {
answer.text = pickOption[row]
delegate?.updateQuestionSelectorCell(self, rep: pickOption[row])
}
}
}
Updated your code.
You didn't set the pickerView.dataSource!
If i understood your question, you want an element to be selected by default, other than element at index 0.
I also added that.
import UIKit
class CustomCell: UITableViewCell, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet weak var answer: UITextField!
var delegate: QuestionSelectorCellDelegate?
var pickOption: [String] = []
var pickerView:UIPickerView = UIPickerView()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
#IBAction func editBeginQuestionSelectorCell(sender: UITextField) {
pickerView = UIPickerView()
pickerView.delegate = self
// --- Thats what I added ---
pickerView.dataSource = self
let indexOfDefaultElement = 1 // Make sure that an element at this index exists
pickerView.selectRow(indexOfDefaultElement, inComponent: 0, animated: false)
sender.inputView = pickerView
}
func displayBlock(block: Block){
if block.answers != nil {
pickOption = block.answers! // block.answers! = ["blue","red","green"] for example
}
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickOption.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickOption[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if row < pickOption.count {
answer.text = pickOption[row]
delegate?.updateQuestionSelectorCell(self, rep: pickOption[row])
}
}
}
How do you limit the number of characters in a UITextField to 10, and if the user enters more than 10, only record the first 10 characters?
This solution deals with a user pasting text or tapping delete key too.
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let length = count(textField.text.utf16) + count(string.utf16) - range.length
return length <= 10
}
You can check the text before it gets displayed by implementing the UITextFieldDelegate.
class ViewController: UIViewController, UITextFieldDelegate {
textField(textField: UITextField!,
shouldChangeCharactersInRange range: NSRange,
replacementString string: String!) -> Bool {
var shouldChange = false
if countElements(textField.text) < 10 {
shouldChange = true
}
return shouldChange
}
}
Slight modification to the answer from #tugce:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// You can check for other things besides length here as well.
return isValidLength(textField: textField, range: range, string: string)
}
private func isValidLength(textField: UITextField, range: NSRange, string: String) -> Bool {
let length = ((textField.text ?? "").utf16).count + (string.utf16).count - range.length
return length <= 10
}
This addresses the question #Ivan asked:
what is the count method? Currently using Swift 3.
It also helps check for other conditions without crowding one method too much. For example, could do something like this to check several conditions while keeping functions a little smaller:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return isValidKey(string: string) && isValidLength(textField: textField, range: range, string: string)
}
private func isDeleteKey(string: String) -> Bool {
if let character = string.cString(using: String.Encoding.utf8) {
let isBackSpace = strcmp(character, "\\b")
if (isBackSpace == -92) {
return true
}
}
return false
}
private func isNumericKey(string: String) -> Bool {
return string.rangeOfCharacter(from: NSCharacterSet.decimalDigits) != nil
}
private func isValidLength(textField: UITextField, range: NSRange, string: String) -> Bool {
let length = ((textField.text ?? "").utf16).count + (string.utf16).count - range.length
return length <= 10
}
private func isValidKey(string: String) -> Bool {
return isDeleteKey(string: string) || isNumericKey(string: string)
}
I'll also mention that to utilize textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool you'll need to conform to the UITextFieldDelegate and set the delegate of the text field. e.g.:
class MyClass: UITextFieldDelegate {
#IBOutlet weak var textField: UITextField!
init() {
textField.delegate = self
}
}