gpt4 book ai didi

ios - presentViewController 在 Swift 中不工作

转载 作者:可可西里 更新时间:2023-10-31 23:56:35 28 4
gpt4 key购买 nike

感谢您阅读本文。我想要一个函数 Swift 文件,我将项目的所有函数放入其中,其他 Swift 文件可以调用。我试图在函数文件中创建一个警报函数,当我传入一个特定的字符串时,它会显示一个特定的警报。当它在主文件中时它工作正常,但是当我将它移动到函数文件时,presentViewController 给我一个错误,说“使用未解析的标识符‘presentViewController’。”请帮忙!这是我的代码:在函数文件中:

import Foundation
import UIKit

/**********************************************
Variables
***********************************************/
var canTapButton: Bool = false
var tappedAmount = 0

/**********************************************
Functions
***********************************************/

//the alert to ask the user to assess their speed
func showAlert(alert: String) -> Void
{
if(alert == "pleaseAssessAlert")
{
let pleaseAssessAlert = UIAlertController(title: "Welcome!", message: "If this is your firs time, I encourage you to use the Speed Assessment Tool (located in the menu) to figure which of you fingers is fastest!", preferredStyle: .Alert)
//ok button
let okButtonOnAlertAction = UIAlertAction(title: "Done", style: .Default)
{ (action) -> Void in
//what happens when "ok" is pressed
}
pleaseAssessAlert.addAction(okButtonOnAlertAction)

presentViewController(pleaseAssessAlert, animated: true, completion: nil)
}
else
{
println("Error calling the alert function.")
}
}

谢谢!

最佳答案

presentViewControllerUIViewController的实例方法类(class)。所以你不能像这样在你的函数文件上访问它。

您应该像这样更改函数:

func showAlert(alert : String, viewController : UIViewController) -> Void
{
if(alert == "pleaseAssessAlert")
{
let pleaseAssessAlert = UIAlertController(title: "Welcome!", message: "If this is your firs time, I encourage you to use the Speed Assessment Tool (located in the menu) to figure which of you fingers is fastest!", preferredStyle: .Alert)
//ok button
let okButtonOnAlertAction = UIAlertAction(title: "Done", style: .Default)
{ (action) -> Void in
//what happens when "ok" is pressed
}
pleaseAssessAlert.addAction(okButtonOnAlertAction)
viewController.presentViewController(pleaseAssessAlert, animated: true, completion: nil)
}
else
{
println("Error calling the alert function.")
}
}

在这里,您将 UIViewController 实例传递给此函数并调用该 View Controller 类的 presentViewController

关于ios - presentViewController 在 Swift 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27701323/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com