5

This seems to be the patten used throughout Apples applications; Creation of a new record is done through a modal View which needs to be saved or canceled to continue, and editing a record is done through a view pushed onto the navigation stack.

It doesn't seem right to be basically duplicating my ViewController for 'add' and 'edit' but there are several differences in how pushed and modal ViewControllers work which complicate things.

How should I be doing this so it can cover both bases?

-

Differences include.

When pushed onto the stack the navBar appears at the top of the View and can be configured to contain the cancel/save buttons. When presented modally this is not the case so to duplicate the interface a toolbar needs to be created separately and close/save buttons added to this instead.

When dismissing a pushed view we send a message to the navigation controller [self.navigationController popViewControllerAnimated:YES];, when dismissing a modal view we send a message to self [self dismissModalViewControllerAnimated:YES];

4

1 回答 1

0

您可以在 InterfaceBuilder 中添加 UIToolbar,然后当 self.navigationController 不为零时将其隐藏在 viewDidLoad 中。

至于解雇,你可以有类似的东西:

- (void)didCancel {
    [self.navigationController popViewControllerAnimated:YES] || [self dismissModalViewControllerAnimated:YES];
}

如果您的视图控制器是导航控件的一部分,这将短路,否则使用dismissModalViewControllerAnimated。

这应该适用于您的取消按钮。对于您的保存按钮,调用某种委托方法很有用,例如:

- (void)didSave {
    // do your saving juju here
    if([self.delegate respondsToSelector:@selector(viewController:didSave:]) {
        [self.delegate viewController:self didSave:whatJustGotSaved];
    }
    [self.navigationController popViewControllerAnimated:YES]; // noop if currently modal
}

然后,在委托的实现中,您可以放置​​:

- (void)viewController:(UIViewController*)viewController didSave:(NSObject*)whatJustGotSaved {
    // do stuff with parameters
    [self.modalViewController dismissModalViewControllerAnimated:YES]; // noop if not modal
}
于 2012-04-27T12:32:23.567 回答