Cannot connect UIButton to ViewController in XCode4 - xcode

I'm learning iPhone Dev and I'm stuck on something. I'm writing a simple calculator program, but when I try to connect one of the buttons on the calculator to the File Owner in Interface Builder I do not get any options:
.h file
#interface CalculatorViewController : UIViewController
{
CalculatorBrain *brain;
IBOutlet UILabel *label;
}
- (IBAction)digitPressed:(UIButton *)sender;
- (IBAction)operandPressed:(UIButton *)sender;
#end
.m file
#import "CalculatorViewController.h"
#implementation CalculatorViewController
- (IBAction)digitPressed:(UIButton *)sender
{
//not implemented yet
}
- (IBAction)operandPressed:(UIButton *)sender
{
//not implemented yet
}
#end
As far as I understand when I'm working with my CalculatorViewController.xib in interface builder if I try to connect a button to the file's owner I should have two options:
1. digitPressed
2. operandPressed
However, I do not get any options. Any ideas what I'm doing wrong?
Thanks.

Check that file's owner is set to CalculatorViewController
In IB, click the button, then click the option on the far right (connections, I think it is called)
Find the action called touchUpInside. Drag from the circle next to this to your files owner, the two actions will pop up then.

Are you dragging from the button to the owner or the other way around?
If you want to refer to an object in a nib from code, then you define an IBOutlet and drag from the owner to the object.
If you want to refer to code from an object in a nib file, then you define an IBAction and drag from the object to the owner.

I encountered a similar issue.
The problem was that I renamed the ViewController.
The XIB file has the name of the view controller inside of it.
when it doesn't match the actual name of the UI View Controller class interface builder will act funny.
To fix it, open the XIB file as source code (right click it and use Open As source code)
it's an XML file. find the name of the old controller and then change it to the correct name.
I found it in the XML under:
<dictionary class="NSMutableDictionary" key="flattenedProperties">
<string key="-1.CustomClassName">xxxxxxViewController</string>

Related

how add a UIscrollView in a view controller ??

I have a view controller .h and .m and I don't know how
to add it to .xib
I pick a UIScrollView and I put it in the view of the .xib
but I can not relie his outlet to the declared UIScrollView in the viewController.h
Just trying to follow what you're trying to do. So you have presumably defined your UIScrollView in your header file as such:
#property(nonatomic, weak)IBOutlet UIScrollView *scrollView;
And now you need to link that to your XIB file? If you open the XIB file and click your main window of that view so the outside of it is selected, then you'll get properties available to you on the right hand side of your Xcode project. On the third tab the first field contains a place for you to select the UIViewController class you defined that UIScrollView. Select it. Then you can click+drag from your window to the UIScrollView in your XIB to connect it.

Can't CTRL+Drag NSButton to custom NSView header

I'd like to create a custom NSTableCellView instantiated by Interface Builder. I've set my Table Cell View class to MyTableCellView, and properly created MyTableCellView : NSTableCellView .m/.h files.
However, I just can't CTRL+Drag a simple button from inside this view to MyTableCellView.h in order to create an IBOutlet.
Here is a video to show what happens: http://youtu.be/sNNbuVT-SZs.
How the view is subclassed:
How I try to CTRL+Drag a button
Also, sometimes, Interface Builder just don't allow the cell view's class to be modified. What's happening ?
I finally found a solution, that is a little weird but works as expected. Instead of connecting the NSButton to MyTableCellView header directly, I used the reversed path:
Manually create an outlet:
#property(retain, nonatomic) IBOutlet NSButton* button;
Then click the empty circle on the left, and drag it to your XIB file's button:
I have no idea why it works this way, please let me know if you know the anwser.

Xcode MVC problem

I have created a baseview project in xcode4. I want to use a new xib file to instead of the old viewController.xib file. So I just change the old viewController.xib file's name. and I also change the MainWindows.xib, let the UIViewController direct to the new name one xib file.
All are ok, but the view are transparent in the MainWindows, like link the xib to UIViewController are not correct. I don't know..
My step are:
1. create a new xib file.
2. link the new xib file to MyUIViewController class
![I think link way is this,I don't know if is correct?]
Try other way:
If I use the xcode template to add sub UIViewController (include xib and class file), and add the code like:
testViewControllerViewController *Controller = [[testViewControllerViewController alloc] init];
self.viewController = Controller;
[Controller release];
//self.window.rootViewController = self.viewController;
[self.window addSubview:self.viewController.view];
[self.window makeKeyAndVisible];
The xib content are show correct.
If I change the name of xib file, the xib content are not show. Because I want to create the Class first, and use the old xib file.
Any body know how to resolve it: Create xib file and sub UIViewController separately.
If these are way to do this, There is a problem: I have two xib file and link to only one UIViewController, if I use the UIViewController, which xib is used?
So, I want to sure whether can we create the xib file and sub UIViewController separately?
Why we must set the Custom Class file to SUB UIViewController, show as the picture above.
I'm not sure if I understand the problem correctly, but are you asking why you must always add the Controller's view as a subview to the main window?
The iPhone hierarchy is quite simple: every App has a main window, with a controller. This controller has a View, most of the time.
So, to make your app do something, and show something you will need to add the Controlling logic (from the Controller) and the View that it's controlling to the main window, by calling
[self.window addSubview:[Controller View]];

IBOutlets not showing

I created three IBOutlets in a .h but when I go to Interface Builder and go to Referencing Outlets or right click on the object that I want to connect to one of the outlets. It just doesn't display the outlets I made. How do I find and connect the images in IB to these. My goal is to make custom buttons, I have the images out in IB and have set the highlighted state. And I want the images to auto switch to the highlighted state when touchupInside is triggered.
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController {
IBOutlet UIButton *brown
IBOutlet UIButton *red
IBOutlet UIButton *blue
}
-(void)brownPressed
-(void)redPressed
-(void)bluePressed
#end
If the code you posted is accurate, you are missing 6 semicolons. Interface Builder won't be able to properly parse the header file without them and so it won't show any IBOutlets.
Try changing the code to the following:
#interface FirstViewController : UIViewController {
IBOutlet UIButton *brown;
IBOutlet UIButton *red;
IBOutlet UIButton *blue;
}
-(void)brownPressed;
-(void)redPressed;
-(void)bluePressed;
#end
This answer is in reference to Xcode 4
If you changed the name of the .xib manually, don't forget to check these IB setting in the right pane:
Identity and Type: File Name
should be:
newName.xib
Custom Class : Class
should be:
newName
...
I was also experiencing this problem - after changing the .xib filename manually. I have no idea why renaming by refactoring was grayed out but that was my first attempted solution to rename. I then made some IBOutlets, but dragging to the File's Owner didn't give me any options to connect to it.
I right clicked on File's Owner and noticed some warnings telling me there was no IBOutlet by the name of the variable it tried to reference.
Basically, since I changed the .xib filename manually, at least one of the above configurations did not automatically follow (makes sense since I did a manual operation).
This happens to me try connecting the touch up inside connection of the button to files owner and then it will show up

Code Pattern: Loading TabBarController objects directly from independent .xib file (instead of from MainWindow.xib)

I've looked around online, and haven't been able to find an acceptable solution to this problem...
I'm looking for a simple code pattern:
Load a TabBarController object (with associated subview controllers) from a separate .xib file, instead of including and loading automatically from a default MainWindow.xib.
In XCode terms, starting from a new iPad/iPhone project as a "Tab Bar Application", the goal is to solve the following:
Create the project
Move: TabBarController, TabBar, FirstViewController, and SelectedSecondViewController from MainWindow.xib, into a new "TabBarController.xib" file
After moving, MainWindow.xib should only contain: File's Owner, First Responder, App Delegate, Window
In TabBarController.xib, File's Owner and First Responder are set to: UIApplication and UIResponder, respectively.
Change "didFinishLaunchingWithOptions" in the main application delegate to the following:
REMOVE:
[self.window addSubview:tabBarController.view];
ADD:
UITabBarController *uiTab = [[UITabBarController alloc] initWithNibName:#"TabBarController" bundle:nil];
[self.window addSubview:uiTab.view];
With these changes, the application builds and runs, but when the TabBarController loads the tab bar is "empty" -- there don't appear to be any contents in the controller.
In looking in the debugger, either the "init" isn't initializing from the data correctly, or something in the .xib file is not set correctly.
What is the correct solution to this? I realize there are other ways of doing this, and yes, I have them working in other applications.
What I'm looking for however, is a specific solution using the default project, that can be used as a general pattern for setting up iOS code.
Thanks in advance for any help
js
I think i know what you are looking for because i want the same thing.
Create New Empty xib file at interface builder.
Add to the xib TabBarController from the library.
Edit whatever you need on this tab bar controller on the xib.
Of course, save...
Determine from which view controller do want to create that xib with tab bar controller. In other words, who is the view controller that will cause this tab bar controller to appear.
Let's call that view controller ParentViewController
In that view controller, create an IBOutlet to a TabBarController.
Back to the xib, make the identity of the File's Owner to the ParentViewController and of course dont forget to hook up the outlet of the tab bar controller in the file's owner to the tab bar controller in the xib.
save the xib and you are ready to go.
When you want to present that tab bar, just decide which way you want to do it: Modally,Popup or something else (Not inside a navigation controller because Apple dont allow tab bar controllers to be inside navigation controllers).
When you decide, just present your tab bar controller outlet the way to present any other view controller. for example:
[self presentModalViewController:self.myTabBarController animated:YES];
Assuming you start with the "Tab Bar Application" template and move the UITabBarController and associated view controllers to a new nib as you described...
In your new nib, File's Owner should be set to your AppDelegate class. Then connect the outlet "tabBarController" of File's Owner to the UITabBarController.
Then in your -[application:didFinishLaunchingWithOptions:], do not remove this line:
[self.window addSubview:tabBarController.view];
Instead, load the new nib right before that with your app delegate as the owner:
[[NSBundle mainBundle] loadNibNamed:#"TabBarController" owner:self options:nil];
That will set your tabBarController property (since you made that connection in the nib) and then you can proceed as normal. What you were doing was actually creating a whole new UITabBarController, and not loading the one from the nib at all. (well, ok you were loading it for a brief moment, but then not doing anything useful with it)
Hope that helps.

Resources