2016-05-13 89 views
1

我有一个警报控制器,应该在用户在文本字段中输入不正确数量的字符后出现。警报控制器根本没有出现。 WUIAlertController not displayed

func usernameFieldCharacters() { 
    let alertController = UIAlertController(title: "Alert", message: "Five characters or more is required in all fields" , preferredStyle: UIAlertControllerStyle.Alert) 

    let okAction = UIAlertAction(title: "OK", style: .Default) { 

    action -> Void in // Does not do anything 
    } 

    alertController.addAction(okAction) // adds the OK button to 
    // to alert controller 

    let allowedChars = 5 // character amount has to be equal or greater in each field 
    let usernameCount = theUsernameField.text?.characters.count 

    if usernameCount < allowedChars { 
     self.presentViewController(alertController, animated: true, completion: nil) 
    } else { 
     alertController.viewDidAppear(false) 
    } 
} 
+0

你不应该需要调用'viewDidAppear'法第所有。然后检查**主线程中提供的alertController ** – ridvankucuk

回答

-2

以上代码正常工作。 而alertController.viewDidAppear(假)是不需要

1

代码工作正确的,当u移动alertcontroller IE浏览器viewDidAppear法:

class ViewController: UIViewController { 

    override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(animated) 

    let allowedChars = 5 // character amount has to be equal or greater in each field 

    let usernameCount = theUsernameField.text?.characters.count 

    if usernameCount < allowedChars { 
     // Do any additional setup after loading the view, typically from a nib. 
     let alertController = UIAlertController(title: "Alert", message: "Five characters or more is required in all fields" , preferredStyle: UIAlertControllerStyle.Alert) 

     let okAction = UIAlertAction(title: "OK", style: .Default) { 

      action -> Void in // Does not do anything 
     } 

     alertController.addAction(okAction) // adds the OK button to 
     // to alert controller 

     self.present(alertController, animated: true, completion: nil) 
    } 
+0

如果所有警报控制器代码都在'if'语句内移动,那将会更好。为什么创建并设置警报,如果它不会显示? – rmaddy

+1

为什么你认为这个代码需要在'viewDidAppear:'?这没有理由。 – rmaddy

+0

@rmaddy不,你是真的,我不知道为什么**。我发现,当我有一个类似的问题。 viewDidLoad似乎要迟到了... –