当前位置: 首页 > 面试题库 >

当UITextField满或为空时显示警报

冯阳华
2023-03-14
问题内容

以下是其中的代码,如果userNameTF或passwordTF已满或为空,则会显示警报。

@IBAction func LoginBtn(sender: AnyObject) {
    let userName = userNameTF.text
    let password = passwordTF.text

    if ((userName?.isEmpty) != nil) {

        displayMyAlertMessage ("Forget to fill your user name")
        return        
    }

    if ((password?.isEmpty) != nil){

        displayMyAlertMessage ("Forget to fill your password")
        return    
    }        
}

func displayMyAlertMessage(userMessage:String){

    let myAlert = UIAlertController(title: "WOF", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert)  
    let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
    myAlert.addAction(okAction)
    self.presentViewController(myAlert, animated: true, completion: nil)
 }

问题答案:

这是一个很好的例子 guard

@IBAction func LoginBtn(sender: AnyObject) {

   guard let userName = userNameTF.text where !userName.isEmpty else {
        displayMyAlertMessage ("Forget to fill your user name")
        return
   }

   guard let password = passwordTF.text where !password.isEmpty else {
        displayMyAlertMessage ("Forget to fill your password")
        return
   }

    // do something with userName and password
}


 类似资料:
  • 我已经在NSNotificationCenter中注册了两个观察员,keyboardWillShow:和keyboardWillHide: 在这两种方法中,当我尝试设置包含UITextView的UIView的动画时,它会跳到我尝试设置动画的帧,然后再设置回原始位置的动画。我在touchesbearth方法中尝试了相同的动画代码来测试它,它在那里工作得很好(设置新帧的动画,而不是跳回原始位置)。 我

  • 刚刚遇到javafx.scene.chart.LineChart的问题。当使用高于5E13的双倍值填充图表数据时,该系列碰巧会消失(参见屏幕截图)。 以防万一:我正在用folowing代码添加数据 它似乎是y轴上界属性设置为NaN

  • 我正在尝试使用ggplot、geom_line和facet_wrap创建多个线图。图表已成功创建并显示,但同时显示以下消息: 我已经尝试过重新启动我的会话,重新启动R,通过谷歌搜索答案,在堆栈溢出上搜索这里(包括以下问题:ggplot2折线图给出了“geom_path:每个组仅由一个观察组成。需要调整组美学吗?”)却找不到任何有帮助的东西。 添加: ...并不能消除警告信息。 我使用的RStudi

  • 应用程序运行良好...只是数据没有出现...我已经添加了sha用户电子邮件显示在登录后,在登录活动中我已经添加了 但是在实时数据库数据不显示以防万一…我也等了半个小时,尝试了所有的东西…网络也不错。数据库中的数据库规则的数据库图片 mainActivity.java//不能用于单个子级,即firebaseDatabase.getInstance().getReference().child(“aj

  • 我有一个应用程序,它有一个列表,当它为空时,它显示“空”,但我想显示任何东西,而不是它,但我不能。我试着做了一个验证,但由于某种原因它仍然显示为null。 代码如下: 但以下是它的表现: 我做错了什么?

  • 我正在Intellij12.1中开发android软件。我可以访问R类中的所有内容,项目运行正常。但是当我在编辑器中打开R类时,R类显示为空。所以我的问题是;为什么R类出现空的?