2012-08-05 73 views
3

我是iOS開發新手。我使用Storyboard創建了一個App來導航到不同的頁面。如何使用push Storyboard Segue for NavigationController

enter image description here

當我點擊添加BarButton客戶頁面 - >轉到添加客戶頁面(使用Modal故事板Segue公司)

在那裏,當我輸入用戶名和密碼,並點擊保存按鈕 - >返回客戶頁面。

一切工作正常。

問題是我想在添加客戶頁面中有back button。我已經知道我可以使用Push故事板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故事板Segue公司(有後退按鈕)?

+0

是,擺脫第二導航控制器。你如何繼續添加客戶視圖控制器?什麼樣的賽格?這是一種模式嗎? – 2012-08-05 01:12:46

+0

是的,它是莫代爾,它工作正常。 – Ali 2012-08-05 08:51:15

回答

8

首先,擺脫第二個導航控制器,就像大家在這裏說的一樣。

然後,在故事板中,直接將「+」按鈕連接到添加客戶視圖控制器並將搜索設置爲推送。

接下來,單擊客戶視圖控制器的導航欄,在應該爲此視圖定義標題(「客戶」)的屬性檢查器中,應該有一個「後退按鈕」行。鍵入「返回」,您將在添加客戶視圖控制器中獲得後退按鈕。

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

對不起,我遲遲不回覆。是的,你的代碼完全正常工作。 – Ali 2012-08-15 22:16:26

3

您只需要一個導航控制器。嘗試刪除第二個導航控制器,並將第一個表中的segue直接設置爲第二個。

編輯:

我花了謹慎看多,我認爲你應該使用popViewControllerAnimated而非dismissModalViewControllerAnimated。後者用於模態,前者用於推動。

+0

我做到了這一點,但我也有這樣的錯誤。我會爲你發佈錯誤。 – Ali 2012-08-05 00:23:23

+0

編輯。嘗試如果這將工作。 – aforaudrey 2012-08-05 00:29:05

+0

是用於'popViewControllerAnimated'嗎? – aforaudrey 2012-08-05 00:33:30

0

您無法推動導航控制器。我懷疑你在這種情況下需要第二個,但有時這種用法可以派上用場。

但是,您需要將樣式設置爲Modal(或自定義 - 並提供您自己的Segue子類實現)。

完成後,調用dismissViewControllerAnimated:completion:以「彈出」導航控制器。

相關問題