【问题标题】:Open second UIAlertController from first UIAlertController从第一个 UIAlertController 打开第二个 UIAlertController
【发布时间】:2018-06-13 04:14:31
【问题描述】:

我们如何从第一个警报中打开第二个 UIAlert?

第一个代码块工作正常,显示警报。但如果选择了第一个警报中的一个选项,我希望能够调用出现的第二个警报视图。

在下面的示例中,xcode 不喜欢在调用第二个警报时使用“self”,我不知道如何设置它。

带有白色感叹号的红色错误是“Use of unresolved identifier 'self'”

有什么想法吗?

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

    let firstAlert = UIAlertController(title: "Title", message: "some message", preferredStyle: .alert)
    firstAlert.addAction(UIAlertAction(title: "Option A", style: .default, handler: alert2() ))
    firstAlert.addAction(UIAlertAction(title: "Option B", style: .default, handler: nil))
    firstAlert.addAction(UIAlertAction(title: "Option C", style: .default, handler: nil))
    firstAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    self.present(firstAlert, animated: true)
}

func alert2(alert: UIAlertAction!) {
    //Put second alert code here:

    let secondAlert = UIAlertController(title: "Title", message: "some message", preferredStyle: .alert)
    secondAlert.addAction(UIAlertAction(title: "Option A", style: .default, handler: nil ))
    secondAlert.addAction(UIAlertAction(title: "Option B", style: .default, handler: nil))
    secondAlert.addAction(UIAlertAction(title: "Option C", style: .default, handler: nil))
    secondAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    self.present(alert2, animated: true)
}

【问题讨论】:

  • firstAlert.addAction(UIAlertAction(title: “Option A”, style: .default, handler: alert2() ))线上的错误吗?
  • 好问题,是的,抱歉我没有说错误在哪里。第二部分中具有“self.present(alert2,animated:true)”的行的错误突出显示
  • 你为什么要展示函数alert2?出示secondAlert
  • 我刚改成secondAlert,错误依然存在。
  • 您有两个错误。这解决了第二个问题。

标签: ios swift uialertcontroller


【解决方案1】:

你有两个错误,

第一个,您需要在 UIAlertController 名称中显示第二个警报,而不是在 UIAlertAction 名称中

 self.present(secondAlert, animated: true)

不是方法名alert2

self.present(alert2, animated: true)

第二个,需要调用第一个alertcontroller UIAlertAction 完成处理程序方法,如 alert2 不是 alert2()

 firstAlert.addAction(UIAlertAction(title: "Option A", style: .default, handler: alert2 ))

完整答案

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

    let firstAlert = UIAlertController(title: "Title", message: "some message", preferredStyle: .alert)
    firstAlert.addAction(UIAlertAction(title: "Option A", style: .default, handler: alert2 ))
    firstAlert.addAction(UIAlertAction(title: "Option B", style: .default, handler: nil))
    firstAlert.addAction(UIAlertAction(title: "Option C", style: .default, handler: nil))
    firstAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    self.present(firstAlert, animated: true)
}

func alert2(alert: UIAlertAction!) {
    //Put second alert code here:
    let secondAlert = UIAlertController(title: "Title", message: "some message", preferredStyle: .alert)
    secondAlert.addAction(UIAlertAction(title: "Option A", style: .default, handler: nil ))
    secondAlert.addAction(UIAlertAction(title: "Option B", style: .default, handler: nil))
    secondAlert.addAction(UIAlertAction(title: "Option C", style: .default, handler: nil))
    secondAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    self.present(secondAlert, animated: true)
}

【讨论】:

  • 太棒了,谢谢你们。这让我意识到为什么我会收到“自我”错误,这是因为我在视图之外有警报 2 的功能确实出现了部分。一旦我把它放回那里,那个错误就消失了。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-06
  • 1970-01-01
相关资源
最近更新 更多