我想知道如何在点击登录按钮后创建一个可以移动到登录页面的功能,代码/编程方式
override func viewDidLoad() {
super.viewDidLoad()
//make the login button
let loginButton = UIButton(frame: CGRectMake(20, 640, 175, 75))
loginButton.setTitle("Login", forState: UIControlState.Normal)
loginButton.addTarget(self, action: #selector(ViewController.tapActionButton), forControlEvents: UIControlEvents.TouchUpInside)
loginButton.backgroundColor = UIColor.init(red: 255, green: 215, blue: 0, alpha: 0.8)
loginButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
loginButton.titleLabel!.font = UIFont(name: "StarJediSpecialEdition", size: 30)
self.view.addSubview(loginButton)
//make the register button
let registerButton = UIButton(frame: CGRectMake(220,640,175,75))
registerButton.setTitle("Register", forState: UIControlState.Normal)
registerButton.addTarget(self, action: #selector(ViewController.tapActionButton), forControlEvents: UIControlEvents.TouchUpInside)
registerButton.backgroundColor = UIColor.init(red: 255, green: 215, blue: 0, alpha: 0.8)
registerButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
registerButton.titleLabel!.font = UIFont(name: "StarJedi Special Edition", size: 30)
self.view.addSubview(registerButton)} func tapActionButton(sender:UIButton!){
print("Button is working")
}
所以我的功能现在就是这个,但实际上我需要点击按钮移动到下一个视图控制器,有人请帮帮我
答案 0 :(得分:12)
根据您创建ViewController的方式,有多种方法可以执行此操作。
故事板和Segue(对于大多数情况,这非常有效)
在您的登录视图控制器和您想要显示的ViewController之间创建segue(例如: - ABCViewController),为您的segue提供标识符和演示文稿样式。对于您的情况,它将是Show
现在在LoginViewController中覆盖下面的方法
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "YourSegueIdentifier"
let abcViewController = segue.destinationViewController as! ABCViewController
abcViewController.title = "Home"
}
}
在loginButton操作上:
performSegueWithIdentifier("YourSegueIdentifier", sender: nil)
你已经完成了。
为此,你必须在视图板中为视图控制器提供一个标识符,然后你可以使用它来实例化它并使用它来推送它:
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let abcViewController = storyboard.instantiateViewControllerWithIdentifier("ABCViewControlleridentifier") as! ABCViewController
abcViewController.title = "ABC"
navigationController?.pushViewController(abcViewController, animated: true)
答案 1 :(得分:2)
您需要创建下一个视图控制器的实例,然后显示或推送该视图控制器
以模态呈现
func goToNextViewController() {
let nextVC = NextViewController() {
//you can set properties of the nextVC here
self.presentViewController(nextVC, animated: true, completion: nil)
或推:
self.navigationController.pushViewController(nextVC, animated: true)
segue的所有内容都适用于故事板
干杯!