最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Swift中使用了pushViewController但是却没有跳转到新页面

Swift crifan 9694浏览 0评论
[背景]
折腾:
期间,需要实现页面到跳转:
swift click protocol
点击 注册协议 跳转到 我新写的:
RegistrationAgreementController
所以参考之前的已有代码,写了如下代码:
    func registrationAgreementAction(button : UIButton){
        print("registrationAgreementAction()")
              centerViewController.navigationController?.pushViewController(RegistrationAgreementController(), animated: true)
           }
但是页面并没有跳转到对应的RegistrationAgreementController里面的viewDidLoad:
import UIKit

class RegistrationAgreementController : UIViewController {
    init() {
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
[解决过程]
1.去改为:
self.navigationController?.pushViewController(RegistrationAgreementController(), animated: true)
或:
centerViewController.navigationController?.pushViewController(RegistrationAgreementController(), animated: false)
结果也还是没有跳转。
2.去掉那些init试试:
import UIKit

class RegistrationAgreementController : UIViewController {
//    init() {
//        super.init(nibName: nil, bundle: nil)
//    }
//
//    required init?(coder aDecoder: NSCoder) {
//        super.init(coder: aDecoder)
//    }

    override func viewDidLoad() {
        super.viewDidLoad()
结果:
还是不行。
3.搜:
swift navigationController pushViewController not switch ui
swift pushViewController not switch ui
swift UI  not switch
swift pushViewController not work
参考:
4.参考:
去试试,重启Xcode。
结果问题依旧。
去用:NSLog打印navigationController 看看是否是nil:
        NSLog("centerViewController.navigationController=\(centerViewController.navigationController)")
        NSLog("self.navigationController=\(self.navigationController)")
果然,self的是nil,不过另外那个不是nil:
2015-10-20 17:43:04.625 xxx[12863:1866954] centerViewController.navigationController=Optional(<UINavigationController: 0x7fd7ca079c00>)
2015-10-20 17:43:04.625 xxx[12863:1866954] self.navigationController=nil
所以改为:
centerViewController.navigationController?.pushViewController(RegistrationAgreementController(), animated: true)
5.搜:
swift navigationController not working
参考:
->
先去看看storyboard是否是nil:
        NSLog("self.storyboard=\(self.storyboard)")
结果是nil:
self.storyboard=nil
6.后来调试期间,无意间发现:
点击了两次的回退:
click navi back second time click navi back
后,然后才执行到了对应的代码:
now run viewdidload
所以:
实际上,之前的pushViewController是生效的
然后去调试代码,
7.通过:
网页内容是可以正常显示了:
但是需要接着解决,页面没有跳转的原因。
搜:
后来去看到:
之前的view的代码,好像不是调用的:pushViewController
而是presentViewController:
    override func viewWillAppear(animated: Bool) {
        if isLogin == false {
centerViewController.presentViewController(LoginViewController(), animated: true, completion: nil)
        }
    }
    func registerAction(button : UIButton){
       self.presentViewController(RegisterViewController(), animated: true, completion: nil)
    }
-》即,先后两层的view的调用,都是用的是:presentViewController
-》所以导致了,我最新的view,即使去调用了pushViewController,但是却被之前的两层的view,挡住了。
-》所以,看起来,去把前面两层的UI的view的跳转方式改为pushViewController
或者是:
最新的UI的view,也去改为:presentViewController
估计就可以正常显示了。
去试试后面的方式:
self.presentViewController(RegistrationAgreementController(), animated: true, completion: nil)
结果是:
就可以正常显示了:
点击:
注册协议
即可跳转到:新的注册协议的页面:
然后点击回退:
register protocol click back
即可退回前面的一个页面。
[总结]
此处,父级页面,对应的页面跳转代码是:
        let regProtolBtn = UIButton()
        regProtolBtn.frame = CGRectMake(210, self.view.bounds.height / 2 + 175, 60, 20)
        regProtolBtn.addTarget(self, action:Selector("registrationAgreementAction:"), forControlEvents:UIControlEvents.TouchUpInside)
        let regProtolLabel = UILabel()
        regProtolLabel.frame = CGRectMake(0, 0, 60, 20)
        regProtolLabel.text = "注册协议"
        regProtolLabel.textColor = UIColor(hexString: "#1970fe")
        regProtolLabel.textAlignment = NSTextAlignment.Left
        regProtolLabel.font = UIFont.boldSystemFontOfSize(13)
        regProtolBtn.addSubview(regProtolLabel)
        self.view.addSubview(regProtolBtn)

    func registrationAgreementAction(button : UIButton){
        self.presentViewController(RegistrationAgreementController(), animated: true, completion: nil)
    }
对应的字页面的返回代码是:
        let backImage = UIImage(named: "back.png")
        let backBtn = UIButton()
        backBtn.frame = CGRectMake(10, 30, 24, 24)
        backBtn.addTarget(self, action:Selector("close"), forControlEvents:UIControlEvents.TouchUpInside)
        backBtn.setImage(backImage, forState:UIControlState.Normal)
        backBtn.setImage(backImage, forState: UIControlState.Selected)
        self.view.addSubview(backBtn)

    func close() {
        self.dismissViewControllerAnimated(true, completion: {

        })
    }
注意:
如果前面用的是pushViewController:
self.navigationController?.pushViewController(RegistrationAgreementController(), animated: true)

则子页面应该用:

    func close(button : UIButton) {
        self.navigationController?.popViewControllerAnimated(true)
    }

这样才能正常的,push一个view,然后退出,返回页面时pop出对应的view。

[后记]
参考了:
把:
pushViewController:animated
改为:showViewController
        let regAgreeVC = RegistrationAgreementController()
        self.showViewController(regAgreeVC as UIViewController, sender: regAgreeVC)
结果也是可以的,正常可以显示页面和切换页面,返回页面的。

转载请注明:在路上 » [已解决]Swift中使用了pushViewController但是却没有跳转到新页面

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.169 seconds, using 22.11MB memory