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

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.

Related

Unrecognized selector NSInvalidArgumentException: for IBAction?

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.

Xcode4: How to create a menu for a game?

I have a game with 4 game modes. the problem is that I want to create a main menu to choose a mode, because instead of that menu there is a TabBar. I'm trying to do that putting each mode in different .xib files, and create an other .xib file for the menu.
Menu.m:
-(IBAction)PlayMode1:(id)sender{
ViewController *Mode1 = [[ViewController alloc] init];
[self presentModalViewController:Mode1 animated:YES];
[Mode1 release];
}
with this I'm getting this error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '- [UIViewController _loadViewFromNibNamed:bundle:] loaded the "ViewController" nib but the view outlet was not set.'
I checked that "view" is linked to "View" on IB so I don't know what to do...
I'm not an advanced developer, thanks for helping!
You should try to do the following:
-(IBAction)PlayMode1:(id)sender{
ViewController *Mode1 = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self presentModalViewController:Mode1 animated:YES];
[Mode1 release];
}
Seems like you forget to put initWithNibName inside the allocation of the view.
Also, make sure that your ViewController.xib is the correct name of the view controller and check if it has the correct outlets linked.
Check View Identity > Class Identity is set too. If not, type in the name of your controller, ViewController
EDIT
Remove everything in the xib, "Window" and "ViewController".
Drag an UIView on the left column and set its outlet.
Click on File's Owner and then, on the Inspector, Identity inspector section (third one from the left), type in the class of your view, `ViewController´.

Reload and display a tableView whitch is based on a view in the MainStoryboard

I'm searching for a solution for the upper since last week, but I found no solution on the internet. So I thought I'll get help here.
Now to may question: I have a MainStoryboard and in it a ViewController with a View. On that view I've put a tableView.
ViewController.h
IBOutlet UITableView *tableView
//on bottom
#property (nonatomic, retain) UITableView *tableView;
ViewController.m I've synthesized it and now I would like to reload and display it while the view is shown.
The following things I tested with no result: [tableView reloadData]; [self.tableView reloadData]; [self.tableView setNeedsDisplay];
And if I used [self.view setNeedsDisplay]; I've got the following Error:
Terminating app due to uncaught exception 'NSInternatlInconsistencyExeption', reason: 'Could not load NIB in bundle: 'NSBundle <...> (loaded)' with name 'ViewController'' * First thow call stack: (...) terminate called throwing an exception(lldb)
Thats right, there is no NIB file, because I've used the Storyboard. Well have anyone an idea?
Only thing I've found is, that if I reload the view by a button on the view within the Storyboard, the tableView gets reloaded - complete reload - but that is no solution.
Can I do something for that in -(void)prepareForSegue:(UIStoryboard *)segue sender:(id)sender? That method I'm using only for an popover till now.
Have you actually connected the IBOutlet up in Interface Builder?
Xcode should tell you if it's connected: it'll show a little circle in the margin of your header file, and if that circle has a dot inside it it's connected, if not then it isn't.
To connect an outlet in Interface Builder, right-drag from the thing with the outlet (i.e. your view controller) to the thing you want to connect it to (your table), and when you let go it gives you a menu of possible outlets to choose from.

Crash when instantiating ViewController from Storyboard

I have got a single view in my storyboard, which I add to my current view by doing the following :
MainViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:#"MainController"];
[self.view addSubview:mvc.view];
The view appears, but everything I do after it appears, leads to a crash. What am I doing wrong ?
Here is an example when it crashes:
-(IBAction)showUsername:(id)sender{
[testLabel setText:#"username"];
}
Everything is hooked up in storyboard as well, so falsely linked connections should not cause the problem.
You instantiate a new view controller:
MainViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:#"MainController"];
But you do not retain it. Your view hierarchy is, as soon you added it to another view.
[self.view addSubview:mvc.view];
So when a button is clicked, a message is sent to you IBAction, but your view controller has been released already. To prevent this from happening, retain your mvc variable, for example somewhere in a property.
#property(nonatomic, strong) MainViewController *controller;
self.controller = mvc;
I can think all reason before you show log...
Turn NSZombie on in the Product>>Edit Scheme you should get more descriptive Error showing then. Then you can add it.
Make sure your method is declared and implemented correctly. Also make sure you have IBOutlet UILabel * testLabel in your .h. The only other problem I can think of other than that is how you hooked it up. Does it only crash when you press the button?
This line is wrong this will be why you are getting the error.
MainViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:#"MainController"];
[self.view addSubview:mvc.view];
replace it with this
MainViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:#"MainController"];
[self presentModalViewController:mvc animated:YES];
In storyboards you are not adding a subview you are doing one of three things presenting a modal, pushing it on to the navigation controller stack or making a custom one of these.

Connecting IBOutlet to ViewController managed by a UITabBarController

I get a crazy error and I am unable to see why it happens.
I have made a simple app that uses a TabBar to navigate 3 views. I created everything and added one UIImageView to each of the 3 ViewControllers that the TabBar manages. Everything works fine. In the app you are able to navigate the 3 views and see the 3 images.
Now I add one UIButton (or any other component) to the 1st ViewController. I add it in the NIB and in my code I do the usual:
IBOutlet UIButton *btn;
#property (nonatomic, retain) IBOutlet UIButton *btn;
#synthesize btn;
[btn release];
and connect the UIButton in my NIB to the "btn". Now the app crashes as soon as the TabBar tries to show this view (which is imediately after it launches) giving me a:
2009-08-24 16:52:25.164
AppName[2249:207] *** Terminating app
due to uncaught exception
'NSUnknownKeyException', reason:
'[
setValue:forUndefinedKey:]: this class
is not key value coding-compliant for
the key btn.'
I tried restarting the SDK, my computer, building for 2.2.1, 3.0, for simulator, for device, cleaning all targets, etc but the problem remains. I am sure it has something to do with the UITabBarController. I just can't see what.
I had a similar problem. It was because the UIViewController for the tab was not set to the specific subclass of UIViewController I had created.
If you look at the .xib in IB you'll see something like this:
**Name** **Type**
File's Owner UIApplication
First Responder UIResponder
Tab Bar Controller UITabBarController
Tab Bar UITabBar
View Controller UIViewController
View Controller UIViewController
View Controller UIViewController
The View Controllers under the tab bar controller will default to a basic UIViewController. You need to change their class to your subclassed view controller in order for them to load and connect to your outlets correctly:
**Name** **Type**
File's Owner UIApplication
First Responder UIResponder
Tab Bar Controller UITabBarController
Tab Bar UITabBar
FirstTab View Controller MyFirstTabController
SecondTab View Controller MySecondTabController
ThirdTab View Controller MyThirdTabController
Then when the tab creates the controller for your tab it will be creating the correct class. Note, this is probably why your viewDidLoad method is not called
You will get that error when you connect a control to an outlet that doesn't exist.
The most important part of your error message was left out (because it was wrapped in angle-brackets):
reason: [<classname> setValue:forUndefinedKey:
classname is the class you, perhaps inadvertently, hooked up the button to in Interface Builder. It doesn't have a btn outlet.
The problem seemed to have been caused by the UITabBarController in my MainWindow NIB. I couldn't fix it so I deleted the UITabBarController from the NIB and created it in code in my AppDelegate class. Then I set all me other classes to "initWithNib" and I can now set IBOutlets in them just fine.
I'm presuming your 4 lines of code are all in the right place? Worth double-checking, as it looks like the compiler is thinking you intend something different for btn.
So, worth double checking that your 4 lines appear as follows:
// in your myController.h file
#interface myController:UIViewController {
IBOutlet UIButton *btn;
}
#property (nonatomic, retain) IBOutlet UIButton *btn;
#end
and
// in your myController.m file
#implementation myController
#synthesize btn;
- (void)dealloc {
[btn release];
[super dealloc];
}
#end
I expect you've put all the bits in the correct place, but like I said it's just worth double-checking!
I recently encountered this exact error, and found that just doing a Clean All targets fixed the problem. Might be as simple as that.
I had this problem today. Cause:
I had defined IBOutlet in my delegate, and connected it. Later on I removed it from code file. However, the reference was still active in XIB file. It was grayed out. Removing the reference in XIB helped.
I was doing something else so I thought the problem was in the new component I added, not realizing it was caused by this.