swift ios 中的视图控制器中的按钮操作和导航不起作用

Posted

技术标签:

【中文标题】swift ios 中的视图控制器中的按钮操作和导航不起作用【英文标题】:Button action and navigation not working in viewcontroller in swift ios 【发布时间】:2017-01-11 06:45:43 【问题描述】:
 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 
    self.window = UIWindow(frame:UIScreen.main.bounds)
    let homeVC = ConstDetailViewController(nibName: "ConstDetailViewController", bundle: nil)
    //homeVC.tabBarItem.title = "Home";
    homeVC.tabBarItem.image = UIImage(named: "HomeRe.png")

    // Settings controller
    let settingsVC = LeaderboardViewController(nibName: "LeaderboardViewController", bundle: nil)

    settingsVC.tabBarItem.image = UIImage(named: "search3.png")

    let mapVC = MapViewViewController(nibName: "MapViewViewController", bundle: nil)
    mapVC.tabBarItem.image = UIImage(named: "graphRe.png")

    let userVC = UserProfileViewController(nibName: "UserProfileViewController", bundle: nil)

    userVC.tabBarItem.image = UIImage(named: "UserRe.png")

    self.tabBarController = UITabBarController()
    self.tabBarController!.setViewControllers([homeVC, settingsVC,mapVC,userVC], animated: false);

    let loginVC = ConstituencyViewController(nibName: "ConstituencyViewController", bundle: nil)

    self.window!.rootViewController = loginVC
    self.window!.makeKeyAndVisible()

我已经在 appdelegate 中设置了 tabbar 控制器。但是现在按钮操作和其他导航在我的视图控制器中不起作用。如何在此处设置导航?请帮忙

【问题讨论】:

检查mysamplecode.com/2013/02/ios-tab-bar-controller-example.html。这将为您提供有关您的要求的完整信息。 @Amanpreet。我没有得到这个客观的 C 代码。请帮助我快速代码。你能帮我写代码吗? 查看geekyviney.blogspot.in/2015/02/programmatically-designing.html @Amanpreet。我已经检查过了。我需要添加导航。我的导航不起作用。 你检查过这个***.com/questions/28005108/… 【参考方案1】:

我已经在这里一步一步地给你看。请遵循-

Step1- AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 

        let loginVC = LoginViewController()
        loginVC.title = "LoginViewController"
        let VC3Navigation = UINavigationController(rootViewController: loginVC)

        self.window! = UIWindow(frame: UIScreen.main.bounds)

        self.window!.backgroundColor = UIColor.white

        self.window?.rootViewController = VC3Navigation

        self.window!.makeKeyAndVisible()
        return true
        // Override point for customization after application launch.
    

Step2- 创建UITabBarController 的新文件,例如:

    import UIKit

    class TabbViewController: UITabBarController,UITabBarControllerDelegate
    
        override func viewDidLoad() 
            super.viewDidLoad()
              delegate = self
            // Do any additional setup after loading the view.
        


        override func viewWillAppear(_ animated: Bool) 
            super.viewWillAppear(animated)
            let VC1 = ViewController()
            let VC1Navigation = UINavigationController(rootViewController: VC1)
            let icon1 = UITabBarItem(title: "Title", image: UIImage(named: "someImage.png"), selectedImage: UIImage(named: "otherImage.png"))
            VC1.tabBarItem = icon1

            let VC2 = SecondViewController()
            let VC2Navigation = UINavigationController(rootViewController: VC2)
            let icon2 = UITabBarItem(title: "Title", image: UIImage(named: "someImage.png"), selectedImage: UIImage(named: "otherImage.png"))
            VC2.tabBarItem = icon2

            let controllers = [VC1Navigation,VC2Navigation]  //array of the root view controllers displayed by the tab bar interface
            self.viewControllers = controllers
        

        //Delegate methods
        func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool 

            print("Should select viewController: \(viewController.title) ?")
            return true;
        

Step3 - 在 ViewController 中,即选项卡的第一项,做你需要的事情

import UIKit

class ViewController: UIViewController 

    override func viewDidLoad() 
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.view.backgroundColor = #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)
        self.title = "item1"
        print("item 1 loaded")
    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    

Step4-类似SecondViewController,即标签的第二项

import UIKit

class SecondViewController: UIViewController 

    override func viewDidLoad() 
        super.viewDidLoad()

        // Do any additional setup after loading the view.
         self.view.backgroundColor = #colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1)
        self.title = "item2"
        print("item 1 loaded")
        let button = UIButton()
        button.frame = (frame: CGRect(x: self.view.frame.size.width - 60, y: 100, width: 50, height: 50))
        button.backgroundColor = UIColor.red
        button.setTitle("Name your Button ", for: .normal)
        button.addTarget(self, action: #selector(button_Action), for: .touchUpInside)
        self.view.addSubview(button)

        func buttonAction(sender: UIButton!) 
            print("Button tapped")
        
        // Do any additional setup after loading the view.
    

    func button_Action()
    
        let nextViewController = TabViewController(nibName: nil, bundle: nil)
        // and push it onto the 'navigation stack'
        self.navigationController?.pushViewController(nextViewController, animated: true)

    

Step5-你的登录VC:

import UIKit

class LoginViewController: UIViewController 

    override func viewDidLoad() 
        super.viewDidLoad()

        self.view.backgroundColor  = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)

        let button = UIButton()
        button.frame = (frame: CGRect(x: self.view.frame.size.width - 60, y: 100, width: 50, height: 50))
        button.backgroundColor = UIColor.red
        button.setTitle("Name your Button ", for: .normal)
        button.addTarget(self, action: #selector(button_Action), for: .touchUpInside)
        self.view.addSubview(button)

        func buttonAction(sender: UIButton!) 
            print("Button tapped")
        
        // Do any additional setup after loading the view.
    

    func button_Action()
    
        let nextViewController = TabbViewController(nibName: nil, bundle: nil)
        // and push it onto the 'navigation stack'
        self.navigationController?.pushViewController(nextViewController, animated: true)

    

Step6- 另一个控制器,即 abc,我在 SecondViewController 上有按钮来显示子视图的导航和标签栏。按钮将推送此视图控制器。

import UIKit

class TabViewController: UIViewController 

    override func viewDidLoad() 
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)


    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    

【讨论】:

它显示错误“致命错误:在展开可选值时意外发现 nil” 当登录成功后,推送你家 VC 的标签。 在这一行显示同样的错误:self.window! = UIWindow(frame: UIScreen.main.bounds)

以上是关于swift ios 中的视图控制器中的按钮操作和导航不起作用的主要内容,如果未能解决你的问题,请参考以下文章

使用 swift3 为 xib 文件中的按钮提供操作

IOS/Swift:将表视图中的对象传递给详细视图控制器

IOS swift - 如何在点击文本字段时打开新视图控制器后结束文本字段中的编辑

视图中的按钮未单击 ios swift

如何在 ios 中的一个控制器的按钮操作中返回当前的上一个视图

使用我的视图控制器 swift 4 中的按钮使用 UIPageViewController 更改页面