Adding my first view to a window based application - iphone

I'm beginner with iPhone and want to know how to add a view in a window based application.
I crate the application, then I added the ViewControler+XIB to my project.
I include the .h file in both AppDelegate-Files.
Then I create a object of the View "StartViewController" with
StartViewController *startView;
and add the property in the AppDelegate.h file:
#property (nonatomic, retain) StartViewController *startView;
In the AppDelegate.m I add:
#synthessize startViewController
and in the application method:
[window addSubview:startViewController.view];
But it doesn't appear when I start the application, what I forgot?

You're not allocating or initializing it anywhere that I can see. From what you're saying, it seems like you need to put in a
startView = [[StartViewController alloc] initWithNibName:#"xib filename without dot xib" bundle:nil];
If this doesn't sound right, it may instead be that you aren't setting Interface Builder up right. How is the main window linking to this XIB / view controller of yours? Do you have it referenced as a view?

I'm beginner with iPhone and want to know how to add a view in a window based application
STEP 1:
Open Xcode Create New Project Select Window Based Application.Name is Some x
that is display two files xappdeligate.h and xappdelegate.m.
Step 2:
in project click right button add ui view controller name is FirstViewController that is displays FirstViewController .h , FirstViewController .m
Step 3:
In xappdeligate.h we have to write
FirstViewController *viewController;
define Property:
#property(nonatomic,retain)FirstViewController *viewController;
Step: 4
In xappdelegate.m we have to synthesize to them
#synthesize viewController;
allocate thememory:
viewController = [[FirstViewController alloc]init];
add view to the window:
[self.window addSubview:FirstViewController.view];
Step: 5
We should compulsary release the memory for this in
-(void)dealloc
{
[window release];
[viewController release];
}
this is simple way to add view to the window.
Ur's Raffi 37...

Just connect the view with interface builder by dragging.
and still if you can't connect , here is link where a number of applications are provided to learn iPhone applications.

Related

Why a separate instance of ViewController Class is affecting previous instance?

UPDATE - Cause found!... please read below & suggest the solution:
While creating a video to show this issue, I have found why does that happen...
Any Control/Element that is defined between #imports & #implementation DetailViewController in .m file is lost by the original detVC when a new instance is created of VC.
Example:
I am including a video that re-creates this issue. As shown, all controls work except a UISegmentedControl & a UILabel.. both of which are defined in DetailViewController.m as:
#import "DetailViewController.h"
UISegmentedControl *sortcontrol;
UILabel *incrementLabel;
#implementation DetailViewController
Video Link - http://www.youtube.com/watch?v=2ABdK0LkGiA
I think we are pretty close.. Waiting for the answer!!
P.S.: I think this info is enough can lead us to the solution, but if needed I can share the code too.
EARLIER:
There's a detailViewController (detVC) in our app which is normally 'pushed' from a UITableViewController.
Now we are also implementing Push Notifications which would 'modally' present the same detVC whenever the notification arrives, which in turn can happen over any view controller.
It works all fine until the detVC through notification is presented while another instance of detVC was already the active view controller (pushed earlier through TableView).
The problem happens when the presented detVC is dismissed - the pushed detVC 'looses' control of about everything - It is not pointing to the same data as earlier & even the UISegmentedControl on Navigation Toolbar points to -1 index on pressing any index!
Why does such thing happen when we are creating a separate instance of detVC? Why does it affect the earlier detVC? How can it be resolved / What about Objective C needs to be learned here? (FYI, we are using ARC in the project)
Thanks
EDIT - Some code:
When Pushed:
ProductDetailViewController *detailViewController = [[ProductDetailViewController alloc] init];
detailViewController.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:detailViewController animated:YES];
detailViewController.serverOffset=serverOffset;
detailViewController.prdctIndex = indexPath.row;
When presented through Notification:
- (void)displaySingleProduct:(NSDictionary*)userInfo{
ProductDetailViewController *onePrdVC = [[ProductDetailViewController alloc] init];
UINavigationController *addNav = [[UINavigationController alloc] initWithRootViewController:onePrdVC];
onePrdVC.justOne=YES;
onePrdVC.onePrdIDtoLoad=[[[userInfo valueForKey:#"prd"] valueForKey:#"id"] intValue];
[self.navigationController presentModalViewController:addNav animated:YES];
}
There is a Product class in detVC which gets the data from the values stored at the prdctIndex row in the SQLite table.
After dismissing the presented detVC through notification, the Product class of the pushed detVC starts pointing to the row which was used for Product class in presented detVC. And there is no such code in viewDidAppear which would alter Product class.
So, this, UISegmentedControl issue mentioned above & some other problems are causing the pain! Basically, the whole detVC acts weird - which re-works if we just pop & re-push it!
EDIT 2
I have more info on it which could lead to the cause.
If I run viewDidLoad on viewDidAppear like this:
if (!justOne) {
aProduct = [allProducts getProduct:(prdctIndex+1) one:NO];
[self viewDidLoad];
[self populateDetails];
}
the pushed detVC regains the control & every element/control starts re-working as expected (but not in the ideal way). So, it is quite clear that the separate instance of presented detVC does messes up the earlier pushed detVC & a re-setting up of all the controls / pointers is required through viewDidLoad.
Can any helpful conclusion can be derived from this info?
When in your code you write:
#import "DetailViewController.h"
UISegmentedControl *sortcontrol;
UILabel *incrementLabel;
#implementation DetailViewController
You are not defining these variables as instance variables (ivars), so they are not individual for each instance. There are three ways to define ivars.
1) The traditional way, in your .h file.
#interface DetailViewController{
UISegmentedControl *sortcontrol;
UILabel *incrementLabel;
}
2) Some additions to objective-C have added support for the next two ways. Like declaring them in your .m file.
#implementation DetailViewController{
UISegmentedControl *sortcontrol;
UILabel *incrementLabel;
}
3) If these ivars use properties to expose them, then you can simply leave out the explicit definition of them. So in .h:
#interface DetailViewController
#property (strong, nonatomic) IBOutlet UISegmentedControl *sortcontrol;
#property (strong, nonatomic) IBOutlet UILabel *incrementLabel;
and then in the .m file:
#implementation DetailViewController
#synthesize sortcontrol;
#synthesize incrementLabel;
Are you loading the view controller from a NIB? If so, it may be that the same instance of the view controller is used each time. You can log the address of your view controller in viewDidLoad and see if this is the case.
It actually depends on how your apps architecture has been created. If you are using TabBarController based application, here is the proper way to do so.
If two instances of the same class are interfering with each other, that strongly suggests that you are not storing all your state in instance variables. I would suspect that something in ProductDetailViewController stores its state in global or class data, and that your problem lives in there.
It's possible that you have done something silly like making ProductDetailViewController a forced singleton (overriding +alloc, which you should almost never do), but generally people remember when they've done that.
Never call viewDidLoad directly. That's only for the system to call. (I know you were just testing, but don't leave that in.)
But are you calling [super viewDidAppear:animated] in your viewDidAppear:? You have to call your superclass in that method.

steps to add tabbarcontroller to AppDelegate using Interface Builder in XCode 4.2 Empty Application template

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

Switching from one view to second view?

I want to know that can in switch between two views in an iPhone application if I have chosen the application as window based application in the Xcode or it is only possible to switch between views in view based application only.
How to design interface for changing the views in such appliocations as I am not able to design the second view in the interface builder after designing the first view.
Your view controller can present any other view controller like this
[firstViewController presentModalViewController:secondViewController animated:YES];
This will take you to second view controller.
To come back to first view controller, in second view controller you say
[secondViewController.parentViewController dismissModalViewControllerAnimated:YES];
Please refer to the documentation here
The main problem i think you have here is the perception of what each project type does.
A Window based application provides just a window and no "default" view controller for you to use.
A View based application provides a window AND a view controller and xib file for you to create your UI.
If you want to see how to add a view to a window based application create an empty view based application and look at the code that is auto added to the didFinishLaunchingWithOptions method in the appdelegate. This is essentially what you need to do with your window based application.
Add a view controller with a xib file for user interface, then look at how the view based application loads this view and displays it (using initWithNibName and then adding the view to the window)
i'd say you need to do more reading: take a look at cocoa fundamentals for iOS - in the documentation and then the view controller programming guide) these are both essential areas of reading. Then have a root around in the standard project types and take a look at how they are set up, this is really useful because you'll see what apple intend you to do when setting up your app
When you have your class which controls the UIWindow, you add objects in that window. One or some of these objects are Navigation Controllers or Views. in you windowController.h, you should define a view:
#property (nonatomic, retain) IBOutlet UIView *mainView;
and in the .m-file, synthesize it:
#synthesize mainView;
Then use it:
MainView *mainView = [[MainView alloc] initWithNibName:nil bundle:nil];
mainView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:mainView animated:YES];
[mainView release];
That is one of the two things that could possibly be your question.
Another question you could ask is: how to switch from my window-based application to a view-based application?
You can just create new classes with corresponding .xib-files, being UIView's. Adapt your appDelegate classes and you should be fine.
Switching from one view to other in view based application
FirstViewController *firstViewController = [[UIViewController alloc] initWithNibName:#"FirstViewController" bundle:[NSBundle mainBundle]];
[self.view addSubview:firstViewController.view];
or you can also use this.
FirstViewController *firstViewController = [[UIViewController alloc] initWithNibName:#"FirstViewController" bundle:[NSBundle mainBundle]];
[self presentModalViewController:firstViewController animated:YES]; // this is deprecated in ios6.0

What am i missing when creating applications from window-only template?

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

How should I load a xib into a detail view for a split view iPad objective app?

I've got a split view iPad app with table view in the master pane. After drilling down several levels, each time loading a new .xib, I'd like one of the cells to trigger a web page load in the detail pane. Right now I can only get the web page .xib to load in the master pane side -- which is a master pain in my side.
The basic load call where "URLWindow" is a class loaded with initWithNibName:
[[self navigationController] pushViewController:URLWindow animated:YES];
I want to do this, but it doesn't seem to work:
#interface
#property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;
...
#implementation
[[self detailViewController] pushViewController:URLWindow animated:YES];
How should I be loading the URLWindow .xib into a detail view for a split view detail pane?
couple of things to check:
1) you may try setting BOTH master / detail controllers as 'UINavigationControllers' ?
2) [detailViewController.navigationController pushViewController:URLWindow animated:YES];
you are calling pushViewController on 'detailViewController', this is calling it on 'navigationController'