【问题标题】:UIButton is taking 3 taps before working? SwiftUIButton 在工作前需要点击 3 次?迅速
【发布时间】:2016-03-11 04:24:07
【问题描述】:

我有一个如图所示的按钮,当用户首先按下它时,它应该会更改图像并且用户现在正在关注此配置文件。但是,当用户第一次按下按钮时,它什么也不做,第二次点击它就可以工作。如何在第一次点击时完成这项工作?

编辑清理后的代码此代码需要点击两次按钮才能工作。双击它会跟随或取消关注用户并更新图像。但就像我说的那样,它需要点击两次。

 @IBAction func favButton(sender: UIButton) {

    buttonPressed = !buttonPressed


            if buttonPressed == true {

                let followedObjectId = userToShowDetail?["name"] as? String

                if isFollowing[followedObjectId!] == true {


                }

            } else  {

                let followedObjectId = userToShowDetail?["name"] as? String

                if isFollowing[followedObjectId!] == false {

                    isFollowing[followedObjectId!] = true
                    sender.setImage(UIImage(named: "fave_on@2x.png"), forState: UIControlState.Normal)


                    print("follow")

                    let following = PFObject(className: "Followers")
                    following["following"] = userToShowDetail?["name"] as? String
                    following["follower"] = PFUser.currentUser()?.objectId
                    favLabel.text = "Favourite Jammer"

                    following.saveInBackground()

                } else {

                    if buttonPressed != true {

                        isFollowing[followedObjectId!] = false
                        print("notFollow")
                        sender.setImage(UIImage(named: "fave_off@2x.png"), forState: UIControlState.Normal)
                        favLabel.text = "Add to favourites?"

                    }

                }
            }

此代码在视觉上修复了它,但它不再在幕后工作。 按钮更改图像并打印关注和取消关注日志,但它不会取消关注用户并添加关注用户访问我的数据两次。

        buttonPressed = !buttonPressed

    if buttonPressed == true {

        let followedObjectId = userToShowDetail?["name"] as? String

        isFollowing[followedObjectId!] = true

        sender.setImage(UIImage(named: "fave_on@2x.png"), forState: UIControlState.Normal)
        print("follow")

        let following = PFObject(className: "Followers")
        following["following"] = userToShowDetail?["name"] as? String
        following["follower"] = PFUser.currentUser()?.objectId
        favLabel.text = "Favourite Jammer"

        following.saveInBackground()

    } else {

        if buttonPressed != true {
            let followedObjectId = userToShowDetail?["name"] as? String

            isFollowing[followedObjectId!] = false
            print("notFollow")
            sender.setImage(UIImage(named: "fave_off@2x.png"), forState: UIControlState.Normal)
            favLabel.text = "Add to favourites?"

        } // !=true
    } // else

} // uibutton

【问题讨论】:

  • 代码有点乱... 初始查询应该在ViewDidLoad 中,以便在按下按钮之前值就在那里。然后按下按钮时的其余逻辑也需要在一个单独的函数中并分解为多个部分。该按钮发生了太多事情。
  • 当您将按钮链接到您的类时(ctrl + 从界面构建器拖动到视图控制器),您是否从按钮内部进行了润色(ctrl + 单击按钮并告诉我是否连接在里面
  • Kholl ya 它在里面修饰。 @likeslvi 好的,我试着清理一下她,看看是否会有所改善。
  • 我正在写一个答案来帮助您改进代码。但是图像更改应该发生在按钮内,而其他一切(查询、更改数据等)需要在函数内和 viewDidLoad 内发生。一旦这样,您就可以轻松更改图像。
  • 好的,非常感谢@lukeslvi

标签: ios swift button uibutton swift2


【解决方案1】:

代码非常混乱,我不建议这样处理......正在发生的查询太多,并且该按钮中的一般情况太多......

但是,任何查询(您要在应用程序中访问和修改的数据)都应该在 viewDidLoad(或类似方法)中,以便在按下按钮之前值就在那里。这些值应存储在数组中。当按下按钮时,您将已经可以访问这些值,并且将来以不同的方法修改它们会更容易。

这需要缩短。

但是,假设您仍然想这样做,按钮逻辑需要分解为功能。

从该查询中创建一个函数,然后在按钮中引用它。发生了太多事情 + 你在错误的地方调用了按钮。

这是一个非常粗略的例子:

您的按钮应该看起来像 这样:

  @IBAction func favButton(sender: UIButton) {

    buttonPressed = !buttonPressed

    if buttonPressed == true {

        let followedObjectId = userToShowDetail?["name"] as? String

        if isFollowing[followedObjectId!] == true {

            //something needs to happen here?

        }

        updateImage()

    } else  {


      followersUpdate()

    }

   }


      //Your functions below the button: 

 func followersUpdate() {

    let followedObjectId = userToShowDetail?["name"] as? String

    if isFollowing[followedObjectId!] == false {

    isFollowing[followedObjectId!] = true
    sender.setImage(UIImage(named: "fave_on@2x.png"), forState: UIControlState.Normal)

    print("follow")

    let following = PFObject(className: "Followers")
    following["following"] = userToShowDetail?["name"] as? String
    following["follower"] = PFUser.currentUser()?.objectId
    favLabel.text = "Favourite Jammer"

    following.saveInBackground()

    } else {

    }

}

func updateImage() {
        if buttonPressed != true {

            isFollowing[followedObjectId!] = false
            print("notFollow")
            sender.setImage(UIImage(named: "fave_off@2x.png"), forState: UIControlState.Normal)
            favLabel.text = "Add to favourites?"


            let query = PFQuery(className: "Followers")

            query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)
            query.whereKey("following", equalTo: (userToShowDetail?["name"] as? String)!)

            query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

                if let objects = objects {

                    for object in objects {

                        object.deleteInBackground()

                    }
                }

            })

        }

    }

    //Your viewDidLoad() initial query to parse:  


 override func viewDidLoad() {
    super.viewDidLoad()

    let query = PFUser.query()

    query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

        if let users = objects {

            for object in users {

                if let user = object as? PFUser {

                    if user.objectId! != PFUser.currentUser()?.objectId {

                        self.usernames.append(user.username!)
                        self.userids.append(user.objectId!)

                        let query = PFQuery(className: "Followers")

                        query.whereKey("follower", equalTo: (PFUser.currentUser()!.objectId)!)
                        query.whereKey("following", equalTo: user.objectId!)

                        query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

                            if let objects = objects {

                                if objects.count > 0 {

                                    self.isFollowing[user.objectId!] = true

                                } else {

                                    self.isFollowing[user.objectId!] = false

                                }
                            }

                            if self.isFollowing.count == self.usernames.count {

                            }
                        })
                    }
                }

            }
        }
    })
}

正如我所提到的,这是一个粗略的示例,可以让您了解应该做什么(即使我认为查询和服务器来回发送是 1)多余的,2)不需要,3)会你的应用程序变慢了。)

如果您像这样分解它并且按钮中的内容较少,则图像应该更新。您将不得不解决这个问题并修改代码/使其更短。

如果您有更多问题,请告诉我,我们很乐意为您提供帮助。

【讨论】:

  • 非常感谢您的帮助。现在要去玩了,我会回复你的。
  • 当然可以。告诉我!
  • 感谢您的帮助我修复了它我调用了两次如果它是正确的,一旦我在我编辑的版本中添加了我的新代码,就调用了错误。
  • 没问题,很高兴它有帮助。今后,尽量让您的代码尽可能简单。
猜你喜欢
  • 1970-01-01
  • 2011-03-15
  • 2020-12-07
  • 2016-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-04
相关资源
最近更新 更多