segue.identifier即将上映(null)

时间:2014-01-31 20:27:51

标签: objective-c segue

我是一个noobie试图在segue(prepareForSegue)之前和segue(unwindToList)之后想出为什么segue.identifier出现(null)。我做错了什么,或者我错过了什么?我已经为所涉及的场景(在segue属性中)正确设置了segue标识符。 (这里的另一位成员善意帮助,但无济于事。)

以下是代码:

在AddToDoItemViewController.m(展开源)中:

    - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"segue identifier sender: %@", segue.identifier);//comes up (null)...??

    self.toDoItem = [[ToDoItem alloc] init];//this code works fine if not conditional in unwind scene
    if (sender != self.doneButton) return;//this code works fine if not conditional in unwind scene
    if (self.textField.text.length > 0 ) {//this code works fine if not conditional in unwind scene
        self.toDoItem.itemName = self.textField.text;//this code works fine if not conditional in unwind scene
        self.toDoItem.completed = NO;//this code works fine if not conditional in unwind scene
        self.toDoItem.flag = 1;//this code works fine if not conditional in unwind scene
    }
}

在ToDoListViewController.m(展开目的地):

    - (IBAction)unwindToList:(UIStoryboardSegue *)segue
{
    ToDoListViewController *source = [segue sourceViewController];
    NSLog(@"segue from: %@", source);//no problem here... correct scene is logged
    NSLog(@"segue identifier: %@", segue.identifier);//comes up (null)...??
    if ([segue.identifier isEqualToString:@"addItemScene"]) {//condition is always FALSE...??
        if (source.toDoItem.flag == 1) {
        ToDoItem *item = source.toDoItem;
        if (item != nil) {
            [self.toDoItems addObject:item];
            [self.tableView reloadData];
            }
        }
    }
    if ([segue.identifier isEqualToString:@"(a different scene"]) {
        //do something else
    }
}

1 个答案:

答案 0 :(得分:0)

以下是问题解决部分的解决方案/解决方法:

假设我有两个视图控制器类,TopViewController和ModalDialogViewController。并且在故事板中创建了三个从TopViewController到ModalDialogViewController的segue。

在ModalDialogViewController.h中,我创建了一个枚举类型:

typedef enum { MD_aboutDialog, MD_helloWorldDialog, MD_anotherDialog } MD_dialogType;

然后我为它添加一个属性:

@interface ModalDialogViewController : UIViewController
@property (nonatomic) EL_dialogType dialogType;
@end

每个对话框类型都与在故事板中输入的segue标识符相关联;我在ModalDialogViewController.h中为它们制作了pound-defined:

#define MD_segueAboutDialogId       @"AboutDialogSegueId"
#define MD_segueHelloWorldDialogId  @"HelloWorldDialogSegueId"
#define MD_segueAnotherDialogId     @"AnotherDialogSegueId"

在topViewController中,我有prepareForSegue和“从对话中解放”方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    MD_dialogType dialogType;

    if ([segue.identifier isEqualToString:MD_segueAboutDialogId]) {
        dialogType = MD_aboutDialog;
    }
    else if ([segue.identifier isEqualToString:MD_segueHelloWorldDialogId]) {
        dialogType = MD_helloWorldDialog;
    }
    else if ([segue.identifier isEqualToString:MD_segueAnotherDialogId]) {
        dialogType = MD_anotherDialog;
    }
    else
        return; // shouldn't get here

    ModalDialogViewController *dialogController = (ModalViewController *)segue.destinationViewController;
    dialogController.dialogType = dialogType;

   // Do whatever other setup you need
}

同时,当你放松时:

- (IBAction)unwindFromDialogOnOK:(UIStoryboardSegue *)segue
{
    ModalDialogViewController *dialogController = (ModalViewController *)segue.sourceViewController;

    if (dialogController.dialogType == MD_aboutDialog) {
        // Do Something
    }
    else if (dialogController.dialogType == MD_helloWorldDialog) {
        // Do Something Else
    }
    else if (dialogController.dialogType == MD_anotherDialog) {
        // Do Something Else Entirely
}

- (IBAction)unwindFromDialogOnCancel:(UIStoryboardSegue *)segue
{
    ModalViewController *dialogController = (ModalViewController *)segue.sourceViewController;

    if (dialogController.dialogType == MD_anotherDialog) {
        // Take any action needed if they canceled
    }
}

当然,只有在prepareForSegue方法中您的segue标识符为非null时,此解决方案才有效。并且可能有一个原因我们的segues在展开时具有空标识符 - 如果这已经解决,则没有理由这样做。但我们很高兴知道我们可以完全访问这两个控制器,以便我们可以使用它们的属性来传递信息。

如果你仍然无法在prepareForSegue中获得非null的segue标识符,那么在TopViewController中有一个相应的dialogType属性,然后你可以使用它在ModalDialogViewController中设置相应的dialogType,因为你可以访问它们控制器。