我想从一个名为InputUIViewController的UIViewController返回一个NSString *
到先前的名为CallerUIViewController的UIViewController,后者启动了InputUIViewController。我想在InputUIViewController调用之前或之时这样做:[self dismissModelViewControllerAnimated:YES];
有标准的方法吗?
最佳答案
执行此操作的标准方法是使用委托。
在您的InputViewController中,添加一个新的委托协议,并为您的委托添加一个属性。
然后在您的CallerUIViewController中实现委托。然后,在关闭模态视图控制器之前,您可以回调给委托。
因此,您的InputViewController可能看起来像这样:
@protocol InputViewControllerDelegate;
@interface InputViewControllerDelegate : UIViewController {
}
@property (nonatomic, assign) id <InputViewControllerDelegate> delegate;
@end
@protocol InputViewControllerDelegate
- (void)didFinishWithInputView:(NSString *)stringValue;
@end
消除模态视图的方法如下所示:
-(void)dismissSelf
{
[self.delegate didFinishWithInputView:@"MY STRING VALUE"];
[self dismissModalViewControllerAnimated:YES];
}
然后,在CallerUIViewController中,您将实现InputViewControllerDelegate和didFinishWithInputView方法。
CallerUIViewController标头类似于:
@interface CallerUIViewController : UIViewController <InputViewControllerDelegate> {
}
并且您的didFinishWithInputView方法将实现为:
- (void)didFinishWithInputView:(NSString *)stringValue
{
// This method will be called by the InputViewController just before it is dismissed
}
在您呈现InputViewController之前,您需要将委托设置为self。
-(void)showInputViewController
{
InputViewController *inputVC = [[InputViewController alloc] init];
inputVC.delegate = self;
[self presentModalViewController:inputVC animated:YES];
[inputVC release];
}