菜鸟教程小白 发表于 2022-12-11 17:03:52

ios - 快速代码返回警报类型字符串不起作用


                                            <p><p>我发现 void 返回警报,但未找到字符串警报返回。
尝试将 Void 转换为字符串 </p>

<pre><code>// return Void Alert
func returnAlert(title: String!, message: String! ,success: (() -&gt; Void)? , cancel: (() -&gt; Void)?) {
    dispatch_async(dispatch_get_main_queue(), {
      let alertController = UIAlertController(title:title,
            message: &#34;&#34;,
            preferredStyle: UIAlertControllerStyle.Alert)

      self.newQtyField = UITextField()
      self.newQtyField.keyboardType = .NumberPad

      func addTextField(textField: UITextField!){
            // add the text field and make the result global
            let row = self.array
            textField.text = String(row.pbQty!)
            self.newQtyField = textField
      }


      let cancelLocalized = NSLocalizedString(&#34;cancel&#34;, tableName: &#34;activity&#34;, comment:&#34;&#34;)
      let okLocalized = NSLocalizedString(&#34;ok&#34;, tableName: &#34;Localizable&#34;, comment:&#34;&#34;)

      let cancelAction: UIAlertAction = UIAlertAction(title: cancelLocalized,
      style: .Cancel) {
            action -&gt; Void in cancel?()
      }
      let successAction: UIAlertAction = UIAlertAction(title: okLocalized,
      style: .Default) {
            action -&gt; Void in success?()
      }
      alertController.addTextFieldWithConfigurationHandler(addTextField)
      alertController.addAction(cancelAction)
      alertController.addAction(successAction)


      self.presentViewController(alertController, animated: true, completion: nil)
    })
}
</code></pre>

<p>//底部调用乐趣 |我认为成功:{ () to success: { textfield , but not working how...需要你的帮助</p>

<pre><code>    returnAlert(&#34;title&#34;, message: &#34;msg&#34;, success: { () -&gt; Void in


      })
    { () -&gt; Void in
      print(&#34;user canceled&#34;)
    }
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>很难理解您在问题中所说的内容。</p>

<p>你应该把方法定义改成这样:</p>

<pre><code>func returnAlert(title: String!, message: String! ,success: ((text:String) -&gt; Void)? , cancel: (() -&gt; Void)?) {
</code></pre>

<p>并更改 <code>successAction</code> 以返回 UITextField 的值:</p>

<pre><code>let successAction: UIAlertAction = UIAlertAction(title: okLocalized,
            style: .Default) { action in
                success?(text: self.newQtyField.text!)
            }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 快速代码返回警报类型字符串不起作用,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38517119/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38517119/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 快速代码返回警报类型字符串不起作用