Skip to content

Commit c085184

Browse files
committed
feat(#Responder view acceptFirstResponder):
1 parent e081ca3 commit c085184

File tree

5 files changed

+64
-7
lines changed

5 files changed

+64
-7
lines changed

macOS-dev/Day38- NSResponderChain/ResponderChainDemo/ResponderChainDemo.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
D8393B7F2248693C00C68A4C /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D8393B7E2248693C00C68A4C /* Assets.xcassets */; };
1313
D8393B822248693C00C68A4C /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D8393B802248693C00C68A4C /* Main.storyboard */; };
1414
D8393B852248693C00C68A4C /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = D8393B842248693C00C68A4C /* main.m */; };
15+
D8F817BF2249CA9A00B49597 /* XCResponseView.m in Sources */ = {isa = PBXBuildFile; fileRef = D8F817BE2249CA9A00B49597 /* XCResponseView.m */; };
1516
/* End PBXBuildFile section */
1617

1718
/* Begin PBXFileReference section */
@@ -25,6 +26,8 @@
2526
D8393B832248693C00C68A4C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
2627
D8393B842248693C00C68A4C /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
2728
D8393B862248693C00C68A4C /* ResponderChainDemo.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = ResponderChainDemo.entitlements; sourceTree = "<group>"; };
29+
D8F817BD2249CA9A00B49597 /* XCResponseView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XCResponseView.h; sourceTree = "<group>"; };
30+
D8F817BE2249CA9A00B49597 /* XCResponseView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = XCResponseView.m; sourceTree = "<group>"; };
2831
/* End PBXFileReference section */
2932

3033
/* Begin PBXFrameworksBuildPhase section */
@@ -61,6 +64,8 @@
6164
D8393B792248693B00C68A4C /* AppDelegate.m */,
6265
D8393B7B2248693B00C68A4C /* ViewController.h */,
6366
D8393B7C2248693B00C68A4C /* ViewController.m */,
67+
D8F817BD2249CA9A00B49597 /* XCResponseView.h */,
68+
D8F817BE2249CA9A00B49597 /* XCResponseView.m */,
6469
D8393B7E2248693C00C68A4C /* Assets.xcassets */,
6570
D8393B802248693C00C68A4C /* Main.storyboard */,
6671
D8393B832248693C00C68A4C /* Info.plist */,
@@ -139,6 +144,7 @@
139144
isa = PBXSourcesBuildPhase;
140145
buildActionMask = 2147483647;
141146
files = (
147+
D8F817BF2249CA9A00B49597 /* XCResponseView.m in Sources */,
142148
D8393B7D2248693B00C68A4C /* ViewController.m in Sources */,
143149
D8393B852248693C00C68A4C /* main.m in Sources */,
144150
D8393B7A2248693B00C68A4C /* AppDelegate.m in Sources */,

macOS-dev/Day38- NSResponderChain/ResponderChainDemo/ResponderChainDemo/Base.lproj/Main.storyboard

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="11134" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11134"/>
4+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14490.70"/>
5+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
56
</dependencies>
67
<scenes>
78
<!--Application-->
@@ -618,7 +619,7 @@
618619
<menuItem title="Show Sidebar" keyEquivalent="s" id="kIP-vf-haE">
619620
<modifierMask key="keyEquivalentModifierMask" control="YES" command="YES"/>
620621
<connections>
621-
<action selector="toggleSourceList:" target="Ady-hI-5gd" id="iwa-gc-5KM"/>
622+
<action selector="toggleSidebar:" target="Ady-hI-5gd" id="iwa-gc-5KM"/>
622623
</connections>
623624
</menuItem>
624625
<menuItem title="Enter Full Screen" keyEquivalent="f" id="4J7-dP-txa">
@@ -673,7 +674,7 @@
673674
<outlet property="delegate" destination="Voe-Tx-rLC" id="PrD-fu-P6m"/>
674675
</connections>
675676
</application>
676-
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModuleProvider=""/>
677+
<customObject id="Voe-Tx-rLC" customClass="AppDelegate"/>
677678
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
678679
<customObject id="Ady-hI-5gd" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
679680
</objects>
@@ -703,8 +704,8 @@
703704
<!--View Controller-->
704705
<scene sceneID="hIz-AP-VOD">
705706
<objects>
706-
<viewController id="XfG-lQ-9wD" customClass="ViewController" customModuleProvider="" sceneMemberID="viewController">
707-
<view key="view" wantsLayer="YES" id="m2S-Jp-Qdl">
707+
<viewController id="XfG-lQ-9wD" customClass="ViewController" sceneMemberID="viewController">
708+
<view key="view" wantsLayer="YES" id="m2S-Jp-Qdl" customClass="XCResponseView">
708709
<rect key="frame" x="0.0" y="0.0" width="480" height="270"/>
709710
<autoresizingMask key="autoresizingMask"/>
710711
</view>

macOS-dev/Day38- NSResponderChain/ResponderChainDemo/ResponderChainDemo/ViewController.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,23 @@ @implementation ViewController
1111
- (void)viewDidLoad {
1212
[super viewDidLoad];
1313
// Do any additional setup after loading the view.
14+
/** 通过NSEvent 添加本地事件监听 来实现监听键盘事件 (这种方法完全可以实现键盘键盘,但对于细致理解响应链的原理不利,为了学习响应链的事件传递过程,本项目中不推荐采用此方式) */
15+
// [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown handler:^NSEvent * _Nullable(NSEvent * _Nonnull event) {
16+
// NSLog(@"local monitor for key down~");
17+
// return event;
18+
// }];
19+
1420
}
1521
- (void)viewDidAppear{
1622
[super viewDidAppear];
1723
/** 显示响应链信息: 此方法需要在控制器的视图显示后调用,若在viewDidLoad中调用,此时window对象尚未创建*/
1824
[self showResponderInfo];
25+
/** 查看窗口的第一响应者对象 */
26+
NSLog(@"first responder = %@",NSApp.keyWindow.firstResponder);
27+
// NSResponder *responder = NSApp.keyWindow.nextResponder;
28+
// [NSApp.keyWindow setNextResponder:self];
29+
/** 设置响应者时,如果没有下面这行代码,会有什么问题呢?? (供思考)*/
30+
// [self setNextResponder:responder];
1931
}
2032
- (void)showResponderInfo{
2133
NSResponder *responder = self.view.nextResponder;
@@ -26,6 +38,7 @@ - (void)showResponderInfo{
2638
responder = responder.nextResponder;
2739
}
2840
}
41+
2942
#pragma mark - mouse event
3043
- (void)mouseDown:(NSEvent *)event{
3144
NSLog(@"%s",__FUNCTION__);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// XCResponseView.h
3+
// ResponderChainDemo
4+
//
5+
// Created by Alexcai on 2019/3/26.
6+
// Copyright © 2019 dongjiu. All rights reserved.
7+
//
8+
9+
#import <Cocoa/Cocoa.h>
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface XCResponseView : NSView
14+
15+
@end
16+
17+
NS_ASSUME_NONNULL_END
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// XCResponseView.m
3+
// ResponderChainDemo
4+
//
5+
// Created by Alexcai on 2019/3/26.
6+
// Copyright © 2019 dongjiu. All rights reserved.
7+
//
8+
9+
#import "XCResponseView.h"
10+
11+
@implementation XCResponseView
12+
13+
//- (void)mouseDown:(NSEvent *)event{
14+
// NSLog(@"%s",__FUNCTION__);
15+
//}
16+
- (BOOL)acceptsFirstResponder{
17+
return YES;
18+
}
19+
20+
@end

0 commit comments

Comments
 (0)