0

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>
    }
}

}

Samuel Hill
  • 176
  • 1
  • 13

1 Answers1

2

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).

Community
  • 1
  • 1
shim
  • 9,289
  • 12
  • 69
  • 108
  • Ooh, I was just about to give +1 until I read the `viewWithTag` bit. Properties are much preferred. If that isn't ideal (maybe there are a lot) then there is always a better way than using `tag`s. – Fogmeister Dec 04 '14 at 21:32
  • 1
    No need to "sheesh". I really think `tag` should actually be removed from `UIView` altogether. It promotes bad patterns. Much better now though. +1 – Fogmeister Dec 04 '14 at 21:55
  • 1
    That was a friendly-I-know-but-it's-ok-to-be-lazy-sometimes-sheesh – shim Dec 04 '14 at 21:56
  • 1
    For the record, I never use tags in cases like this, but there are situations where they are useful, such as for distinguishing alerts. – shim Dec 04 '14 at 21:59
  • Thank you for your answer! Both the parent and child view are set to User Interaction Enabled, but I still have the same issue. – Samuel Hill Dec 04 '14 at 22:08
  • Is the UIStepper enabled? (i.e. stepper.enabled) – shim Dec 04 '14 at 22:12
  • I enabled it, and nothing changed. :( – Samuel Hill Dec 04 '14 at 22:13
  • Also, I've never done enums before - do you happen to have a link? I'm not seeing much in terms of swift tutorials to use it. – Samuel Hill Dec 04 '14 at 22:17
  • https://developer.apple.com/library/ios/documentation/swift/conceptual/Swift_Programming_Language/Enumerations.html Will take another look at your main issue in a bit – shim Dec 04 '14 at 22:57
  • @SamuelHill Your code as shown works fine for me. Also works without the stepper.enabled line. The superview (userTriggers) definitely has to have User Interaction Enabled checked for it to work. – shim Dec 05 '14 at 02:48