I use viewcontrollers
with Navigation Controller
. I press a UIButton
on homescreeen and go another viewcontroller
. When I click a UIButton
on new viewcontroller
, I go homescreen again. But when I press a UIButto
n on homescreen, this time, it gives an error.
I created button with codes, not on storyboard.
What could be the problem? Some friends say, poptorootviewcontroller
solves the problem, but I dont know adding it to button. Anyone can help?
here is the code;
override func viewDidLoad() {
super.viewDidLoad()
let closeButton = UIButton()
closeButton.frame = CGRect(x: screen.width - 70, y: 20, width: 60, height: 60)
closeButton.setTitle("Skip", forState: .Normal)
closeButton.setTitleColor(UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 0.5), forState: .Normal)
closeButton.titleLabel!.font = UIFont.systemFontOfSize(16)
closeButton.addTarget(self, action: #selector(OnboardingController2.pressed(_:)), forControlEvents: .TouchUpInside)
view.addSubview(closeButton)
}
func pressed(sender: UIButton!) {
audioPlayer?.stop();
let loginPageView = self.storyboard?.instantiateViewControllerWithIdentifier("HomePage") as! ViewController
self.presentViewController(loginPageView, animated: true, completion: nil)
}
just replace the below code in your pressed
button action. because when you are skipping and going to home view controller. you are presenting the controller so navigation is no more in the stack. and again when you press on the computer button it will try to push the controller and because navigation is no more in the stack it is crashing. when you are pressing the skip button try to pop
view controller. like below.
func pressed(sender: UIButton!) {
audioPlayer?.stop();
self.navigationController?.popToRootViewControllerAnimated(true)
// OR
self.navigationController?.popViewControllerAnimated(true)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.