这个问题之前可能已经在这里提出过,但它是在obj C中,我尝试了无数种方法但没有成功。所以,我有一个带有一组按钮的viewcontroller,当用户单击一个添加按钮时,这些按钮会一次添加一个按钮。问题是我有一个导航控制器,每当我去另一个页面并回到这个viewcontroller页面时,按钮就消失了。由于我是Swift的新手并且提前感谢,所以非常感谢帮助!
以下是我的代码: 添加按钮
func addStartingButtons()
{
for lockers in arrayOfLockers
{
let addLockerbtn = UIButton(frame: CGRect(x: 250, y: buttonY + 70, width: 120, height: 30))
buttonY = buttonY + 80 // we are going to space these UIButtons 50px apart
addLockerbtn.layer.cornerRadius = 10 // get some fancy pantsy rounding
addLockerbtn.backgroundColor = UIColor.grayColor()
addLockerbtn.setTitle("Add New+", forState: UIControlState.Normal) // We are going to use the item name as the Button Title here.
addLockerbtn.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
addLockerbtn.addTarget(self, action: "ButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(addLockerbtn) // myView in this case is the view you want these buttons added
}
}//end of addStartingButtons Func
func ButtonPressed(sender:UIButton!)
{
let image = UIImage(named: "lock.png") as UIImage!
if number<7
{
if number == 1
{
self.btn1.hidden = false
btn1.backgroundColor = UIColor.grayColor()
btn1.setImage(image, forState: .Normal)
btn1lbl.text? = "Locker\(number)"
btn1.layer.cornerRadius = 10
}
else if number == 2
{
self.btn2.hidden = false
btn2.backgroundColor = UIColor.grayColor()
btn2.setImage(image, forState: .Normal)
btn2lbl.text? = "Locker\(number)"
btn2.layer.cornerRadius = 10
}
else if number == 3
{
self.btn3.hidden = false
btn3.backgroundColor = UIColor.grayColor()
btn3.setImage(image, forState: .Normal)
btn3lbl.text? = "Locker\(number)"
btn3.layer.cornerRadius = 10
}
else if number == 4
{
self.btn4.hidden = false
btn4.backgroundColor = UIColor.grayColor()
btn4.setImage(image, forState: .Normal)
btn4lbl.text? = "Locker\(number)"
btn4.layer.cornerRadius = 10
}
else if number == 5
{
self.btn5.hidden = false
btn5.backgroundColor = UIColor.grayColor()
btn5.setImage(image, forState: .Normal)
btn5lbl.text? = "Locker\(number)"
btn5.layer.cornerRadius = 10
}
else if number == 6
{
self.btn6.hidden = false
btn6.backgroundColor = UIColor.grayColor()
btn6.setImage(image, forState: .Normal)
btn6lbl.text? = "Locker\(number)"
btn6.layer.cornerRadius = 10
}
number++
这是针对navigationController:
let splitViewController = self.window!.rootViewController as! UISplitViewController
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
splitViewController.delegate = self
return true