无法解释可可中如何修改指针值
我遇到了一个无法解释的错误。既然我发现了这个错误,我当然可以修复它,但我想了解这样的事情是如何发生的。
我的代码:
NSLog(@"1 - self.nextPlayerButton = %@",self.nextPlayerButton);
NSLog(@"[self.view setUserInteractionEnabled:TRUE] with self.view=%@",self.view);
[self.view setUserInteractionEnabled:TRUE];
NSLog(@"2 - self.nextPlayerButton = %@",self.nextPlayerButton);
其中 nextPlayerButton 定义为:
@property (retain) IBOutlet UIBarButtonItem *nextPlayerButton;
日志:
2011-05-15 15:23:05.245 Melimemo[1261:207] 1 - self.nextPlayerButton = <UIBarButtonItem: 0x4b668c0>
2011-05-15 15:23:05.248 Melimemo[1261:207] [self.view setUserInteractionEnabled:TRUE] with self.view=<UIView: 0x4b69750; frame = (0 0; 320 460); autoresize = W+H; layer = <CALayer: 0x4b54290>>
2011-05-15 15:23:05.249 Melimemo[1261:207] 2 - self.nextPlayerButton = <UIBarButtonItem: 0x4b65880>
如您所见, self.nextPlayerButton 第二次指向另一个对象。
我的问题的根源在于 self.view 实际上没有正确定义:我初始化了下面代码运行的对象实例,但没有定义视图是什么。不过,对任何东西调用 setUserInteractionEnabled 怎么会导致修改 self.nextPlayerButton 的指针值呢?即使是理论的开始也会有帮助。
I ran into a bug I have trouble explaining. Now that I found the bug, I can certainly fix it but I would like to understand how such a thing could even happen.
My code:
NSLog(@"1 - self.nextPlayerButton = %@",self.nextPlayerButton);
NSLog(@"[self.view setUserInteractionEnabled:TRUE] with self.view=%@",self.view);
[self.view setUserInteractionEnabled:TRUE];
NSLog(@"2 - self.nextPlayerButton = %@",self.nextPlayerButton);
where nextPlayerButton is defined as:
@property (retain) IBOutlet UIBarButtonItem *nextPlayerButton;
The log:
2011-05-15 15:23:05.245 Melimemo[1261:207] 1 - self.nextPlayerButton = <UIBarButtonItem: 0x4b668c0>
2011-05-15 15:23:05.248 Melimemo[1261:207] [self.view setUserInteractionEnabled:TRUE] with self.view=<UIView: 0x4b69750; frame = (0 0; 320 460); autoresize = W+H; layer = <CALayer: 0x4b54290>>
2011-05-15 15:23:05.249 Melimemo[1261:207] 2 - self.nextPlayerButton = <UIBarButtonItem: 0x4b65880>
As you can see, self.nextPlayerButton points to another object the second time around.
The source of my problem is that self.view is actually not properly defined: I initialize an instance of the object in which the code below runs and don't define what view is. Still, how could invoking setUserInteractionEnabled on anything result in modifying the pointer value of self.nextPlayerButton? Even the beginning of a theory would help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更有可能的是 self.view 调用导致 loadView 被调用,并且您的按钮被第二次设置为新按钮。
尝试像这样实现 nextPlayerButton 的设置器:
然后在其中放置一个断点并运行您的应用程序。断点可能会被击中两次。您可以查看堆栈跟踪以了解为什么它会在意外时间被命中。
More likely is that the self.view call is causing loadView to be invoked, and your button to be set a second time to a new button.
Try implementing the setter for nextPlayerButton like this:
Then put a break point in there and run your app. The break point will probably be hit twice. You can look at the stack trace to see why it's getting hit the unexpected time.