PresentModalViewController 在下一个视图上不显示导航栏

发布于 2024-11-09 02:08:37 字数 276 浏览 4 评论 0原文

您好,我正在工具栏上使用一个选项卡栏按钮,该按钮将显示带有表格视图的下一个视图,这是我的代码,

[self presentModalViewController:self.navigationController
                            animated:YES];

我的问题是,当我单击此选项卡栏按钮时,它将显示带有表格视图的下一个视图,但不显示导航栏。因此我无法在 tableView 中执行删除操作。

如何解决问题?

Hello I am using One tab bar button on toolbar , this button will show next view with table view ,Here is my code

[self presentModalViewController:self.navigationController
                            animated:YES];

my problem is that when I click this tab bar button it will showing next view with tableview but not navigation bar. because of this i am unable to perform delete operation in tableView.

How to solve the issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

愿得七秒忆 2024-11-16 02:09:03

斯威夫特 5.1
在全屏模式下呈现带有导航栏和工具栏的 ViewController。如果您不将注释中标记的行放在工具栏上,则工具栏永远不会显示出来。

let sb = UIStoryboard(name: "retail_mainScreen", bundle: nil)
        guard let mainVC = sb.instantiateViewController(withIdentifier: "mainScreen") as? retail_mainGest else { return }
        let navController = UINavigationController(rootViewController: mainVC)
        navController.isToolbarHidden = false //<--- remember this or toolbar will not appear
        navController.modalPresentationStyle = .fullScreen

         sender.vista.present(navController, animated: true, completion: nil)

Swift 5.1
Presenting ViewController with navigation bar AND toolbar in fullscreen mode. If you don't put the row marked on comment the toolbar never show itself.

let sb = UIStoryboard(name: "retail_mainScreen", bundle: nil)
        guard let mainVC = sb.instantiateViewController(withIdentifier: "mainScreen") as? retail_mainGest else { return }
        let navController = UINavigationController(rootViewController: mainVC)
        navController.isToolbarHidden = false //<--- remember this or toolbar will not appear
        navController.modalPresentationStyle = .fullScreen

         sender.vista.present(navController, animated: true, completion: nil)
素罗衫 2024-11-16 02:09:00

斯威夫特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..")
    }
}

Swift 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..")
    }
}

悲凉≈ 2024-11-16 02:08:57

使用栏按钮将导航栏作为子视图添加到新视图中。

试试这个

-(IBAction) editClick:(id)sender
{
    [tableView setEditing:![tableView isEditing]  animated:YES];
}

Add navigation bar as sub view to the new view with bar button.

Try this

-(IBAction) editClick:(id)sender
{
    [tableView setEditing:![tableView isEditing]  animated:YES];
}
心碎的声音 2024-11-16 02:08:54

如果您正在使用导航控制器,请像这样使用

[self.navigationController pushViewController:nextController animated:YES];

if you are using navigationcontroller use like this

[self.navigationController pushViewController:nextController animated:YES];
疧_╮線 2024-11-16 02:08:50

那是因为您正在使用 Modal 来引入新的视图控制器。

模态添加/呈现的视图控制器不会添加到导航控制器堆栈中

That is because you are using Modal to bring the new view controller.

Modally added/presented view controller will not be added to the navigation controller stack

世界如花海般美丽 2024-11-16 02:08:46

此代码在 classA VC 中的按钮单击事件上调用

ClassBVC* bVc = [[ClassBVC alloc] initWithNibName:@"ClassBVC" bundle:nil];
     UINavigationController* tempNavCon = [[UINavigationController alloc]    initWithRootViewController:bVc];
    [self presentModalViewController:tempNavCon animated:YES];
    [tempNavCon release];
    [bVc release];
    bVc = nil

在视图中的 BVC 类中,确实加载了您创建的 UIbarbutton 项目,例如:

UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClicked:)];
    [barButton setTitle:@"Back"];
    [self.navigationItem setLeftBarButtonItem:barButton];
    [barButton release];

在 buttonClickedMethod 中,只需将模型控制器解散为:

-(void)backButtonClicked:(id)sender
{
    [self dismissModalViewControllerAnimated:YES];
}

This code is called on button click event in classA VC:

ClassBVC* bVc = [[ClassBVC alloc] initWithNibName:@"ClassBVC" bundle:nil];
     UINavigationController* tempNavCon = [[UINavigationController alloc]    initWithRootViewController:bVc];
    [self presentModalViewController:tempNavCon animated:YES];
    [tempNavCon release];
    [bVc release];
    bVc = nil

;

and in class BVC in view did load you make an UIbarbutton item e.g:

UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClicked:)];
    [barButton setTitle:@"Back"];
    [self.navigationItem setLeftBarButtonItem:barButton];
    [barButton release];

And in buttonClickedMethod simply dismiss the the model controller as:

-(void)backButtonClicked:(id)sender
{
    [self dismissModalViewControllerAnimated:YES];
}
夏有森光若流苏 2024-11-16 02:08:43

如果您在下一个类上找不到 UINavigationBar ,则意味着它没有导航控制器,因此在推送它之前将 UINavigationController 添加到您的下一个视图。

尝试这样:

NextViewController *nextViewController=[[NextViewController alloc]initWithNibName:@"NextViewController" bundle:nil];
UINavigationController *navBar=[[UINavigationController alloc]initWithRootViewController:nextViewController];
[self.navigationController presentModalViewController:navBar animated:YES];
[navBar release];
[nextViewController release];

请参阅此stackoverflow问题以获取编辑选项。

您只需轻松地将按钮添加到导航栏即可

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(editTable)] autorelease];

-(void)editTable{
[tableView setEditing: YES animated: YES];
}

一切顺利。

If you dont find the UINavigationBar on the next class means , it does not have a navigation controller, so before pushing it add a UINavigationController to your next view.

Try like this:

NextViewController *nextViewController=[[NextViewController alloc]initWithNibName:@"NextViewController" bundle:nil];
UINavigationController *navBar=[[UINavigationController alloc]initWithRootViewController:nextViewController];
[self.navigationController presentModalViewController:navBar animated:YES];
[navBar release];
[nextViewController release];

see this stackoverflow question for edit option.

You can simply add a button to navigation bar with ease

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(editTable)] autorelease];

-(void)editTable{
[tableView setEditing: YES animated: YES];
}

All the best.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文