UISlider值改变不触发

时间:2017-03-25 08:32:37

标签: ios swift uikit uislider

我在这样的类中有一个UISlider对象:

public function vote($id, $vote)
{
    //TODO: refactor this..
    $username = Session::get('username');
    $post = Post::find($id);

    if( empty($post) ){
        abort(404, 'Cannot find post');
    }

    $uservote = PostVotes::where([
            'id' => $post->id,
            'username' => $username,
        ])
        ->first();

    // replace $uservote->count() with !empty($userwote), because you cannot call count() nethod from null
    if (!empty($uservote)) {
        $votetype = $uservote->vote;
        if ($votetype === $vote) {
            $uservote->delete();
        } else {
            Vote::updateOrCreate(
                ['vote' => $votetype], ['vote' => $vote]
            );
        }
    } else {
        $uservote = new PostVotes();
        $uservote->vote = $vote;
        $uservote->username = $username;
        $uservote->id = $post->id; // <!-- probably replace `id` field name on `post_id`
        $uservote->save();
    }
}

一切都很好。我可以看到我的View Controller上的滑块并与之交互,但是当我擦洗滑块时,class SliderObject { // Other properties @IBOutlet weak var slider: UISlider! @IBAction func valChange(_ sender: Any) { // Not called } func getView() -> UIView { // Return a UIView with the slider in it - the slider is in a nib return Bundle.main.loadNibNamed("SliderObjectNib", owner: self, options: nil)?.first as! UIView } } // In some other View Controller var s: SliderObject? // <---- This is the solution.. Keep a strong reference to the variable so it won't get released after `viewDidLoad` returns func viewDidLoad() { s = SliderObject() self.view.addSubView(s.getView()) } 函数永远不会被调用...有什么想法吗?

2 个答案:

答案 0 :(得分:3)

您正在调用从一个类到另一个类的方法。当该方法退出时,将在局部变量中创建此引用。它试图在无效对象上调用该函数。 使用委托 - 为方法定义协议,并让视图控制器实现该协议。然后,您可以将视图控制器传递给方法。

答案 1 :(得分:1)

添加以下代码: -

// As the slider moves it will continuously call the -valChange:
slider.continuous = true; // false makes it call only once you let go
slider.addTarget(self, action: "valChange:", forControlEvents: .ValueChanged)