4

Hello friends I faced problem while creating MAC Application I have two view controllers. when I clicks on button which is placed in first view controller app replace view using below code

- (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;
}

now I have one button and I have set click event for this button on second view. below id button click event

- (IBAction)btnClicked:(id)sender
{
    NSLog(@"clicked");
}

and when I clicked on it app was crashed. and get below error

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
 )
4

2 回答 2

0

看起来您正在向 NSString 对象发送 btnClicked 消息。您可以在调用 [... bntClicked] 的位置发布代码吗?

您描述的行为可能意味着实现 btnClicked 方法的 viewController 在调用操作之前被释放,或者接口构建器中的操作设置不正确。

您应该在实现 btnClicked 的 viewController 的 dealloc 方法中添加一条日志语句,并查看它在调用操作之前没有被释放。如果您使用 ARC,请确保您有一个成员数据指向您的视图控制器,这样它们就不会被释放。

此外,您应该在界面生成器中删除 ibaction 关联并再次执行此操作,确保将按钮连接到正确的视图控制器。

于 2013-08-12T09:34:34.470 回答
0

您的 gotoEmployeeView 代码对我来说看起来不错。
注意replaceSubview:with:方法会导致 oldView 被释放。

看看示例项目

于 2013-08-12T11:48:33.167 回答