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
Related
I want to set a custom font on a UILabel, which I've figured out how to do just fine. My question is how do I access a UILabel that I created via the drag and drop interface builder in the code?
I've tried ctrl + clicking the UILabel and dragging it to the file's owner but it won't let me. I also tried opening the assistant editor and connecting the UILabel directly to the corresponding .h file, but that won't work either. How do I access the UILabel programatically?? I know this should be really easy.
// YourController.h
#interface YourController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *theLabel;
#end
// YourController.m
#implementation
#synthesize theLabel;
///....
#end
Now with the IBOutlet marker in your code you should be able to drag from File's Owner to the label (not the other way round) in IB.
Assuming you added the UILabel to your .xib file. ctrl click the UILabel and dragged it to your .h file. You will have to enter certain info including the name of the Label. Use the name of the label that you entered from y our .m file to access it.
By ctrl+click and dragging the UILabel from the .xib to the .h, you will basically add the UILabel as a property. It will automatically added implementation of it in the .m file so you'll be good to go.
If you want to modify the UILabel at any point, you need to declare it in the .h file, and the .m file where you plan to modify it.
When you declare it is should like roughly like:
#interface MyObject {
UILabel *_testLabel;
}
#property (nonatomic, strong) IBOutlet UILabel *testLabel
Then of course you would do,
#synthesize testObeject = _testLabel;
in your .m file. After you've done this, you can link the actual variable to your physical label in your .xib file the way you were attempting to before.
When I add new view, I always do same and boring work.
If I add UIWebView, I would do follows,
add codes to header file. ( declare, property )
add codes to source file. ( synthesize, viewDidUnload, dealloc )
add UIWebView in IB and connect to the outlet in File's Owner.
[ViewController.h]
#interface ViewController : UIViewController
{
UIWebView *_webView;
}
#property (nonatomic, retain) IBOutlet UIWebView *webView;
#end
[ViewController.m]
#synthesize webView = _webView;
- (void)viewDidUnload
{
[super viewDidUnload];
self.webView = nil;
}
- (void)dealloc
{
[_webView release];
[super dealloc];
}
What if I should add 3 labels and 2 buttons?
What if I have to add textview and some imageviews?
Don't you think it is boring? I would like to listen to your idea.
I hope there will be more easy and simple way to add outlet to the code.
Does anybody have a good idea? :)
When using Interface Builder, If you select an Object drag it's reference to your header, you'll see a popup where you can name it so theres less typing for you to do. As seen below:
This will automatically declare the IBOutlet UIButton *myButton for you, and insert the release and nil code into dealloc and viewDidUnload methods.
Same method also works for actions, as seen below.
Once you Connect it will automatically insert the new Action -(IBAction)cancelSelected:(id)sender into your #implementation class.
Point being, all that's boring for you to do can be done in 2 Reference connections, and inputting data into 2 fields. :)
Hope this helps!
In Xcode 4, you can simply control drag from a UI element to the .h/.m file. If you drag to the .h, Xcode will create a property for you and synthesize that in the corresponding .m, if you drag to the .m, Xcode will stub out an IBAction method for you.
Also, I would recommend switching to ARC, to avoid having to worry about memory management.
If you are having xcode 4.0 you can create outlet property by drag and drop.
Follow the steps:
open the xcode
open you nib file where you need to create an outlet.
Click on the middle tab of the Editor which at right up corner. It will open a new file adjacent to your nib.
Make sure it is the .h file of the controller where you want to create the outlet.
Now select the control, right click on it and drag to the .h file. Name the outlet. Thats it. It will create you a property and it will synthesize it automatically.
It will also insert the code for dealloc and viewDidUnload.
Hope this help.
while I'm stuck at this question I cannot find the right steps to add a UITabBarController to the AppDelegate (not programatically) but by using interface builder for the "Empty Application" template, I tried to add a new empty xib file, then dropped uitabbarcontroller into it, but there is no way to link it (from IB) to AppDelegate !! i.e. when I move the blue line from tabbarcontroller object (in document outline) to File's Owner, interface builder shows only the "Delegate" option in the shown list so there is no IBOutlet option in there.
so, what are the exact steps for adding a tabbarcontroller and connect it to appDelegate using the interface builder way (for the Empty Application template, using XCode 4.2 and IOS 5 SDK) ?
step1: create new Empty Application template project.
... waiting for the next steps...
thanks so much in advance.
Step 1: create new Empty Application template project.
Step 2: add
#property (nonatomic, strong) IBOutlet UITabBarController *tabBarController;
#property (nonatomic, strong) IBOutlet UIWindow *window;
in your app delegate. (dont forget to synthesize these)
Step 3: change this line in your app delegate:
#interface AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>
Step 4: modify this method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:[self.tabBarController view]];
[self.window makeKeyAndVisible];
return YES;
}
Step 5: create a new empty xib. Drag a tab bar controller on to it as well as an empty object.
Set the empty object's class to AppDelegate. Set Files Owner to UIApplication.
Step 6: drag the 'delegate' property from your files owner on to your appdelegate class and drag the tab bar outlet from you appdelegate class to your tabbarcontroller
Step 7: Add a window and drag the 'window' connection from your appdelegate to the window.
Step 8: Dont forget to go into the project settings and set the main-base nib file to the new xib you created.
Thats it. Hope I didn't miss anything.
I don't understand what is the need to add a UITabBarController through the main window. With Xcode 4.2, Apple has made some changes in the default templates. It does not provide a MainWindow.xib in the Empty Application. That is why, if you open up the AppDelegate.h, you will see:
#property (strong, nonatomic) UIWindow *window;
It does not have an IBOutlet for the window as we used to see in previous Xcode versions.
What you want to achieve is a UITabBarController with Core Data support. You could add the tabBarController programatically, and still add CoreData support.
Here's what I had to do.
From your storyboard's Objects list (Controllers & Objects) in the Utilities panel, drag over a generic "Object" (yellow cube) to your Tab Bar Controller Scene page (I place the object under the "Exit" object).
Set the class of the object to your appDelegate. Now you should be able to link your Tab Bar Controller's delegate to your appDelegate object. You can also link the appDelegate to the Tab Bar Controller if you've created a property such as
#property (weak, nonatomic, readwrite) IBOutlet UITabBarController *tabs
I have read other questions here, but they seem to be for Xcode 3.2 or earlier, but nothing for 4.2. :(
I started a simple project and was wanting to connect the File Owner's Outlets within my xib. The bummer is that my IBOutlet's from my ViewController.h aren't coming over.
I don't have a reputation of 10 or above, so here is a screenshot of my File's Owner not showing my IBOutlets.
Here is my ViewController.h code:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UITextField *txtName;
IBOutlet UILabel *lblMessage;
}
#property (nonatomic, retain) IBOutlet UITextField *txtName;
#property (nonatomic, retain) IBOutlet UILabel *lblMessage;
- (IBAction)doSomething;
#end
Here is my ViewController.m code:
#import "ViewController.h"
#implementation ViewController
#synthesize txtName;
#synthesize lblMessage;
- (IBAction) doSomething
{
NSString *msg = [[NSString alloc] initWithFormat:#"Hello, %#",txtName.text];
[lblMessage setText:msg];
}
#end
I am new to Objective-C and Xcode, so I could have made a mistake, but I've followed many tutorials and I can never get my IBOutlets to show. I have gone as far as to delete Xcode 4.2 and re-installed to try and fix this issue. Here is a screenshot of my Xcode Version 4.2, Build 4D199 info.
Anyone else run into this issue? Thanks anyone who can point out any mistakes I have made. Please let me know if more information is needed.
When you create your IBAction, in the .h file, there will be a connection indicator to the left of it. When it isn't connected it shows a empty circle.
Press and hold this and drag it to the item you want to connect it to. I usually open up the XIB in a new window by double clicking it.
If it wont connect you must set the File's Owner in the XIB file. Select File's Owner in the Placeholders panel. Move over to the Utilities panel and make sure the Custom class, in Identity Inspector, is set to what ever your viewcontroller is named.
I Hope this will help you.
Cheers!
Try to reassign your file owner class reference in xib file.
Then attach all your IBOutlet connections.
Hope this might be helpful to you.
Check if the Files Owner is set to "ViewController". Check the following link:
http://developer.apple.com/library/ios/#documentation/IDEs/Conceptual/Xcode4TransitionGuide/InterfaceBuilder/InterfaceBuilder.html
Two things need to be added, before Xcode will allow creation of IBOutlet for the text field from the Storyboard:
Assign the underlying ViewController as the delegate to the UITextField
Add the to the #interface declaration in the ViewController.h file:
#interface ViewController : UIViewController<UITextFieldDelegate>
Until both of these are completed, you can Ctrl-click and drag from the Storyboard to the .h file, but no IBOutlet connection will be enabled.
I've finally figured out the issue, hope this helps anyone else currently having the same problem.
It had nothing to do with the xib's file owner setting. What my issue was that I had the xib file in a different directory than the source files, thus it wasn't able to connect the outlets. Once I moved the files to the same directory, everything worked. FYI, I moved everything to the top directory. Not sure sub directories will work...
To move the files, be sure to update xcode to point to the new locations.
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:?