xcode 创建带有导航栏和后退按钮的模态视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10841653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:27:11  来源:igfitidea点击:

Create a modal view with navigation bar and back button

iphoneobjective-ciosxcodeuitabbarcontroller

提问by Sergey Grischyov

I want to create a modal view with the navigation item (right view on my screenshot) and I want it to have a 'back button'. My app is TabBar application and I don't want this view to have a tab bar, but I want to load a previous view (left view on my screenshot) with a segue similar to the type "push". I can only create push segue to provide right navigation back to the view on the left, if it's loaded as a modal view, the NavigationBar & TabBar are gone. Any workarounds for this? Thanks in advance!

我想用导航项(屏幕截图上的右视图)创建一个模态视图,并且我希望它有一个“后退按钮”。我的应用程序是 TabBar 应用程序,我不希望此视图具有选项卡栏,但我想加载具有类似于“push”类型的 segue 的前一个视图(屏幕截图上的左视图)。我只能创建 push segue 来提供返回到左侧视图的右导航,如果它作为模式视图加载,则 NavigationBar 和 TabBar 消失了。有什么解决方法吗?提前致谢!

Here's my screenshot

这是我的截图

回答by Justin Paulson

Just put a Navbar on the new view with a bar button item. Create an action for the bar button item by control dragging from the bar button item to the .h of the view controller. You can then use a delegate and protocol method to tell the first controller when the button has been pressed and have it use [self dismissModalViewControllerAnimated:YES];

只需在带有栏按钮项的新视图上放置一个导航栏。通过控件从栏按钮项拖动到视图控制器的 .h 为栏按钮项创建操作。然后,您可以使用委托和协议方法来告诉第一个控制器何时按下按钮并让它使用[self dismissModalViewControllerAnimated:YES];

So in your second view create a protocol with a method done, like this:

因此,在您的第二个视图中,创建一个带有 done 方法的协议,如下所示:

@protocol SecondViewControllerDelegate <NSObject>

-(void) done;

@end

@interface SecondViewController : UIViewController {
    ...
    id delegate;
}

...

@property (nonatomic, assign) id<SecondViewControllerDelegate> delegate;

-(IBAction)done:(id)sender;  //this will be linked to your nav bar button.
@end

then in your action from your button call this:

然后在您的按钮操作中调用:

-(IBAction)done:(id)sender{
   [self.delegate done];
}

Your first view controller will need to implement the protocol <SecondViewControllerDelegate>

你的第一个视图控制器需要实现协议 <SecondViewControllerDelegate>

then in your first view controller, set it up as a delegate for your second view controller before you segue.

然后在您的第一个视图控制器中,在继续之前将其设置为第二个视图控制器的委托。

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"Second View Modal Segue Identifier"])
    {
        SecondViewController *viewController = segue.destinationViewController;
        viewController.delegate = self;
    }
}

lastly, catch the done call from the delegate in your first view controller:

最后,在您的第一个视图控制器中从委托中捕获 done 调用:

-(void) done
{
    [self dismissModalViewControllerAnimated:YES];
}

That's how I have done it. If you don't have a lot of experience with protocols and delegates it may seem confusing at first, but it has worked well for me.

我就是这样做的。如果您对协议和委托没有很多经验,一开始可能会让人感到困惑,但它对我来说效果很好。

回答by Mundi

You will need to wrap your right hand side view controller in a new navigation controller. In IB, select it and choose the menu item Editor -> Embed In -> Navigation Controllerand IB will show a nav bar which you can customize to your heart's content.

您需要将右手边的视图控制器包装在一个新的导航控制器中。在 IB 中,选择它并选择菜单项Editor -> Embed In -> Navigation Controller,IB 将显示一个导航栏,您可以根据自己的喜好对其进行自定义。