簡體   English   中英

在iOS中的ViewController中,按鈕操作和導航不起作用

[英]Button action and navigation not working in viewcontroller in swift ios

 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中設置了標簽欄控制器。 但是現在按鈕動作和其他導航在我的視圖控制器中不起作用。 如何在此處設置導航? 請幫忙

我有逐步顯示給您點。 請遵循-

步驟1 -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.
    }

第2步 -創建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;
        }
}

步驟3-在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.
    }
}

步驟4-類似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)

    }}

步驟5-您的登錄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.
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM