简体   繁体   English

在已加载子视图(UIView)的UIViewController中单击按钮

[英]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: 我试过了:

  • Setting the target in the UIViewController instead of the UIView 在UIViewController中而不是UIView中设置目标
  • Keeping the function in the UIView 将该功能保留在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: 这是我将其加载到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.

相关问题 使用Storyboard将UIView子视图作为UIViewController的默认UIView - Make UIView subview as default UIView for UIViewController with Storyboard 如何从 UITableViewCell 按钮创建 UIView 单击单独的 UIViewController? - How to create a UIView from a UITableViewCell button click on a separate UIViewController? 很难将UIView与作为子视图添加到UIViewController - Hard time adding UIView with to a UIViewController as a subview 在UIView中作为子视图调用UIViewController视图时出现问题 - Issue in calling UIViewController view as subview in UIView UIImageView作为我的UIViewController类中UIView的子视图 - UIImageView as a SubView of UIView in my UIViewController Class 另一个UIView的UIView子视图,它是UIViewController的子视图,未在iOS中集中到UIViewController - UIView subview of another UIView which is subview of UIViewController, not getting centered to UIViewController in iOS UIView旋转和Subview(按钮)触摸 - UIView rotation and Subview (Button) touch 如何在加载了nib文件的UIView中调整子视图的大小 - How to resize subview in UIView that loaded with nib file 没有子类化UIView或UIViewController:是否可以捕获是否添加了子视图? - Without subclassing a UIView or UIViewController: possible to catch if a subview was added? 如何通过同一子视图中的一个按钮关闭子视图,该子视图是UIViewController - how to dismiss a subview by a button inside the same subview which is a UIViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM