change initial row pickerView - ios

I read here that you have to use pickerView.selectRow inside viewDidAppear(). While pickerView.selectRow is working inside func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) it does not work in viewDidAppear().
I just want to set the initial row to another row than 0
What am I missing here?
import UIKit
class FirstViewController: UIViewController, UIPickerViewDelegate {
let scrollerOfset = 100
var kiloMeters = [Int](-100...200)
#IBOutlet weak var pickerActive: UILabel!
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.
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
pickerView.selectRow(scrollerOfset, inComponent: 0, animated: false)
}
}
Xcode gives this error
Cannot invoke 'selectRow' with an argument list of type '(Int, inComponent: Int, animated: Bool)'
at line 32 (in viewDidAppear(animated: Bool))
Update
Code changed to this to make it work:
import UIKit
class FirstViewController: UIViewController, UIPickerViewDelegate {
let scrollerOfset = 100
var kiloMeters = [Int](-100...200)
#IBOutlet weak var pickerActive: UILabel!
#IBOutlet weak var picker: UIPickerView!
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.
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
picker.selectRow(scrollerOfset, inComponent: 0, animated: false)
}
}

Seems you are missing the declaration of pickerView property in your class, try declare it with under pickerActive, like
#IBOutlet weak var pickerActive: UILabel!
#IBOutlet weak var picker: UIPickerView!
Then connect it thru IB like pickerActive

Related

does not implement methodSignatureForSelector Error: Keyboard Notifications

I am trying to handle the keyboard notifications using a separate class.
Class-KeyboardHandling:
class KeyboardHandling{
class func addObserverKeyboardWillShow(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
}
#objc func keyboardWillShow(notification: NSNotification){
let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
print(duration)
}
}
MainVC:
class MainVC: UIViewController {
//MARK: Outlets
#IBOutlet weak var textInput: UITextField!
#IBOutlet weak var bottomToolbar: UIToolbar!
#IBOutlet weak var toolbarBottonConstraint: NSLayoutConstraint!
//MARK: Variables
var toolBarIntialBottomConstraint: CGFloat!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
toolBarIntialBottomConstraint = toolbarBottonConstraint.constant
KeyboardHandling.addObserverKeyboardWillShow()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The implementation works perfectly when addObserverKeyboardWillShow() is not a class function.
I get the error otherwise
Error Screenshot (Xcode 8.0):
Can someone explain why the same?

Swift TextField Issue

I am trying to print out the text of a UITextField to the console when a button is tapped. However, I am getting an error that says it cannot unwrap a nil. By default, I have a value typed into in and when I insert a new value, both produce this error. These IBOutlets are appropriately linked.
#IBOutlet weak var entranceFeeTextField: UITextField!
#IBAction func saveButtonTapped(sender: UIButton) {
print("The entrance fee is \(entranceFeeTextField.text)")
}
My class is only conforming to UITableViewCell and UITextFieldDelegate, and not to UIViewController. Could that be the error?
Do something like this
if let text = self.entranceFeeTextField.text {
print(text)
}
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var theTextField: UITextField!
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 onButtonPressed(sender: UIButton) {
print(self.theTextField.text)
}
}
http://i.stack.imgur.com/NXYaK.png
It Should Be Like
#IBOutlet weak var test: UITextField!
#IBAction func btn(sender: AnyObject) {
let txt = test.text!
print(txt)
}
Hope It Helps.

Can't assign the variable of an Int to the UILabel

I can't assign the variable of an Int to the UILabel, how can I fix this?
import UIKit
class ViewController: UIViewController {
#IBOutlet var CoinCounterInt: UILabel!
#IBOutlet weak var CoinCounter: UILabel!
var coins = 0
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 Clicker(sender: AnyObject) {
coins += 1
CoinCounterInt = "\(coins)"
}
}
You need to assign the the string to the text property of the label:
CoinCounterInt.text = "\(coins)"

Swift tableview in popover doesn't show data

I am trying to setup a popover view containing a textfield and a tableview, but I couldn't make the tableview to show the data. It would be much appreciated if you could help me on this.
On the main ViewController, I put a label to trigger the segue of popover,
import UIKit
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
#IBOutlet weak var textField: UITextField!
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 popover(sender: AnyObject) {
self.performSegueWithIdentifier("ShowDetails", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowDetails" {
if let controller = segue.destinationViewController as? UIViewController {
controller.popoverPresentationController!.delegate = self
controller.preferredContentSize = CGSize(width: 320, height: 50)
}
}
}
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return .None
}
}
A PopoverCell is setup for the prototype cell,
import UIKit
class PopoverCellTableViewCell: UITableViewCell {
#IBOutlet weak var AreaCellLabel: 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
}
}
A PopoverControllerView is set for the popover itself.
import UIKit
class PopoverViewController: UIViewController, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate {
var areas = [Area]()
// #IBOutlet weak var NewArea: UITextField!
// #IBOutlet weak var SaveNewArea: UIButton!
#IBOutlet weak var subtableView: UITableView!
override func viewDidLoad() {
subtableView.dataSource = self
subtableView.delegate = self
super.viewDidLoad()
LoadsampleArea()
// Do any additional setup after loading the view.
}
func LoadsampleArea () {
let area1 = Area(AreaName:"Moountain")!
let area2 = Area(AreaName:"ByHill")!
let area3 = Area(AreaName:"Yard")!
areas += [area1, area2, area3]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return areas.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "AreaCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! PopoverCellTableViewCell
let area = areas[indexPath.row]
cell.AreaCellLabel.text = area.AreaName
dismissViewControllerAnimated(true, completion:nil)
return cell
}
}
and a simple data file to put the data.
import UIKit
class Area {
var AreaName: String
init? (AreaName: String) {
self.AreaName = AreaName
if AreaName.isEmpty {
return nil
}
}
}
Where is your UITableView object in your PopoverViewController class ? I can't see any reference to it.
Maybe your didn't copy-paste it since the textfield is commented too, in this case I'll suggest to check if the delegate an datasource are set property.

Sending textbox values to UILabels

import UIKit
var typingSpace = 80
class ViewController: UIViewController {
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.
}
#IBOutlet weak var typingSpace: UIView!
#IBOutlet weak var displayLabel: UILabel!
}
Basically what I'm doing is this: when I type a certain number (let's say 70) in a text box, the label will display something (eg. Your number is 70). I honestly have no idea what I'm doing. Please help.
You need 4 things
Add a outlet for your text view so you'll have #IBOutlet weak var textView: UITExtView!
Make ViewController implement UITextViewDelegate
ViewController: UIViewController, UITextViewDelegate
Make self delegate of textView on viewDidLoad:
textView.delegate = self
Implement textViewDidChange and put your logic in there
func textViewDidChange(textView: UITextView) {
// change your label text
}
Here you have be a complete class example
class ViewController: UIViewController, UITextViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.textView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textViewDidChange(textView: UITextView) {
displayLabel.text = "your text"
}
#IBOutlet weak var textView: UITextView!
#IBOutlet weak var typingSpace: UIView!
#IBOutlet weak var displayLabel: UILabel!
}
Note: #Claudio Redi has supplied the correct answer, this is another way to populate a label from a UITextField.
This is one way of accomplishing your task:
class ViewController: UIViewController {
#IBOutlet weak var typingSpace: UITextField!
#IBOutlet weak var displayLabel: UILabel!
#IBAction func handleButtonPressed(sender: UIButton) {
displayLabel.text = typingSpace.text
}
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.
}
}

Resources