can't add UIView from a UIViewController - iphone

I have some issue showing a view from a view controller. Basically, I have a UIViewController class, I programmatically created the view by overloading loadView method. Here is the code that I have:
UIView* view = controller.view;
UIWindow* window = [UIApplication sharedApplication].keyWindow;
if(!window)
{
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
}
[window addSubview:view];
The view is not visible.
However, if I created a simple view based app by calling
[self presentModalViewController: controller animated: YES];
Everything works well.
I am new to the iOS View Programming. Anything I did was wrong?

Steve,
I'm not sure what you are trying to achieve here, but a typical iphone app will have a base UIViewController subclass, say a nav controller or tab controller and that controller's view is added as a subview of the UIWindow view in the appDelegate class.
You then instantiate further UIViewControllers or subclasses thereof and those views get added as subviews of the current view and so on.
Have you checked out the basic templates in XCode? These should show you how the appDelegate and the like works. You generally don't need to mess with UIWindow in other classes, in my experience.

Related

How can I check and determine which view controller in storyboard is currently showing?

If the application was previously in the background, when applicationDidBecomeActive is called,how can I optionally refresh the user interface ?
I expect the current view controller is always the initial view controller in storyboard.I can't get the pointer to storyboard in my main AppDelegate.m .
How can I check and determine which view controller is currently showing ?
If you just want a pointer to your storyboard then you can do this:
UIStoryboard *sb = [[[self window] rootViewController] storyboard];
where rootViewController is actually the initial view controller of your storyboard. If you expect this controller to be the current controller (as you say) then you're good to go, but if you want to actually update your UI, then I guess you could post a custom notification from the delegate (inside applicationDidBecomeActive), and register each controller that may be interested to catch it so it can update its UI (or just add self as observer for UIApplicationDidBecomeActiveNotification to catch the same one that your delegate catches...).
PS. If your rootViewController is a UINavigationController you can get the currently showing controller like this:
id currentController = [[[self window] rootViewController] visibleViewController];

Set the Delegate on SubView

I have a splitViewController that has a master and detail view controllers. The code below is from the master and it creates the new view in the detail:
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:#"%#",[self.defaultSettingsMenuItems objectAtIndex:indexPath.row]]];
[self.detailViewController.view addSubview:controller.view];
detailViewController is a global instance of DetailViewController. In the detailViewController, I have many textFields and need to utilize the UITExtFieldDelegate. However, I think that the detailViewController isn't self at that point, and that's why I'm getting EXC_BAD_ACCESS errors on using the TextFieldDelegate methods in detailViewController.
EDIT: I have now found that the subView delegate methods only work for the viewController I setup as the rootViewCOntroller relationship from within Storyboard. Ex. If I have 6 views in the default menu settings above, whichever one I have setup as the first and root view in storyboard will work correctly. Any and all other subviews shown (from making a new selection in the master view) will not work properly. I think this will help diagnose the problem.
I am not familiar with storyboards, but I don't see you setting the detailViewController's delegate anywhere. You probably need to have something like self.detailViewController.delegate = self; somewhere before you yield control over to the subview.
This all I needed, the second line:
UIViewController *viewController= [self.detailViewController.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:#"%#",[self.defaultSettingsMenuItems objectAtIndex:indexPath.row]]];
if (self.detailViewController.childViewControllers.count >= 1) {
NSLog(#"childViewControllers: %#",self.detailViewController.childViewControllers);
[[self.detailViewController.childViewControllers objectAtIndex:0] removeFromParentViewController];
}
[self.detailViewController addChildViewController:viewController];
[self.detailViewController.view addSubview:viewController.view];
EDIT: I've updated my answer with the if look to remove viewControllers from the stack. Slightly hacky, but functional.

Allowing autorotation inside a programmatically created view that is displayed modally

This has been asked a dozen times on this site, but I haven't found an answer that works for me. I have an iPad application with a UISplitViewController on the root level that is created programmatically. Inside the view that is being displayed in the right hand pane, triggered by user interaction, a UINavigationController is programmatically created and presented to the user. Here is that code:
listenerController = [[UINavigationController alloc] initWithRootViewController:listenerView];
[listenerController.navigationBar setTintColor:[UIColor colorWithRed:185.0f/255.0f green:80.0f/255.0f blue:0.0f/255.0f alpha:1.0f]];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[listenerController setModalPresentationStyle:UIModalPresentationFormSheet];
[listenerController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[listenerController setModalInPopover:YES];
}
[self presentModalViewController:listenerController animated:YES];
[listenerController release];
This does create the view controller properly, but when it is displayed the iPad is forced back into portrait view regardless of what orientation I have the iPad in. Then when I dismiss the modal window, it will rotate back.
I already have shouldAutorotateToInterfaceOrientation in the viewcontroller of the righthand pane set to YES, and I even tried adding this to the main app delegate class without any luck. It doesn't seem like I should have to subclass UINavigationController just to override the shouldAutorotateToInterfaceOrientation method.
Am I calling presentModalViewController from the wrong object? I've tried [self presentModalViewController ...] as well as [self.parentViewController presentModalViewController ...] with the same results.
I'm assuming that the self in your code example is the right-view (detail) view controller. You need to call presentModalViewController from the root UISplitViewController.

iPhone Sdk: How to add a second UINavigationController?

i created an app from the navigation-based template given by apple. Now i want to add a second navigation controller to my application including a new UITableView. Can anybody show my how to do this? Thanks!
I think this can be done. In your app delegate you normally do something like
[window addSubview:navController.view]. UIWindow is just a UIView. So if you create two UIView ivars in the UIViewController that will contain the two nav controllers you should be able to do a similar thing:
#interface MyViewController : UIViewController
{
UIView* upperView;
UIView* lowerView;
}
etc...
MyUpperRootViewController* myUpperRVC = [[MyUpperRootViewController alloc] init...
UINavigationController* myUpperNavController = [[UINavigationController alloc] initWithRootViewController:myUpperRVC];
[upperView addSubview:navController.view];
[myUpperRVC release];
and something similar on lowerView.
In the root view or subseqeunt views pushed onto the controllers access them in the usual way as if there was one nav controller. [self.navigationController push... should behave normally.
For animating in (and out) the view controllers, just apply the animation to the views - upperView or lowerView. You might want to start with their frames off the visible display and then change them to something visible inside an animation block.

Navigation & View Controller questions

I'm experimenting with ViewControllers & NavigationControllers in Interface Builder trying to get a better grasp of what's tied to what and why... I'm struggling with a scenario that has confused me. Hopefully someone can set me straight...
Say I start with your typical iPhone template View-Based Application and I display a view which is handled by view controller (viewController). Then after a certain event I'd like to replace that view with a "typical" Navigation-Based View (rootVC). I'd like to create as much as possible in IB. My questions have to do with how to show rootVC and remove all traces of the previous viewController as user will never need to return and where/how to wire in the navController in IB. Currently when it's time to show the rootVC I do the following in my viewController:
RootVC *rvc = [[RootVC alloc] initWithNibName:#"RootVC" bundle:nil];
[rvc.view setFrame:[[UIScreen mainScreen] applicationFrame]];
ViewTestAppDelegate *appDelegate = (ViewTestAppDelegate *)[[UIApplication sharedApplication] delegate];
self.rootVC = rvc;
[rvc release];
[appDelegate.viewController.view removeFromSuperview];
[appDelegate.window addSubview:rootVC.view];
[appDelegate.viewController release];
rootVC displays except viewController still has a retain count of 1?!?
Also, where should rootVC's navigationController be instantiated? Having started with the View-Based template the MainWindow.xib contains an object for the viewController (which has its own ViewController.xib) an appDelegate and a UIWindow. My RootVC.xib contains a UITableView. Do I need yet another intermediary view controller that will have another ApplicationDelegate object that I wire up to a UIWindow object and a UINavigationController? The View Controller that comes along with IB's Navigation Controller object would then be set to my RootVC class?
Sorry for the verbosity. It's difficult for me to explain. Because some objects in IB are proxies and some are "real" it's sometimes confusing (to me) when trying "new" things out what's required, where & when. Basically I want to know to go about setting up one view leading to another with no way back to first view. 2nd view basically becomes the "main" root spawning off in many directions...
I would recommend using the navigation-based iPhone application template and presenting your one-time view as a modal view on top of the root view.
I was able to figure it out by putting a reference to the viewController in the MainWindow nib and then autoreleasing the viewController after I added the navigationController & rootVC to the UIWindow. Learned another thing or two about IB along the way. Pretty powerful...