Xcode MVC problem - xcode

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]];

Related

Objective C - XIB load in NSView

I'm new to Objective C coding and the MVC concept gives me some pain. My goal is to develop a Mac Application. I was able to create a .xib for the main window. It starts well.
My main NSWindow has its main NSView. In this NSView, I put 3 subwiews (using Interface Builder). What I want is to change the content of the 2nd subview (by loading the content from another XIB file, called SubWindow1.xib) when I click on a button placed in the 1st subview.
I'm able to catch the click and tried some "xib loading" code but it doesn't work (the content never appears in my 2nd subview).
What I did is:
put the NSButton (on the 1st subview)
put a NSObject on Interface Builder (class renamed CtMainWindow, "Ct" for Controler)
link the IBAction (changeToSubWindow1) and IBOutlet (vSubView2 which is my 2nd subview)
create the CtMainWindow class, create a VwSubWindow1 class (which extends NSView)
In changeToSubWindow1(), I wrote:
VwSubWindow1 * vProfile = [[VwSubWindow1 alloc] initWithFrame:vSubView2.bounds];
[vProfile loadXib];
[vProfile.superview addSubview:vProfile];
The loadXib() function does:
NSNib * nib = [[NSNib alloc] initWithNibNamed:[self #"SubWindow1"] bundle:nil]; //#"SubWindow1" is the name of the expected loaded xib without .xib extention
[nib instantiateWithOwner:self topLevelObjects:nil];
And Voila. I was hoping that loading the sub xib in a view, and putting that view in my 2nd subview, it would appear.
I also tried to put only the following code in changeToSubWindow1()
VwSubWindow1 * vProfile = [[VwSubWindow1 alloc] initWithFrame:vSubView2.bounds];
[vProfile loadSubWindow];
The loadSubWindow() function does:
[NSBundle loadNibNamed:#"SubWindow1" owner:self];
Without more success.
I already read lots of threads from stackoverflow and tried lots of solutions but none of them worked for me.
Can anyone give me the hint I need in order to load and display the Xib in a subview ?
Thanks in advance.
you can try this method
you can put 3 xib files and 3 windows. connect each window
self.packages = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self.window1.contentView addSubview:self.vc.view];
self.packages.view.frame = ((NSView*)self.window1.contentView).bounds;
[window1 makeKeyAndOrderFront:NSApp];

How to programmatically subclass a UIViewController stored in a Storyboard?

I'm working on my first iPad application and using Storyboards for the first time.
I've got UITableViewController in my Storyboard that uses "Dynamic Prototypes" for custom cells.
What I want to do is programatically instantiate my UITableViewController subclasses but loading the super view controller from the Storyboard.
This is because I have a single UI but multiple subclasses for specific functionality that I need for each different instance.
This was very easy to do with .xib files, I would write the following code:
MyViewControllerSubClassA *viewControllerA = [[MyViewControllerSubClassA alloc] initWithNibName:#"generalViewControllerNib" bundle:nil];
MyViewControllerSubClassB *viewControllerB = [[MyViewControllerSubClassB alloc] initWithNibName:#"generalViewControllerNib" bundle:nil];
I know I can assign a subclass in the Storyboard editor when clicking on the View Controller but I want to set the subclass programmatically when instantiating.
This seems impossible to do with Storyboards as they are instantiated automatically.
This makes the entire concept of Storyboards seem flawed and not very OO.
If I move the View Controller out of the Storyboard and into a .xib file I lose the ability to use Dynamic & Static Prototypes cells as these are supported only when using Storyboards. Also Apple's documentation basically says use Storybaords from now on.
I would try something like this:
MyViewControllerSubclassA *controllerA = (MyViewControllerSubclassA *)[self.storyboard instantiateViewControllerWithIdentifier:#"myGenericVC"];

How to make UIScrollview with xCode 4.2 and storyboards

I am new to iOS Development and am wondering how to put a scrollview in a storyboard, using Xcode 4.2. I want the content to be 1280 by 460. This code all works well, but when I go to wire up the outlet, there is no file's owner, so i'm stumped. Here is the code I have:
in the .h file-
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UIScrollView *scrollView;
}
#end
and the .m, under viewDidLoad:
- (void)viewDidLoad
{
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(1280,460)];
[scrollView setPagingEnabled:YES];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
If anyone could help me, that would be great!
Storyboards do not have a File's Owner. You need to use the View Controller to connect the Outlets instead. I.E. Drag to the View Controller in the same way you used to drag to the File's Owner.
OK, I figured it out. I just had to create a class for my view controller. I was trying to use files that were not part of a class that was a subclass of UIViewController.I had to create a new class, then copy the old code in and make my view controller's class that of the NEW files, and THEN wire up the outlets. I was changing the class to something that was not there. I then wired my outlet up to the scroll view I wanted. Thankfully, that part is finally over! Thanks for the suggestions, guys, I really appreciate it.
A good visual tutorial is in the current Stanford CS193p course for iOS 5 in iTunes U.
This course works mainly with storyboards and they cover among other things UIScrollViews

Cannot connect UIButton to ViewController in XCode4

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>

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