- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 UITapGestureRecognizer
有一个奇怪的问题,当调用它的操作时,我总是会遇到崩溃或抛出异常(无法识别的选择器)。我以前使用过手势识别器,但这次我将其附加到 UIView
,它是 NSObject
的属性(并且我使用该对象的方法来构造 View )。以下是我正在做的事情的一个最小示例:
MyObject.h:
#import <Foundation/Foundation.h>
@interface MyObject : NSObject
@property (nonatomic, strong) UIView *aView;
- (void)createViewWithFrame:(CGRect)frame;
@end
MyObject.m:
@interface MyObject ()
- (void)tapped:(id)sender; // Non-specific id type for brevity
@end
@implementation MyObject
- (void)tapped:(id)sender {
NSLog(@"Tapped");
}
- (void)createViewWithFrame:(CGRect)frame {
_aView = [[UIView alloc] initWithFrame:frame];
self.aView.backgroundColor = [UIColor blueColor];
self.aView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(tapped:)];
[self.aView addGestureRecognizer:tap];
}
ViewController.m:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
MyObject *obj = [[MyObject alloc] init];
[obj createViewWithFrame:self.view.frame]; // Excuse poor frame - example only
[self.view addSubview:obj.aView];
}
然后,当我点击 View 时,应用程序在此处崩溃:
libobjc.A.dylib`objc_msgSend:
0x10e008c: movl 8(%esp), %ecx
0x10e0090: movl 4(%esp), %eax
0x10e0094: testl %eax, %eax
0x10e0096: je 0x10e00e8 ; objc_msgSend + 92
0x10e0098: movl (%eax), %edx
0x10e009a: pushl %edi
0x10e009b: movl 8(%edx), %edi ; Thread 1: EXC_BAD_ACCESS (code=2, address=0x9)
0x10e009e: pushl %esi
0x10e009f: movl (%edi), %esi
0x10e00a1: movl %ecx, %edx
确切的崩溃点各不相同,我偶尔会收到无效选择器错误。 NSLog 自然不会发生。我之前(成功)使用过手势识别器,这让我相信问题出在我的设置/设计中。谁能解释为什么手势识别器在这种情况下无法正常工作?
最佳答案
问题出在 MyObject 的范围上,全局声明变量,
@property(nonatomic, strong) MyObject *obj;
在您的 viewDidAppear 中,初始化并添加 View
self.obj = [[MyObject alloc] init];
[self.obj createViewWithFrame:self.view.frame];
[self.view addSubview:self.obj.aView];
就是这样。
问题是什么?,*MyObject obj 的范围以 viewDidAppear 方法结束。当识别到点击时,它正在寻找对象来触发给定的方法。但该对象不存在。
关于ios - 将 UIGestureRecognizer 附加到封装的 UIViews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16380905/
我需要使用封装打印3个变量,并且无法修改主类。 主类声明变量[汽缸、制造商、所有者],并希望使用如下参数创建和打印一个对象: System.out.println (new Vehicle (cyli
我正在研究“Beginning Ruby”这本书,在我进行封装之前一切都进行得很顺利。我明白这段代码在做什么,我只是不知道为什么要用这些方法设置它。 对于那些感兴趣的人 this is the lin
所以我一直在研究面向对象的完整开发 JavaScript 实践,并对以下示例感到好奇。 据我了解,(这对我来说很有意义)以下“ secret ”字段是“私有(private)”的: var MyObj
在面向对象程式设计方法中,封装(英语:Encapsulation)是指,一种将抽象性函式接口的实作细节部份包装、隐藏起来的方法。 封装可以被认为是一个保护屏障,防止该类的代码和数据被外部类定义的代
封装 被定义为 把一个或多个项目封闭在一个物理的或者逻辑的包中 。在面向对象程序设计方法论中,封装是为了防止对实现细节的访问。 抽象和封装是面向对象程序设计的相关特性。抽象允许相关信息可
今天我读了一本书,作者写道,在一个设计良好的类中,访问属性的唯一方法是通过其中一个类方法。这是一个被广泛接受的想法吗?为什么封装属性如此重要?不做会有什么后果?我早些时候在某处读到这可以提高安全性或类
今天我接受了软件工程职位的面试。我已经从 stackoverflow 中阅读了很多关于面试的内容。他们向我询问了与 OOP 相关的正常情况。但他们也问我这些: 没有继承就可以封装吗? 没有继承就可以抽
我正在制作一个简单的网站,并编写了gradle构建脚本,该脚本将执行诸如lint并最小化css / html / js之类的事情。 在这一阶段,我刚刚完成了正在运行的CSS,但是只是初始的非结构化格式
下面的幻灯片是指 C++ 语言,它说 "encapsulation is about ensuring the user uses our ADT in a safe way" 但是,如果他可以访问我
尝试设置一个 Person 类 我在尝试将信息封装在类中以使其不会被意外更改时遇到了问题。除了当我尝试使用 setter/getters 进行封装时,该类工作得非常好。我认为问题是这些方法最终会互相循
面向对象的概念:封装、数据抽象和数据隐藏是3个不同的概念,但彼此之间非常相关。所以我很难通过阅读互联网上的信息来完全理解这些概念。一个地方可用的信息与互联网上另一个地方的信息相矛盾。有人可以指导我阅读
我被封装困住了。在我看来,好像我已经按照规则做了一切,但仍然不起作用。虽然我知道我错过了一些东西。我正在使用 Eclipse。 我想要通过创建一副标准 52 张卡片来进行一些练习,并尝试我学到的新东西
在java中,要获取映射中的所有键,我们可以使用方法keySet。但我想知道为什么方法名称不只是 keys ?名称 Set 是否会泄露有关实现的详细信息? 据我了解,Java 是静态类型语言,名称中包
我正在尝试找出如何防止应用程序中的页面变量被全局定义。我想出了一些方法,但我想知道是否有人们使用的通用标准方法。 我使用这种方法得到了我的插件设计模式:http://www.virgentech.co
我有一个 tcp 连接,我想在服务器类的 HandleConnectionAsync 方法中保持打开状态。它将从客户端接收持续更新。 private async void HandleConnecti
这是我类(class)的一个小样本: #include #include using std::string; using std::vector; struct Book { string
我想要类似下面的代码: class Foo{ public: void update() { for( auto dataModifier : _dataModifierLis
这已经困扰我一段时间了,所以我问了一个同事他是否能理解它,现在我来了;) 为什么可以在依赖属性的 PropertyChangedCallback 中访问持有类的私有(private)成员? 让我通过这
我知道封装意味着一个对象不应该直接访问不同对象的成员……但我猜它与公共(public)领域有关?我假设公共(public)方法不会破坏封装..?我只是在这里不清楚,很高兴得到任何解释。 最佳答案
我就废话不多说了,大家还是直接看代码吧~ ? 1
我是一名优秀的程序员,十分优秀!