使用导航栏和后退按钮创建模态视图 [英] Create a modal view with navigation bar and back button
问题描述
我想用导航项创建一个模态视图(我的截图上的右视图),我希望它有一个'后退按钮'。我的应用程序是TabBar应用程序,我不希望此视图有一个标签栏,但我想加载一个类似于push类型的segue的前一个视图(我的屏幕截图上的左视图)。
我只能创建push segue以提供正确的导航回到左边的视图,如果它作为模态视图加载,NavigationBar& TabBar消失了。这有什么变通方法吗?
提前致谢!
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!
推荐答案
只需在新视图上放置一个带有条形按钮项目的导航栏。通过控制从条形按钮项拖动到视图控制器的.h,为条形按钮项创建操作。然后,您可以使用委托和协议方法告诉第一个控制器何时按下按钮并使用 [self dismissModalViewControllerAnimated:YES];
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];
所以在你的第二个视图中创建一个完成方法的协议,如下所示:
So in your second view create a protocol with a method done, like this:
@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];
}
您的第一个视图控制器需要实现协议 < SecondViewControllerDelegate>
Your first view controller will need to implement the protocol <SecondViewControllerDelegate>
然后在您的第一个视图控制器中,将其设置为第二个视图控制器的委托,然后再进行segue。
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:
-(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.
这篇关于使用导航栏和后退按钮创建模态视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!