[英]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. 我正在尝试将UIView子视图添加到UIViewController中,并且该UIView具有希望用户能够切换的UISwitch。 Based on the state, a UITextField's value will toggle back and forth.
根据状态,UITextField的值将来回切换。 Here is the subview (InitialView):
这是子视图(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:
我试过了:
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: 这是我将其加载到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. 是的,
didMoveToSuperView()
位置没有多大意义。 So you're creating a random, totally unconnected ViewController
instance to make the compiler happy but your project sad. 因此,您要创建一个随机的,完全不连接的
ViewController
实例,以使编译器满意,但使您的项目感到遗憾。 Control code goes in controllers, view code goes in views. 控制代码进入控制器,视图代码进入视图。
You need in your real ViewController
: 您需要在真正的
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. 另外,.TouchUpInside用于
UIButton
。 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.
iOS为您执行所有内部匹配检测。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.