从子类WebView更改Window的元素可见性

Tom*_* B. 6 xcode cocoa objective-c webview

我正在尝试制作一个非常简单的应用程序.它只是一个3页的超级简单的Web浏览器.3 webview,2随时隐藏.

我将WebView子类化为能够在聚焦时捕获击键事件.这部分有效.

现在,当我按下CMD + 1,CMD + 2,CMD + 3(1将显示第一个webview,隐藏2个其他等)时,我需要回调回家并更改其他WebViews的可见性.

我试着考虑如何使用代表来实现我的目标,但是我缺乏知识使我无法完成这个简单的应用程序.

我也听说过NSNotification,我的WebView可以发送一个通知,我的Window可以捕获并改变其孩子的可见性,但我不知道如何实现.

有人能指出我正确的方向吗?

TLDR; 例如,当WebView捕获CMD + 1时,我希望能够调用其他WebView中的方法来隐藏它们.

感谢,并有一个愉快的一天!

Ger*_*d K 1

使用通知:假设您在击键的地方有一个 NSString 对象,其中包含一些 ID 来标识所需的 WebView(例如@"1"@"2"等等),并且每个 Web 视图都有一个viewID属性。因此,在收到击键的地方,您需要添加:

[[NSNotificationCenter defaultCenter]
    postNotificationName:@"ChangeMyActiveWebView"
    object:newViewID  // <- contains string to identify the desired web view
];
Run Code Online (Sandbox Code Playgroud)

在初始化 Web 视图的地方(例如 -awakeFromNib 或 -init),您可以添加以下代码:

[[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(switchViewNotification:)
    name:@"ChangeMyActiveWebView"
    object:nil  // Means any object
];
Run Code Online (Sandbox Code Playgroud)

然后实现-switchViewNotification:方法:

- (void)switchViewNotification:(NSNotification *)aNotification {

    NSString    *newViewID=[aNotification object];

    if([self.viewID isEqualToString:newViewID])
    {
        // show this web view
    }
    else
    {
        // hide this web view
    }
}
Run Code Online (Sandbox Code Playgroud)

最后一步:当 Web 视图消失时,您需要删除观察者,因此将其添加到您的-dealloc方法中:

[[NSNotificationCenter defaultCenter]removeObserver:self];
Run Code Online (Sandbox Code Playgroud)

应该可以做到这一点。