Programmatically Re-Load UIViewController for "Start Over" Button - iphone

I have a UITableViewController. When you select a cell, it calls init on a UIViewController, which programmatically creates a bunch of views and adds them to the screen. Everything here works fine.
As the user interacts with the app, the views are moved or deleted. I want to have a button where the user can "Start Over" and the UIViewController will init and draw itself like new on the screen. Basically I want the same behavior as if the user went "back" to the UITableViewController and clicked on that same item again.
I can create the button and wire it up and everything. What I need to know is how to release and re-initialize the UIViewController.
How do I do that?

Create your UIViews in UIMyViewController controller.
and use the below code for pushing your view controller in navigation stack.
-(void) buttonClcked:(id) sender
{
//Create for pushing another view controller in navigation stack.
UIMyViewController *myViewController = [[UIMyViewController alloc] init];
//to push view controller in navigation stack.
[self.navigationController pushViewController:myViewController animated:YES];
// you could release it because now it's retained by your UINavigationController
[myViewController release];
myViewController = nil;
}

Well, seems to me, you have two choices:
You can exit the UITableViewController (via a delegate call to the parent) and have it destroy it and relaunch it. (or)
You can put your view building code into a separate routine (not a NIB or loadView or ViewDidLoad or even ViewDidAppear, and then release all the subViews of self.view and call the view builder again.

Related

Loading another UIView directly from the main ViewController

I am in the process of creating an application. In the main ViewController I have created the menu. But I want to have a login screen (UIView) to appear before the menu is visible.
But because the menu loads as soon as I run the application I have decided to create another UIView controller and have that loaded on top of the main ViewController.
Therefore at the end of my main ViewController viewDidLoad I have added the following code to open on top of that view the login view
LoginPageView *loginPageView = [[LoginPageView alloc] initWithNibName:#"LoginPageView" bundle:nil];
loginPageView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:loginPageView animated:YES];
As I know the purpose of that code is to present another view, but unfortunately the login view does not appear. Only the main ViewController load.
Can anyone help me on that? Have you realised what exactly I want?
Thanks a lot
In the viewDidLoad method, the view exists, but there is no guarantee that the view is already part of the view hierarchy of your application. In fact, almost ain't.
What you can try is take that code in the viewWillAppear: or viewDidAppear:.
Make a UINavigationController. Use this as your window's root view controller. Set the UINavigationController to not show the navigation bar.
Set the navigation controller's child view controller to your login view controller.
When the user logs in successfully, create your main view controller and push it, like this:
MainViewController *mainVC = [[MainViewController alloc] initWithWhatever...];
[self.navigationController pushViewController:mainVC animated:YES];
When the user logs out, you can just do this to get back to the login VC:
[self.navigationController popViewControllerAnimated:YES];

Hiding a button in parent view from its modal view?

I have a parent view with a hidden button, and a method that unhides that button. That parent view has a modal view in which I need to call the method that hides the button.
ParentViewController.m
- (void)unhideButton {
myButton.hidden = NO;
NSLog(#"Unhide");
}
ModalViewController.m
- (void)levelComplete {
ParentViewController *controller = [[ParentViewController] alloc] init];
[controller unhideButton];
[controller release];
}
The NSLog message Unhide is successfully showing up in the console, but when I dismiss the modal view controller, the button is still hidden. What am I doing wrong?
Modal view controller's have an automatic reference to the view controllers that present using the parentViewController property. So you can directly say,
[self.parentViewController unhideButton];
in the levelComplete method.
But yeah creating a new instance and calling the method on it will not affect the original instance like Ryan said.
Why is the ModalViewController, which is presumably presented by an instance of ParentViewController, instantiating a new ParentViewController? I think what you need to do is pass a reference to the existing ParentViewController to the ModalViewController when you create it, then in ModalViewController it can set the hidden property on the parents button.
If you want to follow good design practices, the ParentViewController needs to delegate the management of its button to the ModalViewController. ParentViewController would conform to a simple protocol, exposing the button, and would set itself as the delegate of the ModalViewController before presenting it.

Switching between 2 Views in 2 distinct View Controllers

I have a problem with transitioning between views in different ViewControllers.
Here is the situation:
My App is a TabBarApplication done with IB which contains a UIViewController for each Tab. The UIViewController (PlayerTabViewController) of the first tab contains another UIViewController (PlayerCreationViewController) to manage a view that will be added as subview.
I was able to add the subview using
[self.view addSubview:playerCreationViewController.view];
In the PlayerTabViewController.
The problem is that from the subview I have to return to the parent view and reload it because it contains a tableview that must be refreshed.
Using [self.view removeFromSuperview]; in the PlayerCreationViewController I can switch back to the parent view, but I'm not able to reload the tableview or do other actions.
I tried to implement the -(void)willRemoveSubview:(UIView *)subview method in PlayerTabViewController but it seems the function is never called.
Do you have an Idea of what am I doing wrong?
you are using wrong method to go on next view. just use navigation view controller to switch from one view to another view.
create a object of view
PlayerCreationViewController *playerViewController = [[PlayerCreationViewController alloc] initWithNibName:#"PlayerCreationViewController" bundle:nil];
[self.navigationController pushViewController:playerViewController animated:YES];
[playerViewController release];

iPhone: Push Navigation Controller (w/ TableViewController) onto stack. Is this possible?

I have a view which contains a UIButton. When this is clicked, it calls a method that loads another NIB. Now, normally, that nib would load a view onto the stack, and everything would be fine. But, I am trying to load a Navigation Controller (so that I can have table views that are multiple levels deep), and all I get it errors.
What is the proper method for loading a Navigation Controller and putting it on the top of the stack?
As the other poster said you should create your Nav controller in your AppDelegate. If you are adding a new UIView to the stack like presentModalViewController you want to create the UIView then add the Nav Controller to it. If you don't want nav controller on that screen but the next just use the navController.hidden property I think it is.
To add the nav controller to the view do this:
NoticesView *noticesScreen = [[[NoticesView alloc] init] autorelease];
noticesScreen.delegate = self;
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:notices_screen] autorelease];
[self presentModalViewController:navController animated:YES];
Hope that helps towards your question. Still trying to find out exactly what your doing
Create the navigation controller in the app delegate. Push your mapview onto the stack as the first view. Push your tableview onto the stack as the second. If you started with a view-based app template, you won't have a navigationcontroller instantiated at all. (Been there done that) the easiest way out of this is to use xCode to make a navigation based applcation and then copy the code out of that. If you do already have a navigation controller, then just push the view controllers as above.

Set a navigation controller without an app delegate

I would like to show a Navigation Controller after clicking a button. Every tutorial assumes the navigation controller will be the first screen so it links it to the App Delegate, but App delegate only appears at MainWindow.xib.
How do you guys add a navigation controller to a view different than the MainWindow?
Thanks!
Here is some sample code to expand on Roger's answer. The following method is linked to some user interaction on the current view controller (to compose an email for example). This will give the compose view the navigation bar across the top instead of coding buttons inside your custom view.
-(void) composeButtonPushed: (id) sender {
ComposeViewController *controller = [[ComposeViewController alloc] initWithNibName:#"ComposeView" bundle:nil];
UINavigationController *composeNavController = [[UINavigationController alloc] initWithRootViewController:controller];
[self presentModalViewController:composeNavController animated:NO];
}
UINavigationController is to navigate a heirarchy of views with UIViewControllers. If you don't have a root UIViewController, it won't work (and doesn;t make sense). If you do have a UIViewController, you simply send a - (id)initWithRootViewController:(UIViewController *)rootViewController init message to a new navigation controller passing in your UIViewController.