Unrecognized selector NSInvalidArgumentException: for IBAction? - iphone

I am facing a strange problem in iOS iPad app. My application is crashing when I map a UIButton to an IBAction with touch up inside event. The error I get in the console is like
Unrecognized selector NSInvalidArgumentException.
By the way all the view controllers in my project have the same problem. The view controllers are mapped to a tab bar controller.
I have created this project by dragging and dropping all the files from another project. The dragged files are placed properly in the current project, no problem with that. Will this cause problem?
I was not able to figure out what went wrong.
Thanks

Just right click on your button in xib and check that you have only one connection for your button, if you have more then one then please delete unwanted connection .

Did you set the class of your custom UIViewController subclass in interface builder?

If problem stil survives after applying the solution by Gabriele Petronella and Rajneesh071 then there is a chance one small mistake :
If you are using :
[btn addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
then buttonTouched method must be (IBAction).
If buttonTouched method is of type (void) then just remove that : from your method call.
I know it's a simple thing, but it may result in crash.

I would check to make sure you have the unrecognized method(s) referenced by the UIButton:
1) is the method signature listed in your .h consistent with the method signature listed in your .m file?
2) are any expected arguments passed when you call them method?
3) is the method consistently listed as a class (+) or instance (-) method in your .h and .m files?
I know you said you copied the files over but did you test to make sure the app you copied from also works? Does the other app have the method hooked up to a button? Did you change ANYTHING after you copied it over?
Cheers

Thanks for your answers. I have resolved the problem.
Actual Problem:
My project is with ARC.
Actually i am navigating to a new view from one view to other by adding it as subview by clicking a button with IBAction as below,
-(IBAction)signInAndGoToFrameworkView :(id)sender{
MyViewController myViewController = [[MyViewController alloc] initWithNibName:#"MyViewController " bundle:nil];
[self.view addSubview:myViewController.view];
}
3 . As the project is with ARC, the object myViewController is released since this is a local variable. So when i press back button to do
[self.view removeFromSuperview];
the app was getting crashed.
Solution:
So when i just declared MyViewController myViewController in .h file and also given #property and #synthesize. Then changed the IBAction with,
-(IBAction)signInAndGoToFrameworkView :(id)sender{
myViewController = [[MyViewController alloc] initWithNibName:#"MyViewController " bundle:nil];
[self.view addSubview:myViewController.view];
}
Now the problem solved.
Thanks.

Related

class is not key value coding-compliant for the key navigationBar

I have added a UINavigationbar and UIscrollView to UIView(SecondView).When I click a button in firstView it should take me to secondView.
On button click :
SecondView *secondview=[[SecondView alloc]initWithNibName:#"SecondView" bundle:nil];
[self presentModalViewController: secondview animated:NO]; //error at this line
[secondview release];
In the secondView.h
#property(nonatomic,retain)IBOutlet UINavigationBar *navigationBar;
#property(nonatomic,retain)IBOutlet UIScrollView *testscroll;
SecondView.m:
#synthesize navigationBar,testscroll;
But Im getting error like :
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: SecondView setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key navigationBar.
My secondView.xib is like this :
Couldnot understand where Im going wrong?
This error generally comes when you have created an outlet in your xib and then by mistake (or knowingly) you have deleted that iboutlet object, or vice versa.
So check your xib's iboutlet connections carefully.
Check the spelling:
#property(nonatomic,retain)IBOutlet UINavigationBar *navigationBar;
Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: SecondView setValue:forUndefinedKey:]: this class is not key
value coding-compliant for the key navigationbar.
Note the difference of navigationBar and navigationbar, it's case sensitive.
you can presenting ModelViweController With navigationbar like this way:-
SecondView *objSecondView = [[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
UINavigationController *navbar = [[UINavigationController alloc] initWithRootViewController:objSecondView];
// add navigation bar image at hear
UIImage *image = [UIImage imageNamed:#"nav_launcher.png"];
[navbar.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
navbar.navigationBar.tintColor=[UIColor whiteColor];
[self presentModalViewController:navbar animated:YES];
and you can Push One View to Another View like:-
SecondView *objSecondView =[[SecondView alloc]initWithNibName:#"SecondView" bundle:nil];
[self.navigationController pushViewController:objSecondView animated:YES];
NOTE
Some time its error occurs because of we are putting wrong Nib name at this line of code : initWithNibName:#"SecondView" bundle:nil];
UPDATE
no need to add navbar tin image at SecondViewcontroller you can add all stuff hear like barbuttonItem, tincolor , navigatin BackgroudnColor ect.
pushViewController: is used to push a UIViewController. It looks like SecondView is a UIView, not a UIViewController.
I solved it myself.
I was wrong with nothing.Everything was OK.
I have added a new view and made the same connections and got it.Don't know why this happens regularly in xcode.
If you face this problem again,
try to: select File's Owner then click on the "Connection Inspector" (upper on the top of right pane), you will see all the outlets. Look for any sign like this (!) you will find it on the small circle which indicates a missing outlet, all you have to do is linking it properly or remove the outlet.
For erroneous case Rakesh provided correct answer (outlet was deleted - need accurately check your xib's / storyboards) with small addition:
it might be also caused by renaming outlet by simply "Find&Replace". In such case the outlet is re-named everywhere in sources, but not in storyboard.
In such case make sure you accurately renamed the outlet in storyboards too, e.g.:
grep -r --include "*.storyboard" navigationBar .
Than everything looks to be ok: XCode caching your xib's, to fix the behavior:
clean build
remove app from device/simulator
restart the app
Restart simulator might be also necessary (for me never was required for device)
I think you have not connect some object to the File's Owner in that UIViewController's xib.
Check one time my friend.

UItextView Delegate methods not called inside subview

In brief:
In ClassA_VC I do:
ClassB_VC *classB_VC=[ClassB_VC alloc]initWithNibName:#"ClassB_VC" bundle:nil];
[self.view addSubview:classB_VC.view];
Then in ClassB_VC I have an UITextField. I set the delegate connection between textfield and File's owner in IB, I added in #interface declaration and I wrote the protocol methods (textFieldDidEndEditing, textFieldDidBeginEditing, etc...) as usual.
This should work but I got an Exception when I touch the textfield. It seems textfield is not reaching its delegate.
However, if I present the view using presentModalViewController, everything works fine. I'd prefer not having to do it so because these views are into a tabViewController and I'd like not hiding the tabBar when showing.
I hope you understand what I'm trying to say. My english is not very good.
Have you retain ClassB_VC in ClassA_VC?
remove the delegate from interface builder and do this :
ClassB_VC *classB_VC=[ClassB_VC alloc]initWithNibName:#"ClassB_VC" bundle:nil];
[classB_VC.yourTextField setDelegate:classB_VC];
[self.view addSubview:classB_VC.view];
Let me know if it helps

Solution Like Trash Can Animation

I would like to put the undocumented trash can animation in my program. The call
method is:
+ (void)animateToolbarItemIndex:(unsigned)index duration:(double)duration target:(id)target didFinishSelector:(SEL)selector;
Can anyone figure out what I should plug in for:
index
duration
target
selector
?
My trials are not working resulting in the error:
2011-11-15 16:05:20.639 CNiPhone[973:707] +[UIToolbar animateToolbarItemIndex:duration:target:didFinishSelector:]: unrecognized selector sent to class 0x3f019c08
2011-11-15 16:05:20.641 CNiPhone[973:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[UIToolbar animateToolbarItemIndex:duration:target:didFinishSelector:]: unrecognized selector sent to class 0x3f019c08'
Here is the relevant code:
#interface UIToolbar (privateMethods2)
+ (void)animateToolbarItemIndex:(unsigned)index duration:(double)duration target:(id)target didFinishSelector:(SEL)selector;
#end
[UIToolbar animateToolbarItemIndex:0 duration:0.5 target:trashToolbarButton didFinishSelector:#selector(animateTrashStep2)];
[UIToolbar commitAnimations];
- (void) animateTrashStep2 {
}
You dont need to do any undocumented stuff for this, just create a custom UIButton. Download the UIKit Artwork Extractor and you'll find the frames for the trash can animation, as well as the UIBarButtonItem background.
You need to call it on the toolbar connected to your IBOutlet as opposed to the class. E.g.:
[self.myToolbar /*(possibly just myToolbar)*/ animateToolbarItemIndex:0 duration:0.5 target:trashToolbarButton didFinishSelector:#selector(animateTrashStep2)];
You will probably find the answer here: https://stackoverflow.com/a/5101910/796103
It's probably because your target is set to trashButtonItem. The target is the object that the didFinishSelector will be sent to. Try setting the target to self. Also, according to http://iphonedevwiki.net/index.php/UIToolbar this is not a class method so you will need to replace the [UIToolbar with the actual toolbar object.
In your didFinishSelector callback I guess you call the method again and the trashcan will close.
Good luck.
This 'undocumented' method is documented here: http://iphonedevwiki.net/index.php/UIToolbar
It's documented as being an instance method and not a class method, which explains the error message you're receiving in the exception.
#jrtc27 has answered the question correctly, in that this should be sent to the UIToolbar instance instead. From your reply to the comments, it seems that you have not changed your class category to assist the compiler. Try the following instead:
#interface UIToolbar (privateMethods2)
- (void)animateToolbarItemIndex:(unsigned)index duration:(double)duration target:(id)target didFinishSelector:(SEL)selector;
#end
And then use:
[self.navigationController.toolbar animateToolbarItemIndex:0 duration:0 target:self.trashToolbarButton didFinishSelector:#selector(animateTrashStep2)];
I think if you know how to do the suckEffect, then you can do a little bit hacking with the toolbar.
Basically, all the official controls are a subclass of UIView, hence you can find out the view hierarchy of the UIToolBar instance.
If you don't know how to find out the subview hierarchy of a given view, you can use a PRIVATE API - (void)recursiveDescription from UIView. Remember to use it in DEBUG configuration.
Why should we bother the view hierarchy?
The answer is, to hide certain view, or to add a subview as we want.
What's next
Find the origin UIBarButtonItem view of your trash can
Before the suckEffect start, hide it, add a new trash can view which can do the animation of open/close/shaking. This moment I think you need to ask it to do open animation.
Then let the suckEffect fly...
After the suckEffect ended, ask your view to do the close animation.
After the close animation is finished, remove your view, and reshow the original trash can view.
Possibility?
I haven't done it before, but I think it's a possible solution because creating an trash can view with open/close/shaking animation is easy.
Risk?
Anyway, this solution is like some kind of hacking without touching the private api, the risk is on your own.
Good luck.

Running a method on screen startup

Let me try and explain this. Within a project folder has .m and .h files (we all know that haha) however when you start a new ".m" with a xib like for example:
ViewTwoController *loginView = [[ViewTwoController alloc] initWithNibName:#"contentscreen" bundle:nil];
[self presentModalViewController:loginView animated:YES];
[loginView release];
I was wondering how do I create a method that runs as soon as that class (if I can call it that, I'm new to objective C) similar to a main method that Java would have. I want to run some code as soon as that .m has been called and started. Any help would be valued thanks :)
To be safe try to put this in - (void)viewDidLoad
Everything that happens here happens when the view is loaded onto the screen.
Note that there are number of places where you can put your method, in viewWillAppear or in your AppDelegate etc. But I found putting UI elements like your login popup better in viewDidLoad
Hope this helps...
You can add additional code in viewDidLoad method, which is called as soon as your view controller has been loaded.
Using the initWithNibName:bundle: method, your view controller will be automatically loaded and initialized.

iPhone : nib file + code = mix up

I'm getting a little confused about when I should use a NIB file and when I should use code.
Here's my problem :
I have a navigation based application with a RootController and its NIB file.
The RootController's NIB file contains a TableView.
When I click on a cell I initialize a new connection with a request to load content.
When the connection has finished loading I create a new postViewController (custom) from a NIB file and I push it on the navigationController viewController stack like that :
PostViewController *postViewController = [[PostViewController alloc] initWithNibName:#"PostViewController" bundle:[NSBundle mainBundle]];
[postViewController.webView setDelegate:self];
postViewController.postContent = [[postsData objectForKey:#"post"] objectForKey:#"content"];
[self.navigationController pushViewController:postViewController animated:YES];
[PostViewController release];
Then, as you can see I try to set the rootViewController as the delegate for the webView in order to be able to intercept a click on a link and push a new ViewController on the stack. I need that new view to have the navigation bar with the back button.
Problem : looks like the setDelegate isn't working because the webView:shouldStartLoadWithRequest:navigationType never gets called !
I guess I should set the delegate in the NIB file but I have no idea how. The NIB file from PostViewController doesn't know about the RootViewController...
Here are screenshots of the NIB files :
If you need more detail just ask me.
Thanks a lot...for helping me not banging my head for another day :)
Make sure postViewController.webView isn't nil when you call setDelegate: on it. Also make sure it isn't being called anywhere else, and make sure the delegate outlet isn't connected to anything in your NIB.
A couple other comments:
It's a bit unusual to use your root view controller as the webview's delegate in a case like this. It should work, but it might make more sense to move those delegate methods down into your PostViewController. From there you can still intercept the link clicks and call [self.navigationController pushViewController:animated:].
[PostViewController release] is releasing the class object. Not a good idea. Instead you should be calling [postViewController release].
[postViewController.webView setDelegate:self]; sets the delegate for the current Controller not postViewController. Try:
[postViewController.webView setDelegate:postViewController];
And you can put this line below pushViewController:animated: then the webView should already exist.