在Swift中的一个ViewController中的视图之间传递数据 [英] Passing data between views in ONE ViewController in Swift

查看:81
本文介绍了在Swift中的一个ViewController中的视图之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所做的所有搜索都专注于在视图控制器之间传递数据。那不是我想要做的。我有一个ViewController,里面有多个Views。 ViewController有一个工作正常的滑块:

All of the searches I've done focus on passing data between view controllers. That's not really what I'm trying to do. I have a ViewController that has multiple Views in it. The ViewController has a slider which works fine:

var throttleSetting = Float()
@IBAction func changeThrottleSetting(sender: UISlider)
{
    throttleSetting = sender.value   
}

然后,在同一个ViewController中包含的一个视图中,我有一个基本行(现在)设置一个初始值,稍后在代码的DrawRect部分中使用:

Then, in one of the Views contained in that same ViewController, I have a basic line that (for now) sets an initial value which is used later in the DrawRect portion of the code:

var RPMPointerAngle: CGFloat {
    var angle: CGFloat = 2.0
    return angle
}

我想要做的是将ViewController中的滑块值传递给ViewController中包含的View,以允许drawRect是动态的。

What I want to do is have the slider's value from the ViewController be passed to the View contained in the ViewController to allow the drawRect to be dynamic.

感谢您的帮助!

推荐答案

编辑:对不起,当我创建这个答案我考虑到了ViewControllers。一种更简单的方法是在SomeView中创建一个方法并直接与它对话。

Sorry, when I created this answer I was having ViewControllers in mind. A much easier way would be to create a method in SomeView and talk directly to it.

示例:

class MainViewController: UIViewController {

    var view1: SomeView!
    var view2: SomeView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Create the views here
        view1 = SomeView()
        view2 = SomeView()

        view.addSubview(view1)
        view.addSubview(view2)
    }

    @IBAction func someAction(sender: UIButton) {
        view1.changeString("blabla")
    }
}

class SomeView: UIView {

    var someString: String?

    func changeString(someText: String) {
        someString = someText
    }
}

委托:

首先创建协议:

protocol NameOfDelegate: class {     // ": class" isn't mandatory, but it is when you want to set the delegate property to weak
    func someFunction()              // this function has to be implemented in your MainViewController so it can access the properties and other methods in there
}

在你的视图中你必须添加:

In your Views you have to add:

class SomeView: UIView, NameOfDelegate {

    // your code

    func someFunction() {

        // change your slider settings
    }
}

最后一步,你必须添加委托的属性,这样你就可以对话了。我个人认为这个属性是某种门,在两个类之间,所以他们可以互相交谈。

And the last step, you'll have to add a property of the delegate, so you can "talk" to it. Personally I imagine this property to be a gate of some sort, between the two classes so they can talk to each other.

class MainViewController: UIViewController {

    weak var delegate: NameOfDelegate?

    @IBAction func button(sender: UIButton) {
        if delegate != nil {
            let someString = delegate.someFunction()
        }
    }
}

我在这里使用了一个按钮来展示你如何使用委托。只需用滑块替换它就可以更改视图的属性

I used a button here just to show how you could use the delegate. Just replace it with your slider to change the properties of your Views

编辑:我忘了提到的一件事是,你需要将SomeView指定为委托。但就像我说的那样,我不知道你是如何创造意见等所以我无法帮助你。

One thing I forgot to mention is, you'll somehow need to assign SomeView as the delegate. But like I said, I don't know how you're creating the views etc so I can't help you with that.

这篇关于在Swift中的一个ViewController中的视图之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆