Add subview not working? - iphone

I have two xib files:
MainView.xib and DetailView.xib
Both are controlled by MainViewController. MainView.xib loads when the app first opens, but if a user clicks on a button, the app loads DetailView.xib as a subview.
DetailView should load because I made an IBOutlet in the MainViewController to the view in the DetailView.xib file.
I am trying to use the addSubview command, but for some reason it is not actually executing the command. It will go through the command, but nothing will actually change. Here is the command:
[self.view addSubview:myDetailView]
where myDetailView is the IBOutlet
What is wrong with this setup?
Thanks for the help.
EDIT:
MainViewController.h (left generated code out):
IBOutlet UIView *myDetailView;
#property (nonatomic, retain) IBOutlet UIView *myDetailView;
MainViewController.m:
#synthesize myDetailView;

NSLog myDetailView before you add the subview, and if it returns "(null)" then the myDetailView has not been initialised. Make sure that you have connected the view in Interface Builder.

I bet myDetailView is nil.
It may be an IBOutlet, but it has to be connected. And the XIB in which you connected the IBOutlet should obviously be loaded.
How did you load the DetailView.xib in your code? Did you use loadNibNamed:owner:options:?

Related

Cannot connect Segmented Control to its IBOutlet

I have created a segmented control in the interface builder.
In my ViewController.h:
#interface ViewController : UIViewController <MKMapViewDelegate>
#property IBOutlet UISegmentedControl *Segment;
- (IBAction)switchMode:(id)sender;
#end
What I could do was to connect the Segmented Control with the IBAction but I cannot connect it with the IBOutlet!
Add a segmented control to the nib/Storyboard
Add the following code into the .h
#property(nonatomic,retain) IBOutlet UISegmentedControl *Segment;
In your storyboard or xib make sure that the files owner has the same classname as that of the class in which you written the outlet
Right click on the segmantControl and a window with outlets and actions appears
click and drag on the referencing outlet and drop it on the filesowner a new pop appears which includes your outlet written in code select it .
Connection established
you have forgotten to write parameters of property, correct it like below code
#property(nonatomic,retain) IBOutlet UISegmentedControl *Segment;
after this synthesize this property in .m file like this
#Synthesize Segment;
It appears that with some xCode update you can no longer connect certain outlets to your .h. You should be fine connecting it in your .m though:
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UISegmentedControl *mySegmentedController;
#end
I will read some docs and see when this changed. Also there really is no reason to connect this property to your public interface (hence why it is no longer allowed). Only the View Controller of that class should have control over it.

iPhone Subclassing view controller and IBOutlet UITableView

I have the following problem. I've created a ViewController pretty much like the above
#interface MyViewController : UIViewController {
IBOutlet UITableView *myTableView;
}
#property (nonatomic, retain) IBOutlet UITableView *myTableView;
I've linked myTableView on the Interface Builder to the matching nib's UITableView. and I've subclassed MyViewController to create YourViewController as so:
#interface YourViewController : MyViewController {
}
And then I load from a TabBarController the YourViewController on a tab item. Although I can see that MyViewController is indeed invoked at the end, no table view is displayed on the emulator.
I've tried debugging the MyViewController and it appears the the IBOutlet is nil.
Why is that?
I have encountered major issues with inheritance and IBOutlets and IBAction. I advise you to avoid that, and create shared stuff in another way.
I was hit hard by bugs when getting memory warnings for instance. Outlets and actions didn't get reconnected properly when they were defined in base class vs derived class.
Probably MyViewController's nib file is not loaded at all. Are you using for YourViewController a specific nib file? and in which way are you creating YourViewController.
Can you also try to define an "awakeFromNib" method in YourViewController and then from it call [super awakeFromNib] ?
However to understand your issue you must clearly explain how you load objects and if and where you use Nibs?
If you add it dynamically using code then only it will work. Not using Nib.
the UITableView (ie. your myTableView) needs delegates for data source and and its control.
And your controller needs a link to the Table view in the xib.
declare table delegate protocols in the interface section of your controller.
using IB, link the TableView in your xib to owners file:delegates.
using IB, link the file owner myTableView to the tableView in the xib.
I hope it will help.
Assuming that you have your whole navigation stack in MainWindow.xib
Open MainWindow.xib
Select YourViewController (Or whatever your controller that is subclassing UITableViewController is called)
Select Attributes Inspector
Ensure that the 'NIB Name' property has your correct YourViewController (Or whatever the name) selected
I had this exact same issue, this solved it for me.

Unable to link UITable delegate to File Owner in IB of xcode

I am facing an issue and that is -
I am using tabBarCpntroller in my application. In this there are just two views. Both the views have tables in that.
I am able to display and populate the table in the first view but as I try to connect the delegate of UITable in the IB for the second view and then run the code, it crashes.
PS: I am using the same code as in first view.
Kindly help me out with the suggestions, I would be highly grateful
Thanks in advance!!
By the looks of your question, you need UITableView, not UITable, if this is what you are using, then you will have problems.
in your header, you must define your UITableView as an IBOutlet.
.h
#interface YourViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableView *myTable;
}
#property (nonatomic, retain) UITableView *myTable;
#end
Once you have done this, build (only) your code, then Interface Builder will see that outlet and you can connect it.

Iphone SDK App UIImageView

I'm a beginner to Iphone Development :)
I am trying to make a button change an image. So I have my button
- (IBAction)myButton {
myUIImageView.image = [UIImage imageNamed:#"myPhoto.png"];
}
I created a UIImageView in IB and I named the label and the name of this 'myUIImageView' but XCode is telling me that it's undeclared. So my question is how do I associate this UIImageView with myUIImageView. Or perhaps how do I reference this UIImage in my myButton IBAction?
Any advice would help, thanks alot!
In your .h, you need this ivar between the curly braces
UIImageView* myUIImageView;
And after the close and before the #end, you need
#property(retain, nonatomic) IBOutlet UIImageView* myUIImageView;
and in the .m, after the #implementation line
#synthesize myUIImageView;
(release in your dealloc and viewDidUnload)
Now,
Open up Interface Builder for the .xib for this view controller
Click on File's Owner icon in the Document dialog
Bring up the Inspector
Go to the connections tab
You should see an outlet named myUIImageView with a circle next to it
Click and drag the circle to the UIImageView in your view (this connects the outlet to the view)
Save, close, rebuild
how to connect the UIImageView in interface builder with the outlet created in xcode named myUIImageView:
close interface builder and open up xcode. Heres what you need to write correctly in the following two files.
in XCode
.h file
#interface FirstViewController : UIViewController {
IBOutlet UIImageView *myUIImageView;
}
#property(retain, nonatomic) IBOutlet UIImageView *myUIImageView;
#end
.m file
after implementation you write
#synthesize myUIImageView;
release in your dealloc and viewDidUnload.
save the file in xcode then open up the xib file that is connected to the .h and .m files. For example i have firstViewcontroller.h and firstViewController.m then i have a .xib file called firstView.xib.
in Interface Builder
Now on the view drag a UIImageView and then in the document dialog you will see the file owners icon.
click on that and press CMD+2 to open up the inspector. Go to the connections tab and there will be an outlet named myUIImageView that we created in xcode. next to it is a circle which you click and drag to your UIImageView. This will connect the outlet in xcode with the imageview in interface builder.
Now save the file. close interface builder and rebuild your project.
Thats the first question answered once you ellaborated on question two i will help you with that.
Let me know if you need any more help.
PK

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.