Xcode: Button clicked results in NSInvalidArgumentException - iphone

I'm working in Xcode 4.3.2.
I am new to Xcode. I'm building an application that must change to different views on button clicks. My files are: AppDelegate.h/.m, GreenViewController.h/.m, SwitchViewController.h/.m, GreenView.xib - I am not using storyboards, but my project demands that I don't use them (backwards compatibility issues).
Here's my problem (it seems very simple): I'm trying to print to the console when a UIButton (placed in GreenView.xib) is clicked. Here's my code for GreenViewController.h
#import <UIKit/UIKit.h>
#interface GreenViewController : UIViewController
- (IBAction)switchViews:(id)sender;
#end
Here's my (deprecated) code for GreenViewController.m:
#import "GreenViewController.h"
#implementation GreenViewController
- (IBAction) switchViews:(id)sender {
NSLog(#"Button Pressed!");
}
The owner for GreenView.xib is GreenViewController.
For some reason I have the error only when the UIButton (in GreenView.xib) is pressed:
2012-10-09 18:07:38.490 MyViewSwitcher[8655:f803] -[SwitchViewController switchViews:]: unrecognized selector sent to instance 0x688a660
2012-10-09 18:07:38.492 MyViewSwitcher[8655:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SwitchViewController switchViews:]: unrecognized selector sent to instance 0x688a660'
It seems that SwitchViewController is expecting something from the method "switchViews", but "switchViews" is only listed in GreenViewController. Before, I had "switchViews" in SwitchViewController, but I deleted all the code corresponding to the method & all the connections. Again, I've double checked that "switchViews" in GreenViewController is connected to the UIButton found in GreenView.xib. I've already cleaned & re-built my project & I still get this error.
Thanks for your help!

What your error is saying is that you are calling the switchViews: method on an instance of SwitchViewController. And since there is no definition for switchViews: (because you deleted it) for the class SwitchViewController, it doesn't know what to do, and crashes.
Hate to tell ya this, but your button is connected to the switchViews: method of a SwitchViewController. You say "I've double checked that 'switchViews' in GreenViewController is connected to the UIButton found in GreenView.xib". Well, yea, from your crash, it is. But are you sure its connected to the switchViews: function of a GreenViewController instance? How did you check this information?
What I would suggest is deleting all connections to the UIButton in the connection inspector. Then reconnecting it to the view controller (which you say is a GreenViewController). It should then bring up the list of IBActions which should only be the switchViews: method.
If you do that, and it still doesn't work. Try deleting the button and remaking it, then reconnecting it.

Related

Using UISegmentedControl in Xcode as RadioButtons

I have just recently started teaching myself Objective-C and iphone/ipad development. With my only prior experience in coding involving Java/Android, I have been having some problems with this new language.
I am trying to use UISegmentedControl in this app to decide which view will be shown when the "members" button is pressed. I do this by grabbing the current selected text from the UISegmentedControl, named "memberPicker", when the "members" button is pressed and then check if it is either #"Current" or #"Alumni" to decide what view is shown.
This function appears to be working correctly, however, when I click the "Alumni" button in the UISegmentedControl, the app crashes with the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainPageViewController MemberSegmentedControl:]: unrecognized selector sent to instance 0x7557250'
I believe my problem is with declaring or setting my UISegmentedControl properly, I understand it can take an array and ect. but I only need it to alternate between selecting Current or Alumni, and then my "members" button will do the rest. Any ideas on how to resolve this issue?
Here is the code from my .m view
#import "MainPageViewController.h"
#import "AKPsiAppViewController.h"
#interface MainPageViewController ()
#property (weak, nonatomic) UISegmentedControl *memberPicker;
#end
#implementation MainPageViewController
#synthesize memberPicker = _memberPicker;
- (IBAction)membersButtonPressed:(UIButton *)sender
{
if([[self.memberPicker titleForSegmentAtIndex: self.memberPicker.selectedSegmentIndex] isEqualToString:#"Current"])
{
[self performSegueWithIdentifier:#"Current Member Segue" sender:self];
}
}
#end
You probably have connected the UISegmentedControl in Interface Builder to a IBOutlet named MemberSegmentedControl that you later removed in the #interface of your class. Check the connections to the UISegmentedControl in Interface Builder if there is not an additional connection besides the memberPicker.
Hope it helps!
The error you are getting means that you are calling the IBAction/function MemberSegmentedControl: which does not exist in the .m file. Maybe you had previously created this function and connected it with Value Changed of UISegmentedControl but you did not write its implemention (in .m file). Maybe you have deleted this but the connection still exists. To check this, please follow the following steps.
Open Interface Builder.
Click on the UISegmentedControl.
Open Connections Inspector (Click on View Menu, Click on Utilities and then Click on "Show Connections Inspector". Alternatively you can press Command+Options+6)
Under Sent Events, check if there is any of the events (especially Value Changed) connected to "File owner's MemberSegmentedControl:". If yes, then remove it by clicking on the x button and then try again.
If your problem doesn't get resolved, please do the following
Share a screenshot of the Interface Builder file (XIB file)
Share the header file (.h file)

"this class is not key value coding-compliant for the key...."

for the past few days i keep on getting this error everytime i run a build in xCode:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIApplication 0x1dd4a1c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key playBtn.'
at the begining it was in something i have made.
than i deleted it and it was still there - in an object (like this playBtn here) that did not exist anymore.
and now - in every single app i am trying to debug.
if on my iphone or in the simulator - the same.
i just can't run anything now !!
*clean doesn't work ! *
what do i do? i can't work like this..
notice:
solved. it was a problem with the appdelegate.m
thanks for who helped.
Check your IBOutlet connections in your XIB
You said playBtn does not exists, so check if connection still exist for the same, They will show ! mark in place of dot in connections.
I bet that You've got your File's Owner stuff messed up in your xibs.One of your views is set up to be the playBtn of the the File's Owner. However, when it's time to unarchive the nib, the owner doesn't have an playBtn property, so the unarchiving is failing.
Two Things you can do :
1) Check your "Connections Inspector" tab for the view that's throwing the error.
Remove all the Outlet Connections and re-connect all the Outlets.
2)
GoodLuck !!!
The problem is that you are indirectly (presumably when one of your nibs is unpacked) calling -setValue:forKey: on your UIApplication.
You can reproduce this issue by creating a new empty iPhone project and adding the following line to your app delegate's -application:didFinishLaunchingWithOptions:.
[[UIApplication sharedApplication] setValue:[[UIButton alloc] init] forKey:#"playBtn"];
Without a backtrace or even a complete log, it's hard to be sure, but it looks quite likely that you are having the same issue that is described here.
I solved it.
it was a problem with the appdelegate.m with the uitabbar.
thanks for who helped and those who tried :)

Works in iPhone Simulator, but not on Phone

So I have an app which runs fine on the simulator, but not on the actual device.
At runtime, it gives me the following error:
2010-12-05 19:58:32.006 Sports[4668:307] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableView isEqualToString:]: unrecognized selector sent to instance 0x800800'
A bit about the structure: I have a Tab Bar Controller, the first view being a UINavigationController.
The view in there is a UITableView. One thing that may or may not be related is that if I do #synthesize tableView; in the table view controller, the table stays blank on both simulator and phone (but does not crash).
If I take that out, it loads the correct data on the simulator, and crashes on the phone.
Where should delegate/dataSource be linked to in the Interface Builder? I tried linking it to "View" to "File's Owner", and making a new "ViewController" and none of those worked.
Both the delegate and dataSource should be linked to File's Owner, which is the view controller class that declares the table view as an IBOutlet; this should be the same view controller that owns the nib file. Additionally, that view controller should be implement the UITableViewDelegate and UITableViewDataSource protocols.
Add that #synthesize line back in, (unless you're subclassing UITableViewController, as pointed out by grahamparks in the comments!) make sure those connections are right, and, finally, make sure you've declared an IBOutlet for the table view, and connected that properly between your class and interface builder.
Found it!
Turns out that there wasn't really a problem with this at all. The problem was that the date field in my database, when run on my phone was always zero/nil.
Why? Because the NSDate object created never initialized and stayed at nil.
Why?
Because my phone is in 24 hour time and did not parse the am and pm properly.
Lessons learned!
Run your app with NSZombieEnabled set to yes. See http://www.cocoadev.com/index.pl?NSZombieEnabled for an explanation about it and how to set it. I think what you will find is that the app will now throw an exception in the simulator telling you that you are sending a message to an object that has been released.
Based on the error message you are receiving, I expect the culprit is a NSString or NSMutableString object. If it is an NSString then warning, that NSString could be shared by several different objects so figuring out where the extra release is might be hard.
Never fear though, Instruments helps tremendously in this regard. Here is a link that explains how to use Instruments to find out exactly where your object is being retained and released so you can track down which release is inappropriate. http://www.markj.net/iphone-memory-debug-nszombie/
Good luck!

Universal app iPad version IBAction throwing NSInvalidArgumentException unrecognized selector

For some reason I cannot figure out I have an IBAction code wired up to fire on the touchupinside for a UIButton on a view using Interface Builder. Simple stuff right?? Well it works fine in my iphone version but for some reason I keep getting the following error when I click on the button in the iPad even though I have it wired up properly. IB has the the correct class name for files owner, it's wired to the action. I've checked and double checked my connections, rewired them from scratch, cleaned classes, deleted the app from the simulator and googled to the end of the internets.
Why would this be any different between ipad and iphone version? Can someone please help me before I go insane on something that should be working but just refuses to work???? Even weirder it seems whatever I try to wire up to an action is throwing a similar error. I'm starting to think Interface Builder is just crazy or something.
2010-08-15 14:19:13.372 AppName[28524:207] * -[UIViewController doClick]: unrecognized selector sent to instance 0x4d7fac0
2010-08-15 14:19:13.373 AppName[28524:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIViewController doClick]: unrecognized selector sent to instance 0x4d7fac0'
UIViewController doesn't respond to doClick: which is what you are seeing here.
In the nib file for your view, have you set the "File's Owner" to the class that you inherit from UIViewController and contains the doClick: method?

Adding UINavigationBar to a UITabBarController

Ok so I actually got to add UINavigationBar inside a UITabBarController using the following tutorial: http://twilloapp.blogspot.com/2009/03/how-to-embed-navigation-controller.html
But now the issue is that whenever I add a new button inside one of the views it just crashes.
For example:
In the first view called FirstViewController I added:
IBOutlet UIButton *test;
Than I also created:
- (IBAction) doSomething:(id)sender;
I hook up the test button with the UI in interface builder. But when I run i get:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x3b12e80> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key test.'
Not sure what's going on with this.
Does anyone know of a solution?
Also, does anyone know where can I find good pre-built templates for these kinda apps so I can just start working rather than editing and setting things up.
Thanks
When you unarchive the nib file at runtime, it's going to invoke setTest:aButton on your controller, which will redirect to setValue:aButton forKey:#"test". Since it's throwing an exception that it doesn't know what the key #"test" means, chances are you forgot to put the #synthesize test; in your FirstViewController.m file.