Cocoa touch - problem with button-viewController connection - iphone

I'm very new to cocoa touch and am stuck on a problem that doesn't seem to have a google solution. I have added a view to my application, and this view has a button on it. When the button is pressed I want the callback to perform an operation.
My problem is that the callback isn't being called. I created the view in Interface Builder, connected the touch-up-inside connection to my Owner class (in this case a viewController class), and selected the appropriate callback.
The error I get is as follows:
2009-10-13 17:13:51.708 MyApp[7467:20b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSCFSet contactsButtonPressed:]: unrecognized selector sent to instance 0x4c27330'
As I understand it this suggests that the connection between contactsButtonPressed and MyViewController is wrong. I'm not sure where the NSCFSet object comes from.
Does anyone have any idea?
Thanks!

The error I get is as follows:
2009-10-13 17:13:51.708 MyApp[7467:20b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSCFSet contactsButtonPressed:]: unrecognized selector sent to instance 0x4c27330'
You're probably under-retaining whatever controller object of yours is supposed to receive that action message. Add an NSLog call in the controller's dealloc method; you'll probably find that it gets deallocked before you expect it to.
The question to ask then is what should own that controller. Then, make sure that all of the owners are retaining it.
If you're holding the controller in a property, make sure that you actually use that property. A common mistake is to write myController = theController, which bypasses the property and assigns directly to the instance variable, instead of self.myController = theController (property access syntax, implicit accessor message) or [self setMyController:theController] (explicit message syntax).
Also, if you've implemented your own accessors for the property (especially setMyController:), make sure your setter releases the old object and retains the new one. Of course, this is assuming you have a reason to implement your own accessor; normally, you should simply #synthesize the property and let the compiler write the accessor for you.

somewhere in your code you have a line that looks like this:
[button addTarget:self action:#selector(contactsButtonPressed:)];
If you have a line that looks like this, you will also need to have a method with this signature:
- (void)contactsButtonPressed:(UIButton *)sender {
...
}
If you look at the error, it seems that you are sending the message to an NSCFSet object instead of the controller. I would check that you are setting delegate to self or the controller.

I'm new to cocoa too,
Maybe you forgot to add the sender parameter to your contactsButtonPressed.
Is your view's class named NSCFSet?
Try pasting some code.

Related

Error: Message sent to deallocated instance

I've made a popup view, with a UIButton on it which closes the view. Whenever I put on the button, the program quits with this message: [MTPopupWindow performSelector:withObject:withObject:]: message sent to deallocated instance 0x84675f0
Here is the header file and source file to use the class I use this line of code:
[MTPopupWindow showWindowWithContent:#"Some text here" insideView:self.view];
I thought that there was something wrong with deallocating my objects too soon but since I'm using ARC I'm not sure what is causing this problem. I think the problem is in this line of code:
[self.closeBtn addTarget:self action:#selector(closePopupWindow) forControlEvents:UIControlEventTouchUpInside];
But I don't see anything wrong with this.
You should have known there was a problem when you wrote this:
// Cast to void because we don't use the result (otherwise compiler warning)
Since you don't use the result, ARC believes that it's free to insert a release on your object after that line, which means the object is getting deallocated way early.
There are a number of ways around this; take a look at things like NS_RETURNS_RETAINED or having the caller of your popup window hold a strong reference to it.

SIGABRT error - NSUnknownKeyExeption (SetValue:forUndefinedKey;)

im getting the SIGABRT error each time i build. Im seeing the reason below:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key numberEntered.'
-(IBAction)numberEntered:(id)sender
{
NSNumber *day=[NSString stringWithFormat:#"#%", dayEntry];
NSLog(#"%#", day);
}
I assume its something to do with me trying to pass the wrong value to *day, but i cant see what the issue is (no errors in xCode).
Essentially all i want to do is to retrieve a Numerical value from a textfield and set it do a variable (day) so i can use it in a calculation. I have added the NSLog in order to ensure im acquiring the correct data from my textfield.
Thanks in advance!
The other problem is your format string and the fact that you are assigning an NSString to an NSNumber.
NSNumber *day=[NSString stringWithFormat:#"#%", dayEntry];
Try to change it to:
NSString *day=[NSString stringWithFormat:#"%#", dayEntry];
Subtle difference, see if you can spot it.
Make sure your custom class implementation implementing numberEntered: (subclassed UIView / UIViewController / UITextFIeld or whatnot) is actually selected within the InterfaceBuilder under Custom Class, Class.
For the sake of this example, I am assuming it is a UIViewController based class and you call it MyViewController. In that sense, you will need to enter that name under the mentioned IB when observing the "File's Owner".
For validating if this actually was your problem, you could add something to your viewDidLoad method that prints something into the console. Before being able to do so, you would have to remove that custom method from the event (as that baby would trigger the exception). If that something does not appear when showing the view of that viewController, you know that you have the described issue.
See this:
try this out :
NSNumber *day = [NSNumber numberWithInt:[dayEntry intValue]];
Make sure, that you actually have registered the method named numberEntered: (note the trailing colon) with IB. It's a method different from numberEntered (without the trailing colon). The error might be caused by
the view calling numberEntered (instead of numberEntered:)
the runtime trying to resolve the (undefined method) via KVC lookup.
This could be caused by a missing (or wrong) File's Owner resp. Custom Class defined in a .xib file for your control (hinted at by Till) or a plain old typo (missing the trailing :).

iOS how to catch exception unrecognized selector sent to instance when target object is NULL already?

I read alot about what is the problem for exception "unrecog..." But i need something else
I have view with two buttons: Start and Delete, also i have two UILabels: oneLabel and secondLabel
So the I press button Start I start NSOperation thread started
And I give him labels (oneLabel,secondLabel) as params
to change text of labels on main loop i use
[oneLabel performSelectorOnMainThread:#selector(setText:) withObject:someString waitUntilDone:YES];
All works fine, but when i press button Delete - it's deleting secondLabel from view with method
[secondLabel removeFromSuperview]
and then
secondLabel=nil;
So, after what i get exception. I understand why it happened - because the target object for message with selector setText if not available now becaus it's nil.
And i get exception and app crashes.
How i can Catch this exception in this case?
For what i need it for? When use tableView controller with ImageView wich loads images in separate thread.
try this:
id yourObject;
if (yourObject != nil && [yourObject respondsToSelector:#selector(yourSelector)]) {
// Do your stuff here
}
This will only call your method to be executed if your objects is still available and it responds to the specifed selector.
Hope this helps, Vlad
setText: it is not an object, it should be a method in your implementation. someString is the object that is send to the setText method.
When the command
[oneLabel performSelectorOnMainThread:#selector(setText:) withObject:someString waitUntilDone:YES]; is executed, then the method setText is called.
You are seeing unrecognized selector sent to instance because the method
-(void)setText:(id)sender does not exist or you have misspelled it.
You dont need to catch the exception. You can make a method that waits for the selector to excecute and catches it when it is really available.
You need to probably remake the secondLabel if I understood correctly because you are deleting it. Do this first and the exception won't be there to catch.
This is not a problem to send a message to nil, it is legal. Unrecognised selector exception means you have wrong spelling in your method name. A common mistake is to forget the colon if the method requires a parameter. For example #selector(setText) when you meant #selector(setText:).

Pass anything except sender in IBAction?

In my view controller (viewDidLoad) I am populating an array defined in the view controllers header file with settings data (from an archive). I am using data from the array to write labels on buttons in the view, no problem so far.
I would like to pass one element of the array through an IBAction to add to the new view controller. I keep getting an error from within the IBAction
'NSInvalidArgumentException', reason: '-[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x5d230f0').
Here is the relevant part of my IBAction code:
-(IBAction)pushedTimer:(id)sender {
if (!timerViewController) {
timerViewController = [[TimerViewController alloc] init];
}
[timerViewController setPreset:[[settingsArray objectAtIndex:0] settingLength]];
[[self navigationController] pushViewController:timerViewController animated:YES];
}
I was thinking that since the array is accessible in other methods, it should also work with the IBAction? The error leads me to believe that the IBAction can not determine that the settingsArray is really an array?
Thanks again.
I am not sure but here is what I think for this common problem:
Your array is garbaged already. What this means is that your array is dealloc somewhere in your code (by your over call of release or autorelease) then the string is allocated into the same memory area of the array. So that when the system try to call the object in that memory area, it is calling a NSString object not a NSArray object anymore.
You should double check all the release and autorelease of your array
Do instances in settingArray implement settingLength?
The question was itself a answer. I meant instances should implement settingLength.
How are you defining the settingArray? Are you creating a #property and #synthesizing it? If you're not, then you can't access that in allocated instances of that object, so your array will be nil. Also make sure to check what #alones said!
-[NSCFString objectAtIndex:]: unrecognized selector sent sounds like settingsArray is a NSString, not a NSArray.

unrecognized selector AGAIN

There are LOTS of iphone-related questions of this type. I know; I read them until I wasn't learning anything new, all in an effort to avoid (a) posting and (b) looking mighty green.
#interface CommonCostingClass : NSObject {
}
-(void) theCCC;
#end
That's the whole thing. As minimal as I could make it. I've even resorted to UIView instead of NSObject. Inside CommonCostingClass.m I have
#import "CommonCostingClass.h"
#implementation CommonCostingClass
-(void) theCCC {
// blah-blah
}
Again, that's all of it. Inside myViewController I coded
#import "CommonCostingClass.h"
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
if (textField.tag == 4) {
[(CommonCostingClass *) self.view theCCC]; // <-ka-boom
}
// other stuff
}
The presence / absence of the cast makes no difference.
self.view generates
* -[UIView theCCC]: unrecognized selector sent to instance 0x5d3dd20
2010-07-20 11:30:54.732 Wind17[3233:207]
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIView theCCC]: unrecognized selector sent to instance 0x5d3dd20'
self generates the same message, with the substitution of "myViewController" for "UIView."
Clean all targets has no effect.
I know that neither UIView nor myView Controller "see" method "theCCC".
I don't know how to say, "It's there! It's there!"
Thank you for the help. Someday this situation will be funny and not embarrassing.
following the comments, on this line
[(CommonCostingClass *) self.view theCCC];
you are trying to perform -theCCC method on self.view. self.view is a UIView.
If you want your custom class to be a NSObject subclass as you have it now, you need to create and initialize a CommonCostingClass object in your view controller, then call -theCCC on it.
edit for unrecognized selector: Unrecognized selector means you are calling a method on a class that does not implement that method. In your case you are calling theCCC on UIView which does not implement or know of a theCCC method.
Seems like you really want
+(void) theCCC;
(note the "+") which is a class method, then you would just call
[CommonCostingClass theCCC]
There really is no mystery to "unrecognized selector" It means the first thing in the brackets (in your case self.view) does not understand (have the method) theCC. And why would it?
You have declared a type of class, but in order to make use of that class you have to have an instance somewhere. So how did you think an instance of CommonCostingClass was ever created?
At least two problems here
If you are going to use CommonCostingClass as a view, it needs to subclass UIView. In the code you're posting, CommonCostingClass is a subclass of NSObject. That could be a typo or it could indicate a much more fundamental issue with what you're trying to do here.
You need to make sure the that view property of your UIViewController is set to be an instance of CommonCostingClass. The easiest way to do this is using Interface Builder.