jjzjj

swift - 在 Swift 中制定隐私政策 : How to make UIAlertController action button disabled until message is scrolled to the bottom

coder 2023-09-17 原文

在我的 iOS 应用程序中,我尝试使用 UIAlertController 实现一个简单的隐私策略。根据法律,该政策在被接受之前必须是可滚动的——就像当今大多数隐私政策一样。

根据我自己的研究,我发现您可以禁用和启用 UIAlertAction 按钮,但我不知道如何识别 UIAlertController 消息正文何时滚动。一直滚动到底部可能是一项要求,我有兴趣找出一种同样可行的方法。

这是我当前为上面的默认外观 UIAlertController 编写的代码。

let alertController = UIAlertController(title: "Privacy Policy", message: privacyPolicyString, preferredStyle: UIAlertControllerStyle.Alert)

let AcceptAction = UIAlertAction(title: "Accept", style: UIAlertActionStyle.Default, handler: {(action: UIAlertAction) -> Void in

    //perform next step in login verification
})

let DeclineAction = UIAlertAction(title: "Decline", style: UIAlertActionStyle.Default, handler: {(action: UIAlertAction) -> Void in

    //User has declined privacy policy. The view resets to standard login state
})

alertController.addAction(AcceptAction)
alertController.addAction(DeclineAction)

self.presentViewController(alertController, animated: true, completion: nil)

最佳答案

这可以通过原生 UIAlertController 实现,如下所示:-

  1. 创建类型为UIAlertAction的变量
  2. enabled设置为false
  3. 当你滚动到底部时,你可以在 ScrollView 委托(delegate)或任何自定义方式上检查它,然后将 enabled 设置为 true
  4. 下面代码中最重要的是:-

    self.actionToEnable = action
    action.enabled = false
    

代码引用:-

weak var actionToEnable : UIAlertAction?

func callbackWhenScrollToBottom(sender:UIScrollView) {
    self.actionToEnable?.enabled = true
}

       let alert = UIAlertController(title: "Title", message: "Long text here", preferredStyle: UIAlertControllerStyle.Alert)
        let cancel = UIAlertAction(title: "Accept", style: UIAlertActionStyle.Cancel, handler: { (_) -> Void in

        })

        let action = UIAlertAction(title: "Decline", style: UIAlertActionStyle.Default, handler: { (_) -> Void in

        })

        alert.addAction(cancel)
        alert.addAction(action)

        self.actionToEnable = action
        action.enabled = false
        self.presentViewController(alert, animated: true, completion: nil)

关于swift - 在 Swift 中制定隐私政策 : How to make UIAlertController action button disabled until message is scrolled to the bottom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38849118/

有关swift - 在 Swift 中制定隐私政策 : How to make UIAlertController action button disabled until message is scrolled to the bottom的更多相关文章

随机推荐