Ios 如何将单独函数中生成的随机数同步到另一个@IBAction函数
我有一个本地Json文件,里面填充了两组数据:图像和它的描述符(文本)。我生成了一个随机数,在这个数上,函数从Json返回图像。还有一个单独的UIButton,它创建了一个必须显示描述的警报视图。我无法在@IBAction func中调用相同的随机数来同步此号码,以获取描述。如果有什么解决办法,我很乐意使用!很多。 这里有一个密码:Ios 如何将单独函数中生成的随机数同步到另一个@IBAction函数,ios,json,swift,function,random,Ios,Json,Swift,Function,Random,我有一个本地Json文件,里面填充了两组数据:图像和它的描述符(文本)。我生成了一个随机数,在这个数上,函数从Json返回图像。还有一个单独的UIButton,它创建了一个必须显示描述的警报视图。我无法在@IBAction func中调用相同的随机数来同步此号码,以获取描述。如果有什么解决办法,我很乐意使用!很多。 这里有一个密码: func popCardViewWithFrame(_ frame : CGRect) -> UIView? { let randomIndex =
func popCardViewWithFrame(_ frame : CGRect) -> UIView? {
let randomIndex = Int(arc4random_uniform(36) + 1)
let imageView = UIImageView()
imageView.image = UIImage(named: people[randomIndex].image
print(randomIndex)
return imageView
}
这里是@IBAction func,我想使用相同的生成随机数:
@IBAction func ButtonAction(_ sender: UIButton) {
let appearance = SCLAlertView.SCLAppearance(
kTitleFont: UIFont(name: "HelveticaNeue", size: 20)!,
kTextFont: UIFont(name: "HelveticaNeue", size: 14)!,
kButtonFont: UIFont(name: "HelveticaNeue-Bold", size: 14)!,
showCloseButton: false
)
let alert = SCLAlertView(appearance: appearance)
let subview: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 70))
let x = (subview.frame.width - 180) / 2
let textfield1 = UILabel(frame: CGRect(x: x, y: 10, width: 180, height: 25))
textfield1.text = people[randomIndex].text
print(randomIndex)
}
首先,您需要使用函数创建一个
变量
。应该是这样的:
var randomNumberCompletion:((Int)->())?
然后,在声明了随机数之后,使用以下完成块:
func popCardViewWithFrame(_ frame : CGRect) -> UIView? {
let randomIndex = Int(arc4random_uniform(36) + 1)
randomNumberCompletion?(randomIndex)
let imageView = UIImageView()
imageView.image = UIImage(named: people[randomIndex].image
print(randomIndex)
return imageView
}
然后在@IBAction func
中调用此完成块,如下所示:
func popCardViewWithFrame(_ frame : CGRect) -> UIView? {
let randomIndex = Int(arc4random_uniform(36) + 1)
randomNumberCompletion?(randomIndex)
let imageView = UIImageView()
imageView.image = UIImage(named: people[randomIndex].image
print(randomIndex)
return imageView
}
@iAction功能按钮操作(\发送方:UIButton){
首先,您需要创建一个带有函数的
变量
var randomNumberCompletion:((Int)->())?
然后,在声明了随机数之后,使用以下完成块:
func popCardViewWithFrame(_ frame : CGRect) -> UIView? {
let randomIndex = Int(arc4random_uniform(36) + 1)
randomNumberCompletion?(randomIndex)
let imageView = UIImageView()
imageView.image = UIImage(named: people[randomIndex].image
print(randomIndex)
return imageView
}
然后在@IBAction func
中调用此完成块,如下所示:
func popCardViewWithFrame(_ frame : CGRect) -> UIView? {
let randomIndex = Int(arc4random_uniform(36) + 1)
randomNumberCompletion?(randomIndex)
let imageView = UIImageView()
imageView.image = UIImage(named: people[randomIndex].image
print(randomIndex)
return imageView
}
@iAction功能按钮操作(\发送方:UIButton){
答案有很多,但问题是我在@iAction func中得到一个错误,类型为“ViewController”的值没有成员“textfield1”。答案有很多,但问题是我在@iAction func中得到一个错误,类型为“ViewController”的值没有成员“textfield1”。