gpt4 book ai didi

Swift 动态按钮创建

转载 作者:可可西里 更新时间:2023-11-01 01:25:38 27 4
gpt4 key购买 nike

我是 swift 的新手。我通过使用以下代码在同一个 viewController 中按下另一个按钮来创建动态按钮:

@IBAction func yenibtn(sender: AnyObject) {

let btn = UIButton()
btn.frame = CGRectMake(10, 10, 50, 50) //set frame
btn.setTitle("btn", forState: .Normal) //set button title
btn.setTitleColor(UIColor.redColor(), forState: .Normal) //set button title color
btn.backgroundColor = UIColor.greenColor() //set button background color
btn.tag = 1 // set button tag
btn.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
self.view.addSubview(btn) //add button in view



}
func clickMe(sender:UIButton!)
{
print("Button Clicked")

}

我创建了第二个 viewController 并通过 performSegueWithIdentifier 函数进行导航。当我回调前一个 Controller 时,动态对象消失了。有没有办法在重新加载以前的 ViewController 期间保留动态按钮?

最佳答案

只需使用这个:

   var btn:UIButton!
@IBAction func yenibtn(sender: AnyObject) {
btn= UIButton()
btn.frame = CGRectMake(10, 10, 50, 50) //set frame
///... your code
}

更新

在第二个 View Controller 中使用您的代码关闭

 @IBAction func basgeri(sender: AnyObject) {

performSegueWithIdentifier("bir", sender: self)
}

想“我正在关闭 View Controller ”,但你错了!你创建了一个新的 ViewController!您不会返回到上一个!

要修复它,请使用:

 @IBAction func basgeri(sender: AnyObject) {

presentingViewController?.dismissViewControllerAnimated(true, completion: nil)

}

关于Swift 动态按钮创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41694427/

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