I'm fairly new to iOS development, but catching on pretty quickly.
I'm attempting to figure out how to create universal apps from the window-only template in xcode. I THOUGHT that i could add a main view to the main_window.xib by following these steps:
Make a new window-based app template.
Go to file > new file > uiviewcontroller subclass with XIB file.
Open the main_window.xib and add a new view controller, with my new uiviewcontroller subclass as the selected NIB name in the inspector.
Control-Drag from the window object to the new view controller, and add it as the rootViewController.
I thought that from here i had something that was essentially the same as the view-based template, but when i add in a segmented view controller, add the IBOutlet/IBAction in code, and then hook up the outlets and received actions in Interface Builder, the app crashes as it launches every time.
I'm positive that i'm missing a vital step in hooking up this process and would be greatful if anyone could offer the solution, as well as some general advice when setting up these sorts of things?
Thanks.
EDIT: Solved it by doing the following:
Create new window based template.
Create UIViewController Subclass, name it whatever you want.
In AppDelegate.h, add #class YourViewControllerName before #interface
Inside the #interface for appDelegate, add YourViewControllerName *mainViewController;
Then outside the #interface add #property (nonatomic, retain) IBOutlet YourViewControllerName *mainViewController;
In AppDelegate.m add in #import YourViewControllerName.h at the top.
Add #synthesize YourViewControllerName.
In ApplicationDidFinishLaunching add: [self.window addSubView:mainViewController.view]
Open MainWindow.xib in interface builder, drag in a new view controller from the library, and use the property inspector to change it's class to be YourViewControllerName, and select the corresponding NIB file from the drop-down menu.
Control drag from the app delegate, which is the yellow box in IB, to your new;y created view controller, and hook up the mainViewController outlet you created.
VOILA! done. Solved all my problems.
Many many thanks for the help guys.
Make an IBOutlet for your custom view controller, called viewController of type MyViewController (or whatever you want your class to be named) in your app delegate, and make MyViewController subclass UIViewController. Next, in the MainWindow.xib file, add a new view controller from the library, being sure to set the class of this view controller to MyViewController (or whatever your class name is). Next, hook up the viewContoller outlet to the view controller in the MainWindow.xib file, and in your applicationDidFinishLaunching method, add this:
[window addSubview:viewContoller.view];
That should do it!
this s my documentation ,this may helps you.....
create Window Based Application (name as your wise)
2.create UIviewcontroller class(.h & .m) with nib file
3.open appdelegate.h and import " view controller .h"(which created in step2)
ADD #class [view controller class name] before (#interface appDelegate )
ADD within #interface appDelegate
view controller class name *alias Name;
#property (nontoxic,retain)IBOutlet view controller class name *alias Name;
4.open appdelegate.m
1.#synthesize aliasname;
2.
-(void)applicationDidfinishLaunching:(UIApplication *)application
{
[window addsubView: aliasname.view];
[window makekeyAndVisible];
}
5.open mainwindow.xib
1.add UIviewcontroller from library
2.open property for UIviewcontroller ,add nib file name and class name
3.link window object with Uiviewcontroller using property
Related
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'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.
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.
I am trying without success to load PatientListTableViewController from a button on my first page. I have a tab bar item to get to this page already. However I want this same page accessed from a button on my start page because PatientListTableViewController is a tab item that is not seen on the tab bar when the user first opens the page. How would I code this?
The header file of PatientListTableViewController includes:
#interface PatientListTableViewController : UITableViewController <PatientAddDelegate, NSFetchedResultsControllerDelegate, UINavigationBarDelegate, UITabBarControllerDelegate>{
Thanks in advance for any help
The header file of PatientListTableViewController includes:
#interface PatientListTableViewController : UITableViewController [PatientAddDelegate, NSFetchedResultsControllerDelegate, UINavigationBarDelegate, UITabBarControllerDelegate] {
If you are already setting this view controller as a sub-view-controller of your UITabBarController, I'd use either setSelectedIndex: or setSelectedViewController on the UITabBarController. If you don't do it this way, you're going to have issues because you may have loaded that view controller's view in two different places (under the tab bar, and wherever you're placing it when you load it manually).
Here's some sample code. I'm assuming that your application delegate has an instance variables myTabBarController, and that you're putting two view controllers in the UITabBarController: myFirstViewController and mySecondViewController. I'll assume that you've set both of these view controllers as children of the tab bar controller in your NIB file.
- (void)buttonPressed:(id)sender {
[myTabBarController setSelectedViewController:mySecondViewController];
// Alternately, you could set the index rather than the actual VC:
// [myTabBarController setSelectedIndex:1]
}
If your issue is related to loading a view controller without a NIB file, make sure you're setting up your view hierarchy correctly in that view controller's loadView method.
Solution above did not work for me after implementing this code into myFirstViewController:
import "MyFirstViewController.h"
import "MyAppDelegate.h"
#implementation MyFirstViewController
#synthesize tabBarController;
(void)buttonPressed:(id)sender { [tabBarController setSelectedViewController:patientListController]
In my MyAppDelegate.h file I have:
IBOutlet UITabBarController *tabBarController;
IBOutlet MyTableViewController *patientListController; .... end
In my MyAppDelegate.m file I have:
#implementation MyAppDelegate
#synthesize patientListController;
#synthesize tabBarController; ... end
Did I miss something here?
I am new to the iPhone SDK and am trying to create 3 views and switch between them. Data will come from a server and I will basically be showing 1 view and caching the other two. So far I am just trying to create a view and display it at run-time. My code is listed below. It shows only a blank screen and I think I am missing a key concept. Any Help?
#import <UIKit/UIKit.h>
#import "ImageViewController.h"
#interface Test5ViewController : UIViewController
{
IBOutlet UIView *rootView;
ImageViewController *curImage;
ImageViewController *nextImage;
ImageViewController *prevImage;
}
#property(nonatomic,retain) IBOutlet UIView *rootView;
#property(nonatomic,retain) ImageViewController *curImage;
#property(nonatomic,retain) ImageViewController *nextImage;
#property(nonatomic,retain) ImageViewController *prevImage;
#end
and
- (void)loadView
{
self.curImage = [[ImageViewController alloc]initWithNibName:#"ImageView" bundle:[NSBundle mainBundle]];
UIImage *pic = [UIImage imageNamed:#"baby-gorilla.jpg"];
[self.curImage assignImage:pic];
self.rootView = self.curImage.view;
}
and
#import <UIKit/UIKit.h>
#interface ImageViewController : UIViewController
{
IBOutlet UIImageView *image;
}
-(void)assignImage:(UIImage *)screenShotToSet;
#property(nonatomic,retain) IBOutlet UIImageView *image;
#end
Welcome to the iPhone SDK!
In general, there are two ways to get any view displayed.
First, and most commonly, you use a NIB file created by the Interface Builder. This is usually the easiest way to get started and I would recommend it for what you're trying to do here. It's too lengthy to describe all the steps you need to do for what you have here, but basically start in xcode by creating a new file and selecting "user interfaces" and choose View XIB. This will create a basic NIB file (they're called NIBs rather than XIBs for historical reasons). The first step in interface builder is to change the class name of the "File's Owner" to your UIViewController subclass (Test5ViewController). You can then drop anything that IB will allow into the view window or even replace the pre-supplied view object with one of your own. And here's the trick: make sure the view outlet (supplied by the UIViewController superclass) is connected to a view. Once this is done, this view will be automatically loaded when your NIB is loaded. You can then just put your UIViewController subclass (Test5ViewController) in your MainWindow.xib NIB file to get it automatically loaded, and you're in business.
Now, the way you're doing it here is the second way. Some people like to code this way all the time and not user interface builder. And while it's definitely necessary sometimes and always more flexible, it makes you understand what is happening a bit better. There may be other things, but the main thing you're missing is that in your code above, you have nothing that is adding your view into the view hierarchy. You need to check first that you have an UIApplicationDelegate subclass and it needs to load your "root" UIViewController class. All initial project creation types in xcode do this (except Window-based application). It is code like:
[window addSubview:rootController.view];
Once this is done, if your view controller wasn't loaded by the NIB (described briefly above), your loadView method will be called, expecting you to build your own view hierarchy. Above, you created the view(s), but failed to put them in a hierarchy. You need something like:
[self.view addSubview:curImage.view];
No view will be rendered until added to the view hierarchy. Make sure to look up the UIView class in the documentation and understand the variety of ways to add and remove views to the view hierarchy.
A couple things I should warn you about:
* your code above is leaking. You need to review how objective-C properties work. There's lots on this site about it. More than I have time to write about here.
* don't create a rootView property in the case you have here. There already is one in the superclass (UIViewController). It's just 'view'. Use that for saving your root view.
I hope this helps you get started. It can be bewildering at first, but you'll soon get it going! I recommend building and rewriting and rebuilding a lot of sample code before you do your "real" application. The SDK has many great samples.