如何显示所有页面导航栏按钮

时间:2017-03-16 12:03:03

标签: ios swift swift3 uinavigationcontroller

我正在使用电子商务应用程序,我想显示所有页面篮子按钮..对于用户点击任何页面用户能够检查所有产品添加列表然后她能够从那里检查..

我的导航栏以编程方式生成:

  let viewController = DishViewController()
  viewController.shopCategory = shopCategory
  let navigationController = UINavigationController.init(rootViewController: viewController)

  self.present(navigationController, animated: true, completion: nil) 

目前我正在为每个页面显示篮子viewDidload():

   func setupNavigationBar(){

        // back button without title
        self.navigationController?.navigationBar.topItem?.title = ""

        //set titile
        self.navigationItem.title = "Your Order"

        let rightButton = UIBarButtonItem(image: #imageLiteral(resourceName: "busket"), style: .plain, target: nil, action: nil)

        //show the Edit button item
        self.navigationItem.rightBarButtonItem = rightButton


    }

2 个答案:

答案 0 :(得分:1)

您可以创建public String appNameAsNullOfAppInfo() { String appName = null; String appVersion = "1.1"; String appKey = "testkey"; long timestamp = 1487076718; AppInfo obj = new AppInfo(appName, appVersion, appKey, timestamp); Gson gson = new Gson(); // searialize into string and return return gson.toJson(obj); } extension并实现他们的功能。

UIViewController

并在extension UIViewController { func setupNavigationBar(title: String) { // back button without title self.navigationController?.navigationBar.topItem?.title = "" //set titile self.navigationItem.title = title let rightButton = UIBarButtonItem(image: #imageLiteral(resourceName: "busket"), style: .plain, target: nil, action: nil) //show the Edit button item self.navigationItem.rightBarButtonItem = rightButton } } 方法中调用

ViewControllers's ViewDidLoad

希望这能解决你的问题。

答案 1 :(得分:0)

您可以创建一个BaseViewController并从中继承视图控制器。 在你的BaseViewController上你有:

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationController?.navigationBar.topItem?.title = ""

    //set titile
    self.navigationItem.title = "Your Order"

    let rightButton = UIBarButtonItem(image: #imageLiteral(resourceName: "busket"), style: .plain, target: nil, action: nil)

    //show the Edit button item
    self.navigationItem.rightBarButtonItem = rightButton

}

然后所有视图控制器都有按钮,只要他们调用super.viewDidLoad()当然