ios - 如何使用 if 条件执行正确的代码方法以显示何时点击 1 到 10

标签 ios swift

我真的需要你的帮助,我想了 2 个小时来使用一种方法来使我的代码更好,但我没有找到......我想在我点击示例 5 时显示一个弹出窗口按数字显示一个特定的弹出窗口所以例如,我可以使用 if field == 1 并 repeat against arrive to 10 但他看起来不太好而且太过时了。所以这里是相关问题的完整代码:

我希望你能理解我想要什么我认为这很简单,但无论如何我会帮助你提高我的技能和我的工作。

我只是想用更好的方式来展示和编写代码。

  func changeLog1P(){
    let changeLog1P = UIAlertController(title: "CHANGELOG V1.0 Build 1", message: "- Modifications et nouveautés -\n\n- Première version déployée", preferredStyle: .alert)
    changeLog1P.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
    self.present(changeLog1P, animated: true)
}

func changeLog2P(){
    let changeLog2P = UIAlertController(title: "CHANGELOG V1.0 Build 1", message: "- Modifications et nouveautés -\n\n- Première version déployée", preferredStyle: .alert)
    changeLog2P.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
    self.present(changeLog2P, animated: true)
}

func changeLog3P(){
    let changeLog3P = UIAlertController(title: "CHANGELOG V1.0 Build 1", message: "- Modifications et nouveautés -\n\n- Première version déployée", preferredStyle: .alert)
    changeLog3P.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
    self.present(changeLog3P, animated: true)
}

func changeLog4P(){
    let changeLog4P = UIAlertController(title: "CHANGELOG V1.0 Build 1", message: "- Modifications et nouveautés -\n\n- Première version déployée", preferredStyle: .alert)
    changeLog4P.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
    self.present(changeLog4P, animated: true)
}

func changeLog5P(){
    let changeLog5P = UIAlertController(title: "CHANGELOG V1.0 Build 1", message: "- Modifications et nouveautés -\n\n- Première version déployée", preferredStyle: .alert)
    changeLog5P.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
    self.present(changeLog5P, animated: true)
}

func changeLog6P(){
    let changeLog6P = UIAlertController(title: "CHANGELOG V1.0 Build 1", message: "- Modifications et nouveautés -\n\n- Première version déployée", preferredStyle: .alert)
    changeLog6P.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
    self.present(changeLog6P, animated: true)
}

func changeLog7P(){
    let changeLog7P = UIAlertController(title: "CHANGELOG V1.0 Build 1", message: "- Modifications et nouveautés -\n\n- Première version déployée", preferredStyle: .alert)
    changeLog7P.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
    self.present(changeLog7P, animated: true)
}

func errorBuildNumber(){
    let errorBuildNumberP = UIAlertController(title: "Build number incorrect", message: "Le build number que vous avez entré n'est pas conforme ou n'est pas correct, assurez-vous d'avoir :\n\n- Choisi un nombre décimal\n- Choisi un nombre positif non nul\n- Choisi un nombre sans virgule ni caractères spéciaux", preferredStyle: .alert)
    errorBuildNumberP.addAction(UIAlertAction(title: "Réésayer", style: .cancel, handler: { (UIAlertAction) in
        self.changelogPopup()
    }))

    self.present(errorBuildNumberP, animated: true)
}

func changelogPopup(){
    let changeLog1 = UIAlertController(title: "CHANGELOG", message: "Veuillez choisir la version pour laquelle vous souhaitez voir le changelog :", preferredStyle: .alert)
    changeLog1.addAction(UIAlertAction (title: "Continuer", style: .default, handler: { (UIAlertAction) in

        let field = changeLog1.textFields?[0] as! UITextField
        let textfieldInt: Int! = Int(field.text!)
        if (textfieldInt <= self.appVersion && textfieldInt >= 1){
            if field.text == "1"{
                self.changeLog1P()
            }
            if field.text == "2"{
                self.changeLog2P()
            }
            if field.text == "3"{
                self.changeLog3P()
            }
            if field.text == "4"{
                self.changeLog4P()
            }
            if field.text == "5"{
                self.changeLog5P()
            }
            if field.text == "6"{
                self.changeLog6P()
            }
            if field.text == "7"{
                self.changeLog7P()
            }
        }else{
            self.errorBuildNumber()
        }
    }))
    changeLog1.addAction(UIAlertAction(title: "Annuler", style: .cancel, handler: nil))


    changeLog1.addTextField { (textField) in
        textField.placeholder = "Numéro de Build de la version"
        changeLog1.textFields![0].keyboardType = UIKeyboardType.numberPad

    }

    self.present(changeLog1, animated: true, completion: nil)
}

谢谢你帮助我,

路易斯。

最佳答案

func changelogPopup(){
    let changeLog1 = UIAlertController(title: "CHANGELOG", message: "Veuillez choisir la version pour laquelle vous souhaitez voir le changelog :", preferredStyle: .alert)
    changeLog1.addAction(UIAlertAction (title: "Continuer", style: .default, handler: { (UIAlertAction) in

    let field = changeLog1.textFields?[0] as! UITextField
    let textfieldInt: Int! = Int(field.text!)
    if (textfieldInt <= self.appVersion && textfieldInt >= 1){
        //MINE
        self.changeLog1P(number: field.text)

        //YOURS
        /*
        if field.text == "1"{
            self.changeLog1P()
        }
        if field.text == "2"{
            self.changeLog2P()
        }
        if field.text == "3"{
            self.changeLog3P()
        }
        if field.text == "4"{
            self.changeLog4P()
        }
        if field.text == "5"{
            self.changeLog5P()
        }
        if field.text == "6"{
            self.changeLog6P()
        }
        if field.text == "7"{
            self.changeLog7P()
        }*/
    }else{
        self.errorBuildNumber()
    }
}))

//Add a parameter to take in
func changeLog1P(number:String){
    //Add the variable inside a string using "\()" wherever you'd like.
    let changeLog1P = UIAlertController(title: "CHANGELOG V1.0 Build \(number)", message: "- Modifications et nouveautés -\n\n- Première version déployée", preferredStyle: .alert)
    changeLog1P.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
    self.present(changeLog1P, animated: true)
}

你的例子不是很好,因为我看不出你想要改变什么,因为一切都是一样的。

关于ios - 如何使用 if 条件执行正确的代码方法以显示何时点击 1 到 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50840283/

相关文章:

ios - 在 Swift 中使用 UIButton 制作图像选择器

ios - 在启用分页的情况下使两个 UICollectionView 同步滚动

swift - 如何设置自动布局以使图像占据全屏?

swift - 是否可以在 Swift iOS 应用程序中设置所有字体的字体大小?

ios - 从应用程序打开系统暗模式 - Swift

html - iOS 将不可见的 Unicode 字符显示为包含 “mvs” 的虚线框

ios - 指定初始化程序的目的

ios - 如何取消 UICollectionView 的拖动?

objective-c - 使用 setAction :(SEL)@selector(start:) 时无法键入强制转换 @selector 表达式

ios - 如何让所有 CFTree Siblings 处于相同的深度?