【问题标题】:How can I present more than one alert in an if statement in Swift UIKit?如何在 Swift UIKit 的 if 语句中显示多个警报?
【发布时间】:2021-04-05 22:30:14
【问题描述】:
    func submit(_ answer: String) {
        let lowerAnswer = answer.lowercased()
        
        if answer.count == 0 {
            showErrorMessage(errorTitle: "No answer", errorMessage: "Please provide a valid answer")
            return
        }
        
        if isPossible(word: lowerAnswer) {
            if isOriginal(word: lowerAnswer) {
                if isReal(word: lowerAnswer) {
                    userWords.insert(answer, at: 0)
                    
                    let indexPath = IndexPath(row: 0, section: 0)
                    tableView.insertRows(at: [indexPath], with: .automatic)
                    
                    return
                }
                
                showErrorMessage(errorTitle: "Word not recognised", errorMessage: "You can't just make them up, you know!")
                return
            }
            
            showErrorMessage(errorTitle: "This word is already used!", errorMessage: "New word is required!")
            return
        }
        
        guard let title = title?.lowercased() else { return }
        showErrorMessage(errorTitle: "Word not possible", errorMessage: "You can't spell that word from \(title)")
        return
    }
    
    func isPossible(word: String) -> Bool { // Will check if the user is able to formuate a word
        guard var tempWord = title?.lowercased() else { return false }
        
        for letter in word { // Loops through the word provided by the player by checking and adding each letter to "letter" loop constant [This will contain all the letter from that word]
            
            if let positon = tempWord.firstIndex(of: letter) { // Checks if the player's word comtains letters from the start word [from what the player need to create a new word]
                tempWord.remove(at: positon) // Removes the used letter from the start word, that letter will not exist anymore so the player will not be able to use it twice
            } else {
                return false
            }
        }
        
        return true
    }
    
    func isOriginal(word: String) -> Bool { // Will check for duplicate words if any
        return !userWords.contains(word) // If the word is new this method will return "true", if the word was used before this method will return "false"
    }
    
    func isReal(word: String) -> Bool { // Will check is the word is a real word [english]
        let checker = UITextChecker()
        let range = NSRange(location: 0, length: word.utf16.count)
        let misspelledRange = checker.rangeOfMisspelledWord(in: word, range: range, startingAt: 0, wrap: false, language: "en")
        
        
        if word.utf16.count < 3 {
            showErrorMessage(errorTitle: "Word to short", errorMessage: "Please provide a longer word")
            print("This word is to short!")
            return false
            
        } else if title?.utf16.count == word.utf16.count {
            // showErrorMessage(errorTitle: "Invalid answer", errorMessage: "You cannot submit the start word!")
            print("You can't submit the start word!")
            return false
        }
        
        return misspelledRange.location == NSNotFound // if misspelledRange.location == NSNotFound [true = not a valid word]; if misspelledRange.location == NSNotFound [false = we have a valid word        
    }
    
    func showErrorMessage(errorTitle: String, errorMessage: String) {
        let alert = UIAlertController(title: errorTitle, message: errorMessage, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default))
        present(alert, animated: true)
    }
}

如何在“isReal”if 语句中显示 3 个警报?
我试过了,调试控制台告诉我这个:

2021-04-05 23:25:37.577297+0100 Project5[4940:314069] [Presentation] Attempt to present on (from

【问题讨论】:

  • 为什么要显示三个警报?你不能把所有信息都放在一个里面吗?
  • 简短回答:是的,你可以这样做,但你不应该
  • 显示 3 个警报是什么意思?您的意思是,在一个警报被解除后显示另一个警报?
  • 我想在用户提供少于 3 个字母的单词以及想要提交起始单词时给他们一些反馈。只是一个练习,我想在学习时进行实验,目前只是从 Paul Hudson 的网站上制作一些小应用程序。感谢大家抽出宝贵时间回复我!
  • 是的,我知道对于真正的文字应用程序来说绝对不是一个好习惯!

标签: swift


【解决方案1】:

你可以用这样的扩展来做到这一点:

extension UIViewController {
    static func top() -> UIViewController {
        guard let rootViewController = UIApplication.shared.delegate?.window??.rootViewController else { fatalError("No view controller present in app?") }
        var result = rootViewController
        while let vc = result.presentedViewController, !vc.isBeingDismissed {
            result = vc
        }
        return result
    }
}

然后,您可以打电话给UIViewController.top().present(alert, animated: true),而不是只打电话给present(alert, animated: true)。 这将导致每个附加警报出现在前一个警报之上。用户必须先关闭所有三个警报,然后才能返回您的屏幕。


以上回答了您的问题,但我希望一旦您了解用户不得不关闭多个警报的情况后,您将重新考虑您的设计,而是针对所有问题提供一个带有适当文本的警报。或者找一些其他的 UI 来处理这个问题。

【讨论】:

  • 谢谢丹尼尔,是的,我在想用户会被所有这些警报淹没,我现在只是在学习保罗哈德森网站上的一些研究项目,我在想如果我在“isReal()”方法中声明了这两个条件,那么在这种情况下为用户显示适当的反馈会很好,但我不知道如何显示更多警报,我明白我'我已经在“isReal”的 if 语句中显示警报。感谢您抽出宝贵时间回到我身边!在这一点上很有帮助。
  • 以前从未使用过扩展程序,是的,我知道这不是一个好习惯,我想我怎么能显示更多警报,用户如何知道他们需要提供更长的单词或者他们无法提交视图加载后给出的起始词。
  • @Daniel.T 我在“isReal()”方法中有不同的条件,是的,我同意只提供一个警报,但我遇到的问题是一个警报如何涵盖 3 个不同的条件如果用户做了不正确的事情?
  • if x || y || z { present Alert }
  • 非常感谢,我将重构我的代码,是的,这些警报绝对不能相互叠加,会给实际的最终用户带来压力:))如果这个应用程序上线了。
猜你喜欢
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 2023-02-26
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
相关资源
最近更新 更多