如何使用推送Storyboard Segue为NavigationController [英] How to use push Storyboard Segue for the NavigationController

查看:98
本文介绍了如何使用推送Storyboard Segue为NavigationController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

当我点击客户页面中的添加栏按钮时 - >转到添加客户页面。(使用模态故事板Segue)

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

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

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

一切正常。

问题是我想要后退按钮。我已经知道我可以使用推送 Storyboard Segue,但是当我使用它时,我面临一个错误:

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

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:

CustomerViewController.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

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

This is the code I use in 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:

AddCustomerViewController.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

和AddCustomerViewController.m:

And 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];

}

你能帮帮我怎样才能使用故事板Segue(有后退按钮)?

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

推荐答案

首先,摆脱第二个导航控制器就像每个人都在说的那样。

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

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

Then, in storyboard, connect the "+" button directly to Add Customer View Controller and set the segue as 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.

在prepareForSegue中,

In prepareForSegue,

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

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

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

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

这篇关于如何使用推送Storyboard Segue为NavigationController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆