UINavigationController没有在模态视图控制器上显示

时间:2013-11-17 14:22:19

标签: ios cocoa-touch uinavigationcontroller modalviewcontroller navigationbar

当用户按下主视图控制器上的按钮时,我以编程方式显示模态视图控制器。我遇到的问题是在没有导航栏的情况下显示模态视图。

我怎么能让酒吧出现?请找到我的代码,让我知道缺少什么或错误:

-(void)appInfoButtonPressed:(id)sender {
infoViewController *iVC=[[infoViewController alloc] init];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:iVC animated:YES completion:nil];
UINavigationController *navBar = [[UINavigationController alloc] initWithRootViewController:iVC];
[self presentViewController:navBar animated:YES completion:nil];

}

同时尝试解除模态视图我打算使用以下行:

[self dismissViewControllerAnimated:YES completion:nil];

我应该在主视图控制器的方法中还是在模态视图控制器本身的方法中包含此行?

2 个答案:

答案 0 :(得分:2)

删除[self presentViewController:iVC animated:YES completion:nil]; 仅用于呈现UINavigationController。

顺便说一句:用“navBar”命名UINavigationController是不合适的。

-(void)appInfoButtonPressed:(id)sender {
    infoViewController *iVC=[[infoViewController alloc] init];
    self.modalPresentationStyle = UIModalPresentationCurrentContext;
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:iVC];
    [self presentViewController:navController animated:YES completion:nil];
}

答案 1 :(得分:0)

快捷键5

 import UIKit

class ListVC: UIViewController {


    // MARK: - Init
    override func viewDidLoad() {
           super.viewDidLoad()

        //Background of the first screen
        view.backgroundColor = .yellow

        //Calling the instance of the navigation controller
        let nav = self.navigationController?.navigationBar

        //Defining the black theme on the navigation controller
        nav?.barStyle = UIBarStyle.black

        //Defining the white characters to make contrast with the black theme on the navigation controller
        nav?.tintColor = UIColor.white

        //Defining the custom color of the title font from navigation controller
        nav?.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.orange]

        //Defining the title of the navigation controller
        nav?.topItem?.title = "List"

        navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: #imageLiteral(resourceName: "AddBtn"), style: .plain, target: self, action: #selector(hello))



//        print(Realm.Configuration.defaultConfiguration.fileURL)

           let realm = try! Realm()

           print(Realm.Configuration.defaultConfiguration.fileURL)

       }


    // MARK: - Selector

    /// A selector function that is called when the 'add' button is pressed on the navigation controller
    @objc func hello() {

        //Instance of the second screen
        let addVC = AddVC()

        //Add the navigationController to the new viewController
        let navController = UINavigationController(rootViewController: addVC)

        //Presenting the second screen modally
        navigationController?.present(navController, animated: true, completion: nil)
    }


}
//Other class
import UIKit

class AddVC: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()

        //Background of the view
        view.backgroundColor = .white

        //Calling the instance of the navigation controller
        let nav = self.navigationController?.navigationBar

        //Initialize the title for the ViewController
        nav?.topItem?.title = "Andrey"

        // Initialize the right bar button item
        navigationItem.rightBarButtonItem = setUpSaveButton()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }

    /// Function that returns the "Save" bar button item
    private func setUpSaveButton() -> UIBarButtonItem {
        let button = UIBarButtonItem(title: "Save", style: .plain, target: self, action: #selector(saveAction))
        button.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.systemBlue],
                                      for: .normal)

        return button
    }

    @objc func saveAction() {
        print("Saving..")
    }
}