Can't interact with UIStepper

818 Views Asked by At

I'm adding a few UISteppers and UITextFields programmatically, based on user's preferences. However, I can't seem to get the UIStepper touch to click - it looks like it's not registering the touch on the -/+ buttons. I tried setting User Interaction of the userTriggers UIView to disabled, but that didn't work.

1) What else can I try?

2) I also need to be able to increment the corresponding UITextField, and then write that into my UserData.plist file, and I'm not sure how to access each field. Should I add them to an array of some sort?

import UIKit

class TriggersViewController: UIViewController{

@IBOutlet var userTriggers:UIView!
@IBOutlet var saveButton:UIButton!

var triggersList:Array<String>!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    loadTriggers()
    var prevInput = 250;
    for index in 0..<triggersList.count{

        //label for trigger
        var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
        label.center = CGPointMake(120, CGFloat(prevInput))
        label.textAlignment = NSTextAlignment.Left
        label.text = triggersList[index]
        userTriggers.addSubview(label)

        //input box for trigger
        var input = UITextField(frame: CGRectMake(0, 0, 50, 21))
        input.center = CGPointMake(250, CGFloat(prevInput))
        input.text = "0";
        //add input to triggersView
        userTriggers.addSubview(input);

        //UIStepper
        var stepper = UIStepper(frame: CGRectMake(0, 0, 50, 21))
        stepper.center = CGPointMake(300, CGFloat(prevInput))
        stepper.addTarget(self, action: "stepperValueChanged:", forControlEvents: .ValueChanged)
        stepper.enabled = true
        //add stepper to triggersView
        userTriggers.addSubview(stepper);


        prevInput += 50 //increment for height

    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func stepperValueChanged(sender:UIStepper!){
    println("It Works, Value is --&gt;\(Int(sender.value).description)")
}

func loadTriggers(){
    var myDict: NSDictionary?

    if let path = NSBundle.mainBundle().pathForResource("UserConfigs", ofType: "plist") {
        myDict = NSDictionary(contentsOfFile: path)
    }
    if let dict = myDict {
        triggersList = dict["Triggers"] as Array<String>
    }
}

}

1

There are 1 best solutions below

10
On

You actually need to set user interaction on the superview to enabled. See here for more info: UIView -- "user interaction enabled" false on parent but true on child?

Also, re: second question an easy (but not so ideal) way to access each field is using tags. Clean-ish way to do that is define tags using an enum. Then access the fields using viewWithTag.

However, there are better ways than tags (e.g. they're not very error-proof because any view can be given any tag). If it's only a few text fields / steppers though you could just as easily add properties to your view controller for each one. The best solution would be to group the steppers and fields together in a UIView subclass (assuming they're next to each other) and store references to those groupings in your view controller (possibly in an array).