如何使用推送Storyboard Segue为NavigationController

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

在此处输入图像描述

当我点击Customer页面中的Add BarButton时 – >转到Add Customer Page。(使用Modal Storyboard Segue)

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

一切正常。

问题是我想在添加客户页面中有back button 。 我已经知道我可以使用Push Storyboard 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:

 #import "AddCustomerViewController.h" @interface CustomerViewController : UITableViewController { IBOutlet UITableView *tableview; NSMutableArray *customers; } @property(nonatomic,retain)IBOutlet UITableView *tableview; @property(nonatomic,retain)NSMutableArray *customers; @end 

这是我在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:

 #import "Customer.h" @class AddCustomerViewController; @protocol AddCustomerViewControllerDelegate  -(void) addCustomerViewControllerDidSave: (AddCustomerViewController *) Controller newCustomer: (Customer *) customer; @end @interface AddCustomerViewController : UITableViewController { } @property (nonatomic, weak) id  delegate; @property (nonatomic, strong) IBOutlet UITextField *firstnameTxt; @property (nonatomic, strong) IBOutlet UITextField *lastnameTxt; - (IBAction)save:(id)sender; @end 

和AddCustomerViewController.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]; } 

你能帮我解决一下如何使用Push Storyboard Segue(有后退按钮)吗?

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

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

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

在prepareForSegue中,

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

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

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

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

编辑:

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

你无法推动导航控制器。 我怀疑你在这种情况下需要第二个,但有时这种用法可以派上用场。

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

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