自定义代表与模态表单不起作用

时间:2015-05-31 05:05:11

标签: ios objective-c delegates

我已经阅读了之前的大多数相关帖子,但是虽然我已经正确地遵循了它们(据我所知),但我无法触发下面代码的委托方法。

目标:ModalView生成一个字符串*SQL_String。按DONE以关闭ModalView并在父视图中触发委托方法以获取*SQL_String

  

SearchModalViewController.h

@protocol SearchControllerDelegate
- (void)didDismissModalView:(NSString *)SQL_String;
@end

@interface SearchModalViewController : UIViewController
@property (nonatomic, assign) id <SearchControllerDelegate> searchDelegate;
- (IBAction)handleDone:(id)sender;
  

SearchModalViewController.m

@interface SearchModalViewController ()
@end

@implementation SearchModalViewController
@synthesize searchDelegate;

- (IBAction)handleDone:(id)sender {
    [self dismissView:sender];
}

- (void)dismissView:(id)sender {
    [searchDelegate didDismissModalView:@"Test"];
    [self dismissViewControllerAnimated:YES completion:nil];
}
  

DetailViewController.m(我的父视图控制器)

@interface DetailViewController () <SearchControllerDelegate>
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    SearchModalViewController *searchModal = [[SearchModalViewController alloc] init];
    searchModal.searchDelegate = self;
}

问题:

  

以下委托方法未被触发。

- (void)didDismissModalView:(NSString *)SQL_String {
    [self dismissViewControllerAnimated:YES completion:nil];
    NSLog(@"The string = %@", SQL_String);
}

知道我做错了吗?

编辑:谢谢你们。通过快速建议,我可以通过添加以下代码而不是之前的IB连接来关闭它。

- (IBAction)showSearchModal:(id)sender {
    SearchModalViewController *searchModal = [self.storyboard instantiateViewControllerWithIdentifier:@"search"];
    searchModal.searchDelegate = self;
    searchModal.modalPresentationStyle = UIModalPresentationFormSheet;
    searchModal.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:searchModal animated:YES completion:nil];
}

3 个答案:

答案 0 :(得分:1)

您需要在呈现SearchModalViewController时设置委托。您的代码目前无法运行的原因是因为模态视图控制器的委托是零。

<强>更新

您可以在prepareForSegue:sender:

中设置委托
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)__unused sender
{
    if ([[segue identifier] isEqualToString:@"modalSearch"])
    {
        SearchModalViewController *controller = (SearchModalViewController *)[segue destinationViewController];
        controller.delegate = self;
    }
}

答案 1 :(得分:1)

首先,确保dismissView:的{​​{1}}被触发。

其次,请确保SearchModalViewController方法中的searchDelegate不是dismissView:

答案 2 :(得分:1)

这就是

更改DetailViewController.m

var dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
     Frame.Navigate(typeof(BasicPage1), abc);
});

它会起作用。