views:

565

answers:

2

Hi,

I am working on a iphone app but found that I have now require another view / window to get the user to input and save data / information there.

how do I add another view? do I add it in interface builder and then link it in the main app delegate or will it have its own .h and .m files.

I selected a window view app to start with, do I need to start over with a flip side view app or can this just be added in anyway if I have the correct code there.

manny thanks

Carl

+2  A: 

The Window app is perfect for you. In your AppDelegate file, you should have a section like this:

    - (void)applicationDidFinishLaunching:(UIApplication *)application {    

    //instantiate the venue view controller object
    YourViewController *yourViewController = [[YourViewController alloc] initWithNibName:@"YourView" bundle:[NSBundle mainBundle]];

    // Configure and show the window
    [window addSubview:[yourViewController view]];
    [window makeKeyAndVisible];

}

This is the part of the code that declares, allocates and adds your custom view to the window. You have a couple choices for how to add the second view. You can either add it in place of this one, or add it after this one using a Navigation Controller. To add the navigation controller, change the above method to look like this:

    - (void)applicationDidFinishLaunching:(UIApplication *)application {    

//instantiate the venue view controller object
YourViewController *yourViewController = [[YourViewController alloc] initWithNibName:@"YourView" bundle:[NSBundle mainBundle]];
    UINavigationController *yourViewControllerWrapper = [[UINavigationController alloc] initWithRootViewController: yourViewController];



// Configure and show the window
[window addSubview:[yourViewControllerWrapper view]];
[window makeKeyAndVisible];

}

There, we create your custom view, then wrap it in a navigation controller. The navigation controller is what gets added to the window. Next the code to switch to the second view would look like this, assuming you switch views on a button press:

    -(IBAction)switchViewController{
MySecondViewController *secondViewController = [[MySecondViewController alloc] init];

[self.navigationController pushViewController:secondViewController]; }

Of course, you should replace the line

MySecondViewController *secondViewController = [[MySecondViewController alloc] init];

with the proper way of instantiating your second view controller. This could be from a nib file like above, or programatically.

As far as creating the view files, you should create a nib in Interface builder for the layout of everything, then create a .h and .m file for the ViewController code itself.

Dan Lorenc
Thank you for your help, I will try this out
carl
A: 

you can also display new frame instead of new view. It is easier sometimes, as you don;t have to pass parameters - you are in one class:

CGRect frame = okresView.frame;
frame.origin.x = frame.size.width;
if ( [okresView superview] == nil )
{
[self.view addSubview:okresView];
}

okresView.frame = frame;
[okresDataTableView reloadData]; // przeładowanie tabeli na subwidoku

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.5];
frame.origin.x = 0;
okresView.frame = frame;
[UIView commitAnimations];

if you want new subview, you can use a few methods - just download few applications from XCode help and check how they do this. Nice example are in 'Elements' and 'UICatalog' application where you have flipped view and other examples.

// Create and push another view controller. UIViewController *myViewController = [[UIViewController alloc] init]; myViewController.title = @"My First View"; myViewController.view.backgroundColor = [UIColor redColor];

//to push the UIView. [self.navigationController pushViewController:myViewController animated:YES];

plusz