Connecting a UILabel to an IBOutlet causes a crash - iphone

I have a UIViewController I am loading from a xib file and pushing onto the navigation controller stack.
In the header file for the View Controller, I have:
IBOutlet UILabel *myTitle;
I don't do anything with "myTitle" in the code yet; I was just setting up the connections.
When I compile and run the application, and there are no labels defined in the xib file (and thus, nothing attached to the IBOutlet), it works. The view controller animates into view just fine, showing the view I built in Interface builder.
If I add a label to the xib in the interface builder, but don't connect it to the outlet, and recompile, it still works, showing the label with the default text I entered for it.
But if I connect the IBOutlet myTitle to the label in interface builder, recompile, and run the app, it works fine until I try to push the view controller onto the navigation controller's stack, at which point I get a crash:
*** -[UILabel copyWithZone:]: unrecognized selector sent to instance 0x4558e20
If I disconnect the outlet again, it resumes working, showing the static label as before. It appears that there's something funky going on when the view gets displayed, because the crash happens when I push the view onto the navigation stack.
Am I not supposed to add an IBOutlet to a UILabel or something? Or is there something else going on? Any suggestions on where to look for trouble?

Yes, it could be caused by wrong param name, e.g. try this:
IBOutlet UILabel title;
and it'll call an exception like that:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UILabel copyWithZone:]: unrecognized selector sent to instance 0x143f520'

Did you create an accessor for your IBOutlet?
#property (assign) IBOutlet UILabel *myTitle;
And then, in your .m file
#synthesize myTitle

I had the same problem. Turns out *title is reserved and Xcode doesn't tell you this. Once I renamed it to something else it worked.

In my case I had a UILabel in a UIToolBar. What fixed it for me was to delete the toolBar and add a new one with a new UILabel. An IB bug.

Related

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.

ios StoryBoard - Subclass of UIImageView is showing up as a UIImageView rather than the subclass in IBOutlet variable

I have a subclass of UIImageView called SlideShowView. The SlideShowView .h and .m files are in a shared project that is referenced from my main project.
In my storyboard, I have an instance of SlideShowView connected to an IBOutlet in my ViewController.
When I try to call the SlideShowView method setImages: on that instance, I get this error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView setImages:]: unrecognized selector sent to instance 0x6b57360
When I do po [self slideShowView], which is defined as:
#property (weak, nonatomic) IBOutlet SlideShowView *slideshowView;, it shows up as <UIImageView: 0x6b57360; ....
So it seems that for some reason, it isn't being set as an instance of my subclass, but an instance of UIImageView.
UPDATE:
If I copy the SlideShowView.m and .h files out of the shared library to my main project, it works. So that makes me think it has something to do with storyboard not finding the shared library files.
UPDATE 2: I tried setting the customClass in the storyboard to fkdsfewnwef, and it didn't throw any error or anything, it just still shows up as a UIImageView. So now I'm pretty sure it has something to do with SlideShowView not being found in the linked static library project.
In the storyboard, select it and set it to be a SlideShowView in the Identity inspector. Otherwise, the storyboard cannot know what class it is intended to be.

weird error when connecting in Interface Builder

i am getting a weird error when i connect my outlets in my xib.
I have a tab bar app, with 3 tabs. the first two work perfectly, but the third tab is having problems - it crashes the app when you push it when any IBOutlets are connected in IB. if you remove the connections, but leave them declared in xcode then it works fine. but as soon as i connect them again, it crashes the app.
any thoughts?
********** added code *********
.h
#interface Orders : UIViewController {
UILabel *username, *customer;
}
#property (nonatomic, readonly) IBOutlet UILabel *username, *customer;
.m
#implementation Orders #synthesize
username, customer;
message from console
*** Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[
setValue:forUndefinedKey:]: this class
is not key value coding-compliant for
the key username.'
Your posted code shows the UILabels are called customer and username but the screenshot shows them called customerLabel and usernameLabel in the nib file. Make sure that you're using the same names for the views everywhere (I recommend appending "Label" to the end of the names if they are UILabels. Better to have descriptive names.).
And like Kirby says in a comment to your question, make sure that the File's Owner in the nib file is set to be an instance of your UIViewController subclass (looks like it's called "Orders" but it should really be something like "OrdersViewController").

Problem loading a Nib from a UITabBarController

I'm trying to load a Nib from a TabBarController. I'm doing this by assigning selectedIndex. The ViewController loaded by index is indicated in the MainWindow.xib where the TabBarController is, assigning the name of the Nib to be loaded in each Tab Bar Item. Loading works, but if I create an IBOutlet in the ViewController to be loaded and link it to anything (the IBOutlet's object can be of any class) then it crashes in the line where I assign selectedIndex.
Edit>>
I have the application delegate which contains a UITabBarController linked to a UITabBarController object created in MainWindow.xib.
In that UITabBarController object there are some Tab Bar Items each one linked to different UIViewControllers. These links are established through the property NIB Name.
I'm trying to test the first item linking it to an empty UIViewController, which in this case I called TestViewController (TestViewController.h, TestViewController.m and TestViewController.xib are created). I wrote TestViewController in the NIB Name property of the first Tab Bar Item. It works.
Now I put an IBOutlet UILabel in the TestViewController. I define it like this in the TestViewController.h:
#import <UIKit/UIKit.h>
#interface TestViewController : UIViewController {
UILabel *label;
}
#property (nonatomic, retain) IBOutlet UILabel *label;
#end
I then synthesize the label object in the TestViewController.m. I place a UILabel in the TestViewController.xib. It still works.
Then I link the label object defined in the TestViewController to the UILabel I created in the NIB file. I compile and test. It doesn't work. It crashes in the part where I assign the selectedIndex to the UITabBarController defined in the application delegate. The assignation is made like this:
self.tabBarController.selectedIndex = 0;
I use 0 because I'm testing only with the first Tab Bar Item. It crashes with SIGABRT signal.
Any ideas why this could be happening?
Your question is significantly improved by the edit. It's hard to say definitively why the crash happens given what you've told us, but I can point you in the right direction.
You've found the line that causes the crash, which is a good start. Place a breakpoint on that line and debug the app. When you hit the breakpoint, look at self.tabBarController. Is it nil? If not, how many objects are in its viewControllers array? If there's one or more, take a look at your TestViewController. Put a breakpoint on its -loadView method, or on UIViewController -loadView. Even if you can't see the source code for -[UIViewController loadView], you can still watch its effect: by the time you get to the end of the method, the view controller's view property should be non-nil.
Also, look in the console after this crash happens. There's usually an error message there telling you roughly why the app crashed.
The problem is solved. I forgot to change the UIViewController linked to the Tab Bar Item to TestViewController.

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.