2017-05-16 28 views
3

我想为用户设置一个UIAlert作为“检查”。用户将要保存一组信息,但我想让用户有机会在点击“保存”按钮后查看信息。从本质上讲,他们点击“保存”,然后我想UIAlert出现显示他们正在保存要求信息“你确定所有这些信息是正确的:”,然后显示所有信息的:如何设置UIAlert以显示大量信息?

@IBAction func saveButtonTapped(_ sender: UIBarButtonItem) { 
     //create alert (I know how to do this) that shows all info (I don't know how to do this) 
     //If user is ok with the info, perform a segue, if not return to page 
} 

问题是,我知道UIAlerts可以有像“标题”和“消息”的组件,但我希望警报在时尚等列表中显示大量信息。无论如何要做到这一点?或者,我是否需要不使用警报,而是可能将它带到不同的确认页面或可能是不同的UI元素?

回答

2

要在视图控制器上显示警报,可以使用以下代码。

let a = UIAlertController(title: "Your title", message: "Your message", preferredStyle: .alert) 
a.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in 
    // Pressed "OK" 
})) 
self.present(a, animated: true, completion: { finished in 
    // Alert shown 
}) 

但是,有多少信息可以适合警报的限制。如果你有更长的时间,创建一个新的视图控制器和模态地呈现它也可以工作。这可让您自定义您想要呈现的信息的方式(例如,使用滚动/页面视图)。您还可以自定义您的视图控制器,使其看起来像一个警报,使其目的更清晰。要以模态方式显示视图控制器,可以使用present方法。

self.present(otherViewController, animated: true, completion: nil) 

一种方法我在通路中,以使警报状态视图控制器呈现较小的视图控制器在当前之一,并改变其模式的演讲风格,让您可以看到基本视图控制器通过它。

let otherVC = self.storyboard?.instantiateViewController(withIdentifier: "OtherViewController") as! OtherViewController 
otherVC.modalPresentationStyle = .overCurrentContext 
otherVC.view.center = vc.view.center 
otherVC.delegate = self //Create a delegate so that you can control actions such as "OK" buttons 
self.view.isUserInteractionEnabled = false //Stop the user interacting with the view controller behind the alert 
self.present(otherVC, animated: true, completion: nil) 

编辑: 您可以委派控制操作,如用行动关闭警报。例如,这可能是你委托的一个例子:

protocol AlertDelegate { 
    func didCancel() 
    func didOkay() 
} 

然后,你可以实现这个像这样:

class RootViewController: UIViewController, AlertDelegate { 
    func didCancel() { ... } 
    func didOkay() { ... } 

    func showAlert() { 
     ... 
     otherVC.delegate = self 
     ... 
    } 
} 

然后在你的警报视图控制器,你可以与委托进行交互。

class MyAlert: UIViewController { 
    var delegate: AlertDelegate! 

    @IBAction func cancelButton(sender: UIButton) { 
     delegate.didCancel() 
     self.dismiss(animated: true, completion: nil) 
    } 
} 

所以,当点击警报视图控制器上的取消按钮时,代表会说,它取消了,和模态视图控制器将被解雇。然后,根视图控制器会收到这个动作,并可以相应地处理它。

+0

我喜欢这个想法。那么我怎么最终关闭overop viewController(otherVC)?而且,与此设置,我可以运行从原来的VC代码,让说,用户在otherVC点击“确定”后otherVC关闭?... 就像我要上originalVC的saveButton打开显示信息的otherVC ...用户在其他VC中点击“OK”,另一个VC关闭/消失,然后我想让其余的'saveButtonTapped'代码从原始VC运行? –

+0

@AkkPiasecki是的,代表在这里派上用场。请参阅我为更多信息所做的修改。 – brimstone

+0

我还有一个问题。当按钮被击中时,你在RootViewController中调用'self.view.isUserInteractionEnabled = false'。显然,一旦按钮运行(“ok”或“cancel”),我需要在另一个VC中设置'self.view.isUserInteractionEnabled = true',但是如何在其他VC的代码中引用RootViewController?我试过了: 'let rootVC = self.storyboard?.instantiateViewController(withIdentifier:“RootViewController”)as! RootViewController',然后在另一个VC的“ok”按钮中输入'rootVC.view.isUserInteractionEnabled = true',但这不起作用。 –

1

可以使用UIAlertViewController来呈现很多行,它会自动处理滚动。但问题是,如果可用和相当不够?

看一看这个简单的例子:

@IBAction func showAlert(_ sender: UIButton) {   
     var lines = [String]() 
     for i in 1...100 { 
      lines.append("this is line \(i)") 
     } 

     let alertController = UIAlertController(title: "title", message: lines.joined(separator: "\n"), preferredStyle: .alert) 

     alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) 

     present(alertController, animated: true, completion: nil) 
    } 

,给了我这样的观点:

alert view with many lines

与滚动内容。

的几个问题和注意事项:

  1. 似乎有一个上限。如果我改变了对for i in 1...500比如我看不到任何内容
  2. 这是不是真的那么人性化,我认为

所以...我想你应该考虑比UIAlertViewController :)

另一种解决方案

希望能帮到你。

+0

有一个iOS的设计原则,建议您不要添加这么多的信息,一个'UIAlertViewController'也必须滚动。 –

+0

@DanielStorm啊......谢谢,这很有道理。 'UIAlertViewController'不是它的混乱内容 – pbodsk