朋友们,我在创建MAC应用程序时遇到问题我有两个视图控制器。当我点击放在第一个视图控制器app中的按钮时,使用下面的代码替换视图
- (IBAction)gotoEmployeeView:(id)sender
{
delegate = (AppDelegate *)[[NSApplication sharedApplication]delegate];
secondViewController *employeeVC = [[secondViewController alloc]initWithNibName:@"secondViewController" bundle:nil];
[[delegate.window.contentView animator] replaceSubview:self.view with:secondVC.view];
secondVC.view.frame = ((NSView *)delegate.window.contentView).bounds;
}
现在我有一个按钮,我在第二个视图中设置了此按钮的点击事件。在id按钮点击事件下面
- (IBAction)btnClicked:(id)sender
{
NSLog(@"clicked");
}
当我点击它时,应用程序崩溃了。并得到以下错误
2013-08-12 14:33:46.989 Employee Register[1954:303] -[__NSCFString btnClicked:]: unrecognized selector sent to instance 0x10213ace0
2013-08-12 14:33:46.991 Employee Register[1954:303] -[__NSCFString btnClicked:]: unrecognized selector sent to instance 0x10213ace0
2013-08-12 14:33:46.994 Employee Register[1954:303] (
0 CoreFoundation 0x00007fff8d76cf56 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff80e47d5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8d7f91be -[NSObject doesNotRecognizeSelector:] + 190
3 CoreFoundation 0x00007fff8d759e23 ___forwarding___ + 371
4 CoreFoundation 0x00007fff8d759c38 _CF_forwarding_prep_0 + 232
5 CoreFoundation 0x00007fff8d75c70d -[NSObject performSelector:withObject:] + 61
6 AppKit 0x00007fff8c5208ca -[NSApplication sendAction:to:from:] + 139
7 AppKit 0x00007fff8c5207fe -[NSControl sendAction:to:] + 88
8 AppKit 0x00007fff8c520729 -[NSCell _sendActionFrom:] + 137
9 AppKit 0x00007fff8c51fbec -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2014
10 AppKit 0x00007fff8c59fb74 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 489
11 AppKit 0x00007fff8c51e7f6 -[NSControl mouseDown:] + 786
12 AppKit 0x00007fff8c4e9c98 -[NSWindow sendEvent:] + 6306
13 AppKit 0x00007fff8c4833a5 -[NSApplication sendEvent:] + 5593
14 AppKit 0x00007fff8c419a0e -[NSApplication run] + 555
15 AppKit 0x00007fff8c695eac NSApplicationMain + 867
16 Employee Register 0x0000000100001342 main + 34
17 Employee Register 0x0000000100001314 start + 52
)
答案 0 :(得分:0)
看起来你正在向NSString对象发送btnClicked消息。你可以在你调用[... bntClicked]的地方发布代码吗?
您所描述的行为可能意味着实现btnClicked方法的viewController在调用操作之前被释放,或者接口生成器中的操作未正确设置。
您应该在实现btnClicked的viewController的dealloc方法中添加一个日志语句,并在调用操作之前看到它没有被释放。如果您正在使用ARC,请确保您有一个指向视图控制器的成员数据,以便它们不会被释放。
此外,您应该删除界面构建器中的ibaction关联并再次执行此操作,确保将按钮连接到正确的视图控制器。
答案 1 :(得分:0)
您的转到员工查看代码对我来说很合适
注意:replaceSubview:with:
方法会导致oldView被释放。