“无法识别的选择器发送到实例” Objective-C 中的错误

发布于 2024-08-25 12:16:48 字数 1070 浏览 11 评论 0原文

我创建了一个按钮并为其添加了一个操作,但是一旦调用它,我就收到此错误:

-[NSCFDictionary numberButtonClick:]: unrecognized selector sent to instance
 0x3d03ac0 2010-03-16 22:23:58.811
 Money[8056:207] *** Terminating app
 due to uncaught exception
 'NSInvalidArgumentException', reason:'*** -[NSCFDictionary numberButtonClick:]:  unrecognized selector sent to instance 0x3d03ac0'

这是我的代码:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        UIButton *numberButton = [UIButton buttonWithType:UIButtonTypeCustom];        
        numberButton.frame = CGRectMake(10, 435, 46, 38);
        [numberButton setImage:[UIImage imageNamed:@"one.png"] forState:UIControlStateNormal];
        [numberButton addTarget:self action:@selector(numberButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview: numberButton]; 
    }
return self;
}

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

I created a button and added an action for it, but as soon as it invoked, I got this error:

-[NSCFDictionary numberButtonClick:]: unrecognized selector sent to instance
 0x3d03ac0 2010-03-16 22:23:58.811
 Money[8056:207] *** Terminating app
 due to uncaught exception
 'NSInvalidArgumentException', reason:'*** -[NSCFDictionary numberButtonClick:]:  unrecognized selector sent to instance 0x3d03ac0'

This is my code:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        UIButton *numberButton = [UIButton buttonWithType:UIButtonTypeCustom];        
        numberButton.frame = CGRectMake(10, 435, 46, 38);
        [numberButton setImage:[UIImage imageNamed:@"one.png"] forState:UIControlStateNormal];
        [numberButton addTarget:self action:@selector(numberButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview: numberButton]; 
    }
return self;
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

染年凉城似染瑾 2024-09-01 12:16:49

对于那些像我一样通过 Google 到达这里的人来说,这可能更多地与 Xcode 4.2+/iOS 5+ 相关,那么 ARC 又如何呢?我遇到了同样的错误“无法识别的选择器发送到实例”。就我而言,我设置了 UIButton 的目标操作以将其自身作为发送者参数传递,但后来意识到我不需要它并在代码中将其删除。因此,类似:

- (IBAction)buttonPressed:(UIButton *)sender {

更改为:

- (IBAction)buttonPressed {

右键单击​​有问题的 UIButton 显示 Touch Up Inside 事件与视图控制器的 buttonPressed: 方法相关联。删除它并将其重新分配给修改后的方法,效果很好。

For those getting here via Google like I did, which probably pertains more to Xcode 4.2+/iOS 5+ more, what with ARC. I had the same error "unrecognized selector sent to instance". In my case I had a UIButton's target action set up to pass itself as the sender parameter, but later realised I didn't need it and removed that in code. So, something like:

- (IBAction)buttonPressed:(UIButton *)sender {

Was changed to:

- (IBAction)buttonPressed {

Right clicking the UIButton in question showed that the Touch Up Inside event was associated with the view controllers buttonPressed: method. Removing this and reassigning it to the modified method worked a treat.

傲鸠 2024-09-01 12:16:49

这是这个问题的最佳谷歌答案,但我有不同的原因/结果 - 我想我应该添加我的两分钱,以防其他人偶然发现这个问题。

今天早上我也遇到了类似的问题。我发现,如果右键单击出现问题的 UI 项目,您可以看到已创建的连接。就我而言,我有一个按钮可以连接两个操作。我从右键单击菜单中删除了操作并重新连接它们,我的问题得到了解决。

因此,请确保您的操作正确连接。

This was the top Google answer for this issue, but I had a different cause/result - I thought I'd add in my two cents in case others stumble across this problem.

I had a similar issue just this morning. I found that if you right click the UI item giving you the issue, you can see what connections have been created. In my case I had a button wired up to two actions. I deleted the actions from the right-click menu and rewired them up and my problem was fixed.

So make sure you actions are wired up right.

人心善变 2024-09-01 12:16:49

好吧,我必须在这里插话。 OP动态创建按钮。我有一个类似的问题,答案(经过几个小时的狩猎)是如此简单,让我感到恶心。

使用时:

action:@selector(xxxButtonClick:)

or (as in my case)

action:NSSelectorFromString([[NSString alloc] initWithFormat:@"%@BtnTui:", name.lowercaseString])

如果在字符串末尾放置冒号 - 它将传递发件人。如果您不将冒号放在字符串末尾,则不会,并且如果接收者需要冒号,则会收到错误。如果您动态创建事件名称,很容易错过冒号。

接收者代码选项如下所示:

- (void)doneBtnTui:(id)sender {
  NSLog(@"Done Button - with sender");
}
 or
- (void)doneBtnTui {
  NSLog(@"Done Button - no sender");
}

与往常一样,总是会错过明显的答案。

OK, I have to chip in here. The OP dynamically created the button. I had a similar issue and the answer (after hours of hunting) is so simple it made me sick.

When using:

action:@selector(xxxButtonClick:)

or (as in my case)

action:NSSelectorFromString([[NSString alloc] initWithFormat:@"%@BtnTui:", name.lowercaseString])

If you place a colon at the end of the string - it will pass the sender. If you do not place the colon at the end of the string it will not, and the receiver will get an error if it expects one. It is easy to miss the colon if you are dynamically creating the event name.

The receiver code options look like this:

- (void)doneBtnTui:(id)sender {
  NSLog(@"Done Button - with sender");
}
 or
- (void)doneBtnTui {
  NSLog(@"Done Button - no sender");
}

As usual, it is always the obvious answer that gets missed.

小ぇ时光︴ 2024-09-01 12:16:49

就我而言,该函数不需要参数,但按钮配置为发送导致错误的参数。为了解决这个问题,我必须重新连接事件处理程序。

这是我的函数:

在此处输入图像描述

请注意,它不包含任何参数。

这是我的按钮配置的图像(右键单击按钮查看它):

在此处输入图像描述

注意有3 个事件处理程序。

为了解决这个问题,我必须删除每个事件项,因为其中一个事件项正在向 EnterPressed 函数发送对其自身的引用。要删除这些项目,我单击每个项目名称旁边的小 x 图标,直到没有显示任何项目。

在此处输入图像描述

接下来我必须将按钮重新连接到事件。为此,请按住 Control 键,然后将一条线从按钮拖动到操作。它应该显示“连接操作”。注意:出于某种原因,我必须重新启动 XCode 才能使其正常工作;否则它只允许我在函数上方或下方插入操作(也称为创建新操作)。

在此处输入图像描述

您现在应该有一个连接到不传递参数的按钮事件的事件处理程序:

在此处输入图像描述

此答案补充了 @Leonard Challis 的答案,您也应该阅读该答案。

In my case the function was not expecting an argument but the button was configured to send one causing the error. To fix this I had to rewire the event handler.

Here is my function:

enter image description here

Notice it contains no arguments.

Here is an image of my button configuration (right click on the button to view it):

enter image description here

Notice there are 3 event handlers.

To fix this I had to remove each of the event items since one of them was sending a reference to itself to the enterPressed function. To remove these items I clicked on the little x icon next to the name of each item until there were no items shown.

enter image description here

Next I had to reconnect the button to the event. To do this hold down the Control key and then drag a line from the button to the action. It should say "Connect Action". Note: I had to restart XCode for this to work for some reason; otherwise it only let me insert actions (aka create a new action) above or below the function.

enter image description here

You should now have a single event handler wired to the button event that passes no arguments:

enter image description here

This answer compliments the answer by @Leonard Challis which you should read as well.

只是在用心讲痛 2024-09-01 12:16:49

如果您没有在界面生成器中设置视图的“类”,也会发生这种情况。

This can also happen if you don't set the "Class" of the view in interface builder.

往事随风而去 2024-09-01 12:16:49

就我而言,我使用 NSNotificationCenter 并尝试使用不带参数但添加冒号的选择器。去除冒号解决了这个问题。

使用选择器名称时,如果没有参数,请勿使用尾随冒号。如果有一个参数,请使用一个尾随冒号。如果有多个参数,则必须为它们命名并为每个参数添加一个尾随冒号。

请参阅 Adam Rosenfield 的回答:Objective-C 中的选择器?

In my case, I was using NSNotificationCenter and was attempting to use a selector that took no arguments, but was adding a colon. Removing the colon fixed the problem.

When using a selector name, don't use a trailing colon if there are no arguments. If there's one argument, use one trailing colon. If there are more than one argument, you must name them along with a trailing colon for each argument.

See Adam Rosenfield's answer here: Selectors in Objective-C?

深海夜未眠 2024-09-01 12:16:49

我在一个动态创建按钮的 Swift 项目中遇到了这个问题。问题代码:

var trashBarButtonItem: UIBarButtonItem {
    return UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "newButtonClicked")
}

func newButtonClicked(barButtonItem: UIBarButtonItem) {
    NSLog("A bar button item on the default toolbar was clicked: \(barButtonItem).")
}

解决方案是在操作后添加完整的冒号“:”:例如

var trashBarButtonItem: UIBarButtonItem {
        return UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "newButtonClicked:")
    }

    func newButtonClicked(barButtonItem: UIBarButtonItem) {
        NSLog("A bar button item on the default toolbar was clicked: \(barButtonItem).")
    }

此处的完整示例:https://developer.apple.com/library/content/samplecode/UICatalog/Listings/Swift_UIKitCatalog_DefaultToolbarViewController_swift.html

I had this problem with a Swift project where I'm creating the buttons dynamically. Problem code:

var trashBarButtonItem: UIBarButtonItem {
    return UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "newButtonClicked")
}

func newButtonClicked(barButtonItem: UIBarButtonItem) {
    NSLog("A bar button item on the default toolbar was clicked: \(barButtonItem).")
}

The solution was to add a full colon ':' after the action: e.g.

var trashBarButtonItem: UIBarButtonItem {
        return UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "newButtonClicked:")
    }

    func newButtonClicked(barButtonItem: UIBarButtonItem) {
        NSLog("A bar button item on the default toolbar was clicked: \(barButtonItem).")
    }

Full example here: https://developer.apple.com/library/content/samplecode/UICatalog/Listings/Swift_UIKitCatalog_DefaultToolbarViewController_swift.html

尹雨沫 2024-09-01 12:16:49

造成这种情况的最明显原因(为了完整性而包括在内)是不正确地转换指针并调用错误类的方法。

NSArray* array = [[NSArray alloc] init];
[(NSDictionary*)array objectForKey: key]; // array is not a dictionary, hence exception

The most obvious cause of this (included for completeness) is improperly casting a pointer and calling a method of the wrong class.

NSArray* array = [[NSArray alloc] init];
[(NSDictionary*)array objectForKey: key]; // array is not a dictionary, hence exception
江湖彼岸 2024-09-01 12:16:49

如何调试“无法识别的选择器发送到实例”

在大多数情况下,Xcode 不会将我们带到发生此问题的确切行。当应用程序崩溃时,您不会看到导致此问题的代码行,而是会被带到应用程序委托类,其中错误输出可能如下所示:

[UITableViewCellContentView image]: unrecognized selector sent to instance

[__NSDictionaryI objectAtIndex:] unrecognized selector sent to instance

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TestApp.MyViewController viewDidLoad:]: unrecognized selector sent to instance 0xDABCDD'

如何查找导致此问题的代码行:< /strong>

转到断点导航器。单击“+”选项。单击“异常断点”。将出现如下所示的新小部件。

输入图片此处描述

添加以下条件块:

-[NSObject(NSObject) doesNotRecognizeSelector:]

在此处输入图像描述

您还可以为所有异常设置断点。
现在再次运行您的代码。这次,当发生此异常时,断点将触发。

作者:Prafulla Singh

完整说明:https://prafullkumar77.medium.com/how-to-debug-unrecognized-selector-send-to -instance-402473bc23d

How to debug ‘unrecognized selector send to instance’

In most of the cases Xcode do not take us to the exact line where this issue happen. When app crash you won’t see the line of code that caused this, rather you will be taken to App delegate class, in which the error output may look like:

[UITableViewCellContentView image]: unrecognized selector sent to instance

or

[__NSDictionaryI objectAtIndex:] unrecognized selector sent to instance

or

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TestApp.MyViewController viewDidLoad:]: unrecognized selector sent to instance 0xDABCDD'

How to find line of code causing this:

Go to breakpoint navigator. Click ‘+’ option. click ‘Exception Breakpoint’. An new widget like following will apear.

enter image description here

Add following condition block:

-[NSObject(NSObject) doesNotRecognizeSelector:]

enter image description here

You can also put breakpoint for all exception.
Now run your code again. this time, breakpoint will trigger when this exception occurs.

WRITTEN BY: Prafulla Singh

Full explanition: https://prafullkumar77.medium.com/how-to-debug-unrecognized-selector-send-to-instance-402473bc23d

或十年 2024-09-01 12:16:49

我也有同样的问题。

我删除了故事板中的 uibutton 并重新创建了它..现在一切正常。

I also had the same issue.

I deleted my uibutton in my storyboard and recreated it .. now everything works fine.

养猫人 2024-09-01 12:16:49

我遇到了类似的问题,但对我来说,解决方案略有不同。就我而言,我使用类别来扩展现有类(用于某些调整大小功能的 UIImage - 请参阅此 howto 如果您感兴趣的话)并忘记将 *.m 文件添加到构建目标。愚蠢的错误,但当它发生在哪里时并不总是显而易见的。我认为值得分享...

I had a similar problem, but for me the solution was slightly different. In my case, I used a Category to extend an existing class (UIImage for some resizing capabilities - see this howto in case you're interested) and forgot to add the *.m file to the build target. Stupid error, but not always obvious when it happens where to look. I thought it's worth sharing...

眼泪也成诗 2024-09-01 12:16:49

另一个可能的解决方案:将“-ObjC”添加到链接器参数中。

完整的解释在这里:静态库中的 Objective-C 类别

我认为要点是:如果类别是在您静态链接的库中定义的,则链接器不够智能,无法链接类别方法。上面的标志使链接器链接到所有目标 C 类和类别,而不仅仅是它基于分析源代码而认为需要的链接器。 (请随意调整或更正该答案。我了解链接语言,所以我只是在这里鹦鹉学舌)。

Another possible solution: Add '-ObjC' to your linker arguments.

Full explanation is here: Objective-C categories in static library

I think the gist is: if the category is defined in a library you are statically linking with, the linker isn't smart enough to link in category methods. The flag above makes the linker link in all objective C classes and categories, not just ones it thinks it needs to based on analyzing your source. (Please feel free to tune or correct that answer. I'm knew to linked languages, so I'm just parroting here).

你列表最软的妹 2024-09-01 12:16:49

这发生在我身上,因为不小心删除了 UIViewcontroller 类代码中的“@IBAction func...”,因此在 Storyboard 中创建了 Reference Outlet,但在运行时有任何函数可以处理它。

解决方案是删除属性检查器内的 Outlet 引用,然后使用命令键将其拖动到类代码中重新创建它。

希望有帮助!

This happened to my because accidentally erase the " @IBAction func... " inside my UIViewcontroller class code, so in the Storyboard was created the Reference Outlet, but at runtime there was any function to process it.

The solution was to delete the Outlet reference inside the property inspector and then recreate it dragging with command key to the class code.

Hope it helps!

孤蝉 2024-09-01 12:16:49

我认为你应该在返回类型中使用 void,而不是 IBAction。因为您以编程方式定义了一个按钮。

I think you should use the void, instead of the IBAction in return type. because you defined a button programmatically.

老子叫无熙 2024-09-01 12:16:49

我遇到了同样的错误,我发现了以下内容:

当您使用代码时,

[self.refreshControl addTarget:self action:@selector(yourRefreshMethod:) forControlEvents:UIControlEventValueChanged];

您可能认为它正在寻找选择器:

- (void)yourRefreshMethod{
    (your code here)
}

但它实际上正在寻找选择器:

- (void)yourRefreshMethod:(id)sender{
    (your code here)
}

该选择器不存在,因此您会崩溃。

您可以将选择器更改为接收(id)发送者以解决该错误。

但是,如果您有其他函数在不提供发送者的情况下调用刷新函数,该怎么办?您需要一种对两者都适用的功能。简单的解决方案是添加另一个函数:

- (void)yourRefreshMethodWithSender:(id)sender{
    [self yourRefreshMethod];
}

然后修改刷新下拉代码以调用该选择器:

[self.refreshControl addTarget:self action:@selector(yourRefreshMethodWithSender:) forControlEvents:UIControlEventValueChanged];

我还在一台无法升级到最新版本 Mac OSX 的旧 Mac 上进行斯坦福 iOS 课程。所以我仍在为 iOS 6.1 进行构建,这解决了我的问题。

I had the same error and I discovered the following:

When you use the code

[self.refreshControl addTarget:self action:@selector(yourRefreshMethod:) forControlEvents:UIControlEventValueChanged];

You may think it's looking for the selector:

- (void)yourRefreshMethod{
    (your code here)
}

But it's actually looking for the selector:

- (void)yourRefreshMethod:(id)sender{
    (your code here)
}

That selector doesn't exist, so you get the crash.

You can change the selector to receive (id)sender in order to solve the error.

But what if you have other functions that call the refresh function without providing a sender? You need one function that works for both. Easy solution is to add another function:

- (void)yourRefreshMethodWithSender:(id)sender{
    [self yourRefreshMethod];
}

And then modify the refresh pulldown code to call that selector instead:

[self.refreshControl addTarget:self action:@selector(yourRefreshMethodWithSender:) forControlEvents:UIControlEventValueChanged];

I'm also doing the Stanford iOS course on an older Mac that can't be upgraded to the newest version of Mac OSX. So I'm still building for iOS 6.1, and this solved the problem for me.

陌伤ぢ 2024-09-01 12:16:49

就我而言,我在 2 小时后解决了问题:

发件人(tabBar 项目)没有任何引用插座。所以它没有指向任何地方。

只需创建一个与您的功能相对应的引用出口

希望这可以帮助你们。

On my case I solved the problem after 2 hours :

The sender (a tabBar item) wasn't having any Referencing Outlet. So it was pointing nowhere.

Juste create a referencing outlet corresponding to your function.

Hope this could help you guys.

萌无敌 2024-09-01 12:16:49

我目前正在学习 iOS 开发,并正在阅读 aPress 的《Beginning iOS6 Development》一书。我在第 10 章:故事板中遇到了同样的错误。

我花了两天时间才弄清楚,但发现我不小心将 TableView 单元格的标签设置为 1,而我不应该这样做。对于其他正在阅读这本书并收到类似错误的人,我希望这会有所帮助。

我真的希望我的代码中的未来错误更容易被发现!哈哈哈。调试错误并没有让我朝着正确的方向去弄清楚它(或者至少我太新了,无法理解调试器,哈哈)。

I'm currently learning iOS development and going through the "Beginning iOS6 Development" book by aPress. I was getting the same error in Chapter 10:Storyboards.

It took me two days to figure it out but found out I accidentally set the TableView cell's tag to 1 when I shouldn't have. For anyone else doing this book and receive a similar error I hope this helps.

I really hope future errors in my code are easier to find! hahaha. The debug error did nothing to push me in the right direction to figuring it out (or at least I'm too new to understand the debugger, lol).

爱情眠于流年 2024-09-01 12:16:49

就我而言,我使用的是 UIWebView,并且在第二个参数中传递了 NSString 而不是 NSURL。所以我怀疑传递给函数的错误类类型可能会导致此错误。

In my case I was using a UIWebView and I passed a NSString in the second parameter instead of a NSURL. So I suspect that wrong class types passed to a functions can cause this error.

寄居者 2024-09-01 12:16:49

..现在

我的按钮链接到一个方法,该方法访问另一个按钮的参数,并且效果很好,但是当我尝试对按钮本身执行某些操作时,我遇到了崩溃。编译时没有显示错误..解决方案?

我无法将该按钮链接到文件的所有者。所以如果这里有人像我一样愚蠢,试试这个:)

..And now mine

I had the button linked to a method which accessed another button's parameter and that worked great BUT as soon I tried to do something with the button itself, I got a crash. While compiling, no error has been displayed.. Solution?

I failed to link the button to the file's owner. So if anyone here is as stupid as me, try this :)

眉目亦如画i 2024-09-01 12:16:49

另一个略有不同的解决方案/案例。

我正在使用 Xamarin 和 MvvmCross,并且尝试将 UIButton 绑定到 ViewModel。我将 UIButton 连接到插座和 TouchUpInside。

绑定时我只使用 Outlet:

set.Bind (somethingOutlet).For ("TouchUpInside").To(vm => vm.Something);

我所要做的就是删除 XCode 中的操作 (TouchUpInside) 连接,这解决了它。

聚苯乙烯
我想这在其基础上都与之前的答案有关,特别是与 @Chris Kaminski 相关,但我希望这可以帮助某人......

干杯。

Yet another slightly different solution/case.

I am using Xamarin and MvvmCross and I was trying to bind the UIButton to a ViewModel. I had the UIButton wired up to an Outlet and a TouchUpInside.

When Binding I only use the Outlet:

set.Bind (somethingOutlet).For ("TouchUpInside").To(vm => vm.Something);

All I had to do was remove the action (TouchUpInside) connection in XCode and that solved it.

P.S.
I guess this is in its base all related to the previous answers and to @Chris Kaminski in particular, but I hope this helps someone...

Cheers.

情话已封尘 2024-09-01 12:16:49

我有同样的问题。对我来说,问题是一个按钮有两个操作方法。我所做的是为我的按钮创建第一个操作方法,然后在视图控制器中将其删除,但忘记在连接检查器中断开主故事板中的连接。因此,当我添加第二个操作方法时,一个按钮现在有两个操作方法,这导致了错误。

I had the same issue. The problem for me was that one button had two Action methods. What I did was create a first action method for my button and then deleted it in the view controller, but forgot to disconnect the connection in the main storyboard in the connection inspector. So when I added a second action method, there were now two action methods for one button, which caused the error.

情未る 2024-09-01 12:16:49

对我来说,这是在interfacebuilder bij ctrl-dragging 中创建的剩余连接。断开的连接的名称位于错误日志中,

*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[NameOfYourApp.NameOfYourClass nameOfCorruptConnection:]:
unrecognized selector sent to instance 0x7f97a48bb000'

我有一个链接到按钮的操作。按下按钮会使应用程序崩溃,因为我的代码中不再存在 Outlet。
在日志中搜索该名称使我在故事板中找到了它。删除它,崩溃就消失了!

For me, it was a leftover connection created in interfacebuilder bij ctrl-dragging. The name of the broken connection was in the error-log

*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[NameOfYourApp.NameOfYourClass nameOfCorruptConnection:]:
unrecognized selector sent to instance 0x7f97a48bb000'

I had an action linked to a button. Pressing the button crashed the app because the Outlet no longer existed in my code.
Searching for the name in the log led me to it in the storyboard. Deleted it, and the crash was gone!

四叶草在未来唯美盛开 2024-09-01 12:16:49

我正在回复 Leonard Challis,因为我也在参加斯坦福 iOS 课程 C193P,用户“oli206”也是如此

“由于未捕获的异常‘NSInvalidArgumentException’而终止应用程序,原因:”

问题是我有“Enter”按钮在计算器上连接了两次,一位朋友指出,检查情节提要中的按钮后发现,当我右键单击“Enter”按钮时,“Touch Up Inside”属性上有 2 个条目。删除两个“Touch Up Inside”“已发送事件”之一解决了该问题。

这表明问题是作为 2 个发送事件触发的(对于作业 1 的计算器演练中的 C193P 视频类),其中一个事件导致了异常。

I'm replying to Leonard Challis, since I was also taking the Stanford iOS class C193P, as was user "oli206"

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:"

The problem was that I had the "Enter" button on the calculator connected twice,and a friend pointed out that doing an inspection of the button in the Storyboard showed that 2 entries were on the "Touch Up Inside" attributes when I right clicked on the "Enter" button. Erasing one of the two "Touch Up Inside" "Sent Events" solved the problem.

This showed that the problem is triggered (for the C193P video class on the Calculator Walkthrough on Assignment 1) as 2 sent events, one of which was causing the exception.

情未る 2024-09-01 12:16:49

当您没有将 ViewController 分配给 ViewControllerScene 时,可能会发生这种情况
界面生成器。所以ViewController.m没有连接到任何场景。

It can happen when you do not assign the ViewController to the ViewControllerScene in
the InterfaceBuilder. So the ViewController.m is not connected to any scene.

弥枳 2024-09-01 12:16:49

包括我的分享。我在这个问题上停留了一段时间,直到我意识到我已经创建了一个项目 ARC(自动计数引用) 已禁用。将该选项快速设置为“是”解决了我的问题。

Including my share. I got stuck on this for a while, until I realized I've created a project with ARC(Automatic counting reference) disabled. A quick set to YES on that option solved my issue.

A君 2024-09-01 12:16:49

另一个非常愚蠢的原因是在接口(.h)中定义了选择器,但不在实现(.m)中(pe拼写错误)

Another really silly cause of this is having the selector defined in the interface(.h) but not in the implementation(.m) (p.e. typo)

情魔剑神 2024-09-01 12:16:49

添加到列表中的另一个原因/解决方案。这是由iOS6.0(和/或糟糕的编程)引起的。在旧版本中,如果参数类型匹配,选择器就会匹配,但在 iOS 6.0 中,我在以前的工作代码中遇到崩溃,其中参数名称不正确。

我正在做类似的事情

[objectName methodName:@"somestring" lat:latValue lng:lngValue];

,但在定义(.h 和 .m)中我有

(viod) methodName:(NSString *) latitude:(double)latitude longitude:(double)longitude;

这在 iOS5 上运行良好,但在 6 上则不然,即使部署到不同设备的完全相同的构建也是如此。

无论如何,我不明白为什么编译器不能告诉我这一点 - 问题已解决。

Another reason/solution to add to the list. This one is caused by iOS6.0 (and/or bad programming). In older versions the selector would match if the parameter types matched, but in iOS 6.0 I got crashes in previously working code where the name of the parameter wasn't correct.

I was doing something like

[objectName methodName:@"somestring" lat:latValue lng:lngValue];

but in the definition (both .h and .m) I had

(viod) methodName:(NSString *) latitude:(double)latitude longitude:(double)longitude;

This worked fine on iOS5 but not on 6, even the exact same build deployed to different devices.

I don't get why the compiler coudn't tell me this, anyway - problem soled.

居里长安 2024-09-01 12:16:49

当您想要将 ControllerA 的属性设置为自定义 ControllerB 类内的公共属性,并且尚未在故事板的身份检查器内设置“自定义类”时,也可能会发生这种情况。

This also might happen when you want to set a property from a ControllerA to a public property inside a custom ControllerB class and you haven't set the "Custom Class" inside the identity inspector in storyboards yet.

ゃ人海孤独症 2024-09-01 12:16:49

我的问题和解决方案是不同的,我想我应该将其发布在这里,以便未来的读者可以避免他们的头撞到墙上。

我将不同的 xib 分配给相同的 UIVIewController,即使在到处搜索后我也找不到如何纠正它。然后我检查了我调用 initWithNibName 的 AppDelegate,可以看到在复制代码时,我更改了 xib 名称,但忘记更改 UIViewController 类。因此,如果没有一个解决方案适合您,请检查您的 initWithNibName 方法。

My problem and solution was different and I thought I should post it here so that future readers can save their head from banging to the wall.

I was allocating different xib to same UIVIewController and even after searching everywhere I couldn't find how to correct it. Then I checked my AppDelegate where I was calling initWithNibName and can see that while copying the code, I changed the xib name, but forgot to change UIViewController class. So if none of the solution works for you, check your initWithNibName method.

一枫情书 2024-09-01 12:16:48

看起来您没有正确管理视图控制器的内存,并且它在某个时刻被释放 - 这导致 numberButtonClicked: 方法被发送到另一个对象,该对象现在占用视图的内存控制器先前占用...

确保您正确保留/释放视图控制器。

It looks like you're not memory managing the view controller properly and it is being deallocated at some point - which causes the numberButtonClicked: method to be sent to another object that is now occupying the memory that the view controller was previously occupying...

Make sure you're properly retaining/releasing your view controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文