NSButton argument binding doesn't pass argument? - objective-c

I have a NSCollectionView with a NSButton in the collection view item. The xib's owner is set to my BatchListViewController and the controller has the method
#interface BatchListViewController : NSViewController
-(IBAction)another_click;
#end
I set the binding for target to be:
This works fine but I also want to send the underlying model to the another_click method. According to the Apple docs,
The objects specified in the argument bindings are passed as parameters to the selector specified in the target binding when the NSButton is clicked.
So I set the binding for argument to be:
This runs fine if I keep the selector method's signature the same another_click: but if I change it to
-(IBAction)another_click:(id)arg;
I get the dreaded error:
BatchListViewController another_click]: unrecognized selector sent to instance
What am I doing wrong? Apple's docs say this is possible but I haven't been able to find an example of this working. Even other SO threads are saying this isn't possible but that can't be right.

The colon is part of the method signature, and you've forgotten to include it when setting up your bindings.

Related

Unrecognized Selector Sent to Instance; selector doesn't exist?

So I'm new to objective c, practically new to programming in general.
Anyway, in my simple program, I control-dragged my UITextField from the storyboard into my ViewController.m thinking it was a way to create a method to be invoked when the field was entered/tapped on. It wasn't long until I deleted the method.
When running the simulator, the keyboard would come up and the text field would already be focused on. If I tapped anywhere else on the screen, it resulted in a crash giving me the unrecognized selector error for the selector I already deleted.
There's nothing in my ViewController.h and the rest of the code seems fine. If I re-add the selector with no instructions, it behaves as intended and the keyboard resigns. My question is, why am I getting this error?
It would be more helpful to have the output of the crash. With that said I suspect your storyboard still has an outlet hooked up, referencing the function or outlet you created. From your storyboard click on your textfield and then navigate to the connections inspector (view -> utilities -> connections inspector). From there you should be able to see any connections you have made. Click the x to get rid of it.
The connection inspector is the icon in the upper right with the right facing arrow inside of a circle.
Edit:
I realized you asked why this is happening and not how to fix it. Unrecognized selector means just what it sounds like. Your code, in this case the storyboard is trying to call a method or access a variable on an object that doesn't implement that method. Normally the compiler catches these types of errors in your code but there are cases it can't catch, like the one your experiencing. For example it is ok to assign an NSArray object to a variable declared as something else, say an NSString as follows
id object = #[#"hello", #"world"];
NSString *notAString = object;
[notAString length];
Try running this and it will crash with a similar error because NSArray implement the length method, or in Objective-c speak, it doesn't respond to the selector "length".
Most typically it's this:
Select your UITextField in IB, go to the Connections inspector and remove the ghost connection to the removed method.

Cocos3D - 'May not respond' warning on dot notation

I am using Cocos3D and this function give me a 'may not respond' warning:
[CClayer.CC3scene someFunctionFromCC3scene];
I know one way to resolve this is to have a Function in CCLayer to make CC3scene call someFunctionFromCC3scene. But is there any other way to do this? Thanks!
More likely you're not importing the corresponding header. For example #import "CC3Scene.h". I suppose this can happen if the return type of the CC3scene property is id instead of the actual class.
Or the compiler is correct about this and it will crash with "does not respond to selector" when you run it. In that case CC3Scene does not implement the someFunctionFromCC3scene selector. Check spelling and parameters, and of course that the class actually implements that selector and has it declared in the #interface.
From the look of your example, there are several things that might be going wrong:
You might be confusing CCLayer with CC3Layer. CC3Layer is a subclass of CCLayer that supports displaying a 3D scene. Be sure you are instantiating a CC3Layer.
If you really are using a CC3Layer and the CC3Scene property (which is actually cc3Scene) is returning an instance of CC3Scene, then, as LearnCocos2D indicates, verify that the method you are invoking actually exists in the CC3Scene class. If you provide the specific method name here, then I can provide further help.
If the someFunctionFromCC3Scene is actually defined and implemented in your custom subclass, then you should cast the CC3Scene instance to your custom subclass before invoking that method:
[((MyCC3Scene*)aCC3Layer.cc3Scene) someFunctionFromMyCC3Scene];
...Bill

Wiring NSToolBarItem with XIBs

For some reason I'm having a very difficult time wiring my NSToolbarItem to an IBAction.
I'm using an NSWindowController with its own XIB. The NSToolbar is dropped into the XIB, and I added the NSToolbarItem without issue. This whole NSWindowController is created by a master NSViewController when an image is clicked.
The problem lies with the new button not sending. I unchecked "Autovalidates" and checked "Selectable" so that I could actually click the button.
However when pressed, I receive "unrecognized selector sent to instance".
I've wired this several times over through the XIB interface to make sure I wasn't messing up.
What's going on? Thank you.
Full error:
-[__NSCFType buttonPressed:]: unrecognized selector sent to instance 0x101915010
The delegate class does not know the selector you've wired. Either your delegate is wrong or you've got a typing error in your method.
Check the signature of the Method you've declared in your headerfile vs. its implementation in the class file. Perhaps you forgot to declare a parameter.
edit:
the class type where the selector is called looked weird, so I googled __nscftype unrecognized selector. here are some suggestions, it seems like your delegate is already disposed :
http://imlocation.wordpress.com/2007/09/13/strange-objects-nscftype-indicate-memory-management-bugs/
[__NSCFType searchKeyword:]: unrecognized selector sent to instance 0x6d8eb80

How does forwardingTargetForSelector: work?

I have a UIBarButtonItem. When it receives a message it cannot handle, I want it to forward that message to a particular view controller.
I thought I might be able to accomplish this using the bar button item's forwardingTargetForSelector: method, but apparently no such property is found on objects of type UIBarButtonItem. (Point of terminology: Does that mean forwardingTargetForSelector: is a private property? edit: Wait, I think I'm confused... methods with a colon at the end aren't properties... so can you ever make public a method (like a getter/setter) to which parameters are passed?)
And does that mean that in order to set the value of forwardingTargetForSelector: I must do it from within the .m file of the object for which I want to set it? Which would mean that I would have to subclass my UIBarButtonItem?
And if so, why is this not a public property of NSObjects?
And moreover, what's the best way to achieve my forwarding goal, preferably avoiding subclassing?
additional information:
It all stems from my inclination to reuse a single action in response to various instances of an identical button being pressed. The action is currently contained in my delegate (see How should I implement [almost] identical actions used throughout various VCs? (Answer: use a category)) and varies only in that it should send a presentViewController message to the view controller that instantiated the button that sent the action. Thus, in the action, I can send a presentViewController message to sender, which is an instance of the button, and I want to be able to forward that message to the view controller that created that instance of the button, which I can do if I set each button's forwarding property immediately after it is instantiated in its respective view controller.
I hoped to avoid the "why" just to make the question shorter, but there ya go.
forwardingTargetForSelector: is not really a property; it's more like a question the runtime asks an instance when the instance doesn't respond to a message.
It can't be a property in the #property/declared-property sense, because each selector could have a different target; there would need to be a mapping behind it. That's just not how declared properties work.
UIBarButtonItem descends from NSObject, and it inherits this method along with all the others, but you can't "set" the forwarding target for a selector from outside an instance (without creating some other machinery to allow you to do so, anyways -- possible, but not available by default).
In order to utilize this method, yes, you have to implement it in the class that is doing the forwarding. This does indeed mean subclassing. It also means that the forwarding instance needs to have a reference to the object to which it is forwarding; this requires careful design.
forwardingTargetForSelector: is all but certainly not the correct way to achieve whatever your goal is. In general, in fact, it's a bit esoteric.
I'm not sure exactly what problem you're trying to solve ("making a button forward messages it doesn't respond to" is still rather general -- in particular, why is that necessary?), so it's hard to be more precise.

Creating member object in Xcode: Program received signal: "SIGABRT"

Warning: complete newbie Xcode question.
In MainViewController.h I have the following line:
IBOutlet WorkItem *m_WIone;
I have created a class called WorkItem which is inherited from UILabel. The line above is so that I can use m_WIone (which will eventually become an array of such objects) as a member variable throughout MainViewController.
I have tried various ways to call WorkItem methods and finally settled on:
[m_WIone Reset];
where Reset is a WorkItem method. The method is declared in WorkItem.h as:
-(void) Reset;
and in WorkItem.m as:
-(void) Reset {}
With or without contents in this method, the app always crashes with:
Thread 1: Program received signal: "SIGABRT".
In the logging, I read:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILabel Reset]: unrecognized selector sent to instance 0x6a5f9f0'
How can I solve this? Am I even declaring this member variable correctly? (Despite trawling through StackOverflow, I cannot find the solution.)
Did you specify your custom class for the NIB? It seems like you want a widget in XCode's Interface Builder to use your custom class. Only the default class (UILabel) would be instantiated instead of your custom class.
Click on the UILabel you're using as the outlet and change the class (the screenshot here is for UITableView, but it should work the same for UILabel).
With that being said, there are alternatives to what you're trying to do. Instead of just using UILabel (which you eventually plan to make an array of them). Why not use a UITableView and customize cells to what you see fit (since UITableViews optimal for displaying lists of items)?