【发布时间】:2016-09-08 13:22:55
【问题描述】:
我有一个 Swift 视图控制器,它定义了一个应该在 Objective-C 视图控制器中遵循的协议:
ChildViewController.swift
@objc public protocol ChildViewControllerDelegate {
func closeChild();
}
@objc public class ChildViewController: UIViewController{
var delegate: ChildViewControllerDelegate?
@IBAction func childCloseButton(sender: UIButton) {
delegate?.closeChild()
}
}
MainViewController.m
@interface MainViewController () <ChildViewControllerDelegate>
@end
@implementation MainViewController
- (void)viewDidAppear:(BOOL)animated {
// creating frame for child and placing it on the bottom of the screen
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
CGFloat yOffset = self.view.frame.origin.y;
CGFloat childHeight = (8/15) * screenWidth;
CGRect child = CGRectMake(0, screenHeight - yOffset - childHeight, screenWidth, childHeight);
// adding storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"child" bundle:nil];
ChildViewController *childVC = [storyboard instantiateViewControllerWithIdentifier:@"childViewController"];
childVC.view.frame = childBox;
[self addChildViewController: childVC];
[self.view addSubview:childVC.view];
}
- (void) closeChild {
NSLog(@"Why doesn't this get trigger???");
}
@end
当我单击 UI 上的“childCloseButton”时,MainViewController.m 中的“closeChild”方法似乎没有被调用。通过调试器,我发现“delegate”属性 ChildViewController.swift 设置为 nil,但我不明白为什么。谁能告诉我我错过了什么?
【问题讨论】:
-
发布您添加子视图控制器的代码=
-
你在哪里设置了代理?
-
你还没有设置委托属性,这就是为什么它的 nil 并且没有被触发
-
你能告诉我在哪里设置委托属性吗?我以为它被设置在 ChildViewController 类主体的第一行。
标签: ios objective-c swift protocols viewcontroller