简体   繁体   中英

Click button in a UIViewController that was loaded a subView (UIView)

I'm trying to add a UIView subview into a UIViewController, and that UIView has a UISwitch that I want the user to be able to toggle. Based on the state, a UITextField's value will toggle back and forth. Here is the subview (InitialView):

import UIKit

class InitialView: UIView {

// All UI elements.
var yourZipCodeSwitch: UISwitch = UISwitch(frame: CGRectMake(UIScreen.mainScreen().bounds.width/2 + 90, UIScreen.mainScreen().bounds.height/2-115, 0, 0))

override func didMoveToSuperview() {
    self.backgroundColor = UIColor.whiteColor()

    yourZipCodeSwitch.setOn(true, animated: true)
    yourZipCodeSwitch.addTarget(ViewController(), action: "yourZipCodeSwitchPressed:", forControlEvents: UIControlEvents.TouchUpInside)
    self.addSubview(yourZipCodeSwitch)
}

}

If I want to have it's target properly pointing at the below function, where should I either set the target or include this function? I tried:

  • Setting the target in the UIViewController instead of the UIView
  • Keeping the function in the UIView

Here's the function:

// Enable/disable "Current Location" feature for Your Location.
func yourZipCodeSwitchPressed(sender: AnyObject) {
    if yourZipCodeSwitch.on
    {
        yourTemp = yourZipCode.text
        yourZipCode.text = "Current Location"
        yourZipCode.enabled = false
    }
    else
    {
        yourZipCode.text = yourTemp
        yourZipCode.enabled = true
    }
}

And here is where I'm loading it into the UIViewController:

// add initial view
var initView : InitialView = InitialView()

// Execute on view load
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    view.addSubview(initView)        
}

Any help is much appreciated - thanks!

Yeah, the didMoveToSuperView() placement doesn't make much sense. So you're creating a random, totally unconnected ViewController instance to make the compiler happy but your project sad. Control code goes in controllers, view code goes in views.

You need in your real ViewController :

override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(initView)
    // Note 'self' is the UIViewController here, so we got the scoping right
    initView.yourZipCodeSwitch.addTarget(self, action: "yourZipCodeSwitchPressed:", forControlEvents: .ValueChanged)        
}

Also, .TouchUpInside is for UIButton s. Toggle switches are much more complicated, so their events are different. Touching up inside on a toggle switch's current setting can and should do nothing, whereas touchup inside on the opposite setting triggers the control event above. iOS does all the internal hit detection for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM