ios 如何为 NavigationController 使用 push Storyboard Segue

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11812996/
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-08-30 19:35:26  来源:igfitidea点击:

How to use push Storyboard Segue for the NavigationController

iphoneobjective-ciosxcodestoryboard

提问by Ali

I am new in iOS development. I created an App using Storyboard to navigate to different pages.

我是 iOS 开发的新手。我使用 Storyboard 创建了一个应用程序来导航到不同的页面。

enter image description here

在此处输入图片说明

When I click on the Add BarButton in Customer page --> go to the Add Customer Page.(using ModalStoryboard Segue)

当我单击“客户”页面中的“添加 BarButton”时 --> 转到“添加客户”页面。(使用ModalStoryboard Segue)

There, when I enter username and password and click on save button --> come back to Customer page.

在那里,当我输入用户名和密码并单击保存按钮时 --> 返回客户页面。

And everything works fine.

一切正常。

The problem is I want to have back buttonin Add Customer page. I already know that I can use PushStoryboard Segue, but when I use it I face with an error:

问题是我想back button在添加客户页面中添加。我已经知道我可以使用PushStoryboard Segue,但是当我使用它时,我遇到了一个错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'
*** First throw call stack:
(0x13cb022 0x155ccd6 0xeff1b 0xefa24 0x44bde6 0x4404d0 0x13cce99 0x1814e 0x256a0e 0x13cce99 0x1814e 0x180e6 0xbeade 0xbefa7 0xbe266 0x3d3c0 0x3d5e6 0x23dc4 0x17634 0x12b5ef5 0x139f195 0x1303ff2 0x13028da 0x1301d84 0x1301c9b 0x12b47d8 0x12b488a 0x15626 0x25ed 0x2555)
terminate called throwing an exception(lldb) 

CustomerViewController.h:

客户视图控制器.h:

#import "AddCustomerViewController.h"
    @interface CustomerViewController : UITableViewController<AddCustomerViewControllerDelegate>
    {
        IBOutlet UITableView *tableview;
        NSMutableArray *customers;
    }

    @property(nonatomic,retain)IBOutlet UITableView *tableview;
    @property(nonatomic,retain)NSMutableArray *customers;

    @end

This is the code I use in CustomerViewController.m:

这是我在 CustomerViewController.m 中使用的代码:

self.customers = [[NSMutableArray alloc]init]; 

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"AddCustomer"])
    {
        UINavigationController *navigationController = segue.destinationViewController;
        AddCustomerViewController *addCustomerViewController = [[navigationController viewControllers] objectAtIndex:0];
        addCustomerViewController.delegate = self;
    }
}

-(void) addCustomerViewControllerDidSave: (AddCustomerViewController *) Controller newCustomer: (Customer *) customer
{
    [self dismissViewControllerAnimated: YES completion:NULL];
    [self.customers addObject:customer];
    [self.tableview reloadData];
}

AddCustomerViewController.h:

添加CustomerViewController.h:

#import "Customer.h"

@class AddCustomerViewController;

@protocol AddCustomerViewControllerDelegate <NSObject>

-(void) addCustomerViewControllerDidSave: (AddCustomerViewController *) Controller newCustomer: (Customer *) customer;

@end

@interface AddCustomerViewController : UITableViewController
{

}

@property (nonatomic, weak) id <AddCustomerViewControllerDelegate> delegate;

@property (nonatomic, strong) IBOutlet UITextField *firstnameTxt;
@property (nonatomic, strong) IBOutlet UITextField *lastnameTxt;

- (IBAction)save:(id)sender;

@end

And AddCustomerViewController.m:

并添加CustomerViewController.m:

- (void)save:(id)sender 
{
    NSLog(@"Save");
    Customer *newCustomer = [[Customer alloc]init];
    newCustomer.firstname = self.firstnameTxt.text;
    newCustomer.lastname = self.lastnameTxt.text;
    [self.delegate addCustomerViewControllerDidSave:self newCustomer:newCustomer];

}

Can you help me how can I use PushStoryboard Segue (to have back button) ?

你能帮我如何使用PushStoryboard Segue(有后退按钮)?

回答by Rick

First of all, get rid of the second navigation controller like what everybody here are saying.

首先,像这里每个人所说的那样,摆脱第二个导航控制器。

Then, in storyboard, connect the "+" button directly to Add Customer View Controller and set the segue as push.

然后,在 storyboard 中,将“+”按钮直接连接到 Add Customer View Controller 并将 segue 设置为 push。

Next, click on the navigation bar of Customer view controller, in the attributes inspector where you should have defined the title ("Customer") for this view, there should be a "Back Button" row. Type "Back" and you will get a Back button in Add Customer view controller.

接下来,单击 Customer 视图控制器的导航栏,在您应该为此视图定义标题(“Customer”)的属性检查器中,应该有一个“Back Button”行。键入“返回”,您将在“添加客户”视图控制器中获得一个“返回”按钮。

In prepareForSegue,

在 prepareForSegue 中,

if([segue.identifier isEqualToString:@"AddCustomer"])
{    
    AddCustomerViewController *addCustomerViewController = segue.destinationViewController;
    addCustomerViewController.delegate = self;
}

To close Add Customer View controller, use popViewControllerAnimated as follows:

要关闭添加客户视图控制器,请使用 popViewControllerAnimated 如下:

-(void) addCustomerViewControllerDidSave: (AddCustomerViewController *) Controller newCustomer: (Customer *) customer
{
    [self.customers addObject:customer];
    [self.tableview reloadData];
    [self.navigationController popViewControllerAnimated:YES];
}

回答by danqing

You only need one navigation controller. Try delete the second navigation controller and set the segue from your first table directly to your second one.

您只需要一个导航控制器。尝试删除第二个导航控制器并将第一个表中的 segue 直接设置为第二个。

Edit:

编辑:

I took a more careful look and I think you should use popViewControllerAnimatedrather than dismissModalViewControllerAnimated. The latter is for modal, the former is for push.

我仔细看了看,我认为你应该使用popViewControllerAnimated而不是dismissModalViewControllerAnimated. 后者用于模态,前者用于推送。

回答by Jody Hagins

You can't push a navigation controller. I doubt you need the second one in that scenario, but sometimes that usage can come in handy.

您不能推送导航控制器。我怀疑在那种情况下您是否需要第二个,但有时这种用法会派上用场。

However, you need to set the style to Modal (or Custom - and provide your own segue subclass implementation).

但是,您需要将样式设置为 Modal(或自定义 - 并提供您自己的 segue 子类实现)。

When done, call dismissViewControllerAnimated:completion: to "pop" the navigation controller.

完成后,调用dismissViewControllerAnimated:completion: 来“弹出”导航控制器。