【问题标题】:How to use push Storyboard Segue for the NavigationController如何为 NavigationController 使用 push Storyboard Segue
【发布时间】:2012-08-05 00:19:23
【问题描述】:

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

当我点击客户页面中的添加栏按钮时 --> 转到添加客户页面。(使用 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<AddCustomerViewControllerDelegate>
    {
        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 <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:

- (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(有后退按钮)吗?

【问题讨论】:

  • 是的,去掉第二个导航控制器。您如何继续添加客户视图控制器?什么样的segue?是模态的吗?
  • 是的,它是 Modal 可以正常工作。

标签: iphone objective-c ios xcode storyboard


【解决方案1】:

首先,像这里大家所说的那样,去掉第二个导航控制器。

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

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

在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];
}

【讨论】:

  • 抱歉,我的回复延迟。是的,您的代码完全可以工作。
【解决方案2】:

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

编辑:

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

【讨论】:

  • 我这样做了,但我也遇到了错误。我会为你发布错误。
  • 是给popViewControllerAnimated的吗?
  • 谢谢,但是如何使用 popViewControllerAnimated?我不知道我在哪里使用了dismissModalViewControllerAnimated!
  • addCustomerViewControllerDidSave 中的第一行。抱歉dismissModalViewControllerAnimateddismissViewControllerAnimated 相同。
  • 这是 popViewControllerAnimated 的新错误: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 0x25bd 0x2525) terminate called throwing an exception(lldb)
【解决方案3】:

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

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

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

【讨论】:

    猜你喜欢
    • 2012-03-12
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 2013-11-04
    • 1970-01-01
    • 2015-02-10
    • 2016-11-18
    相关资源
    最近更新 更多