where to control view controllers? - iphone

i'm working in view-based template,
and I have main.m, which I haven't touched.
and one view controller which is my current display,
and one dummy controller which has nothing.
If I implement the dummy controller, and want to switch between
two view controllers, where and how should I do it?
I've only worked with subviews,
but not quite sure where to touch the view controllers...
does it work the same as addView, release as with subviews?
Please help me out..

It depends on how you want you're looking to do. You could present your dummy view controller either modally or non-modally. To do it modally, check out:
- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
Here is a sample on how you would do something like that:
DummyViewController *dummyController = [[DummyViewController alloc] init];
[self presentModalViewController: dummyController];
You could also do something like:
- (void)presentFlipSideViewController:(UIViewController *)flipsideViewController
You would call these methods when the user did something, such as tap a button. Both of these methods would be implemented in the view controller handling the tap or action, in your case, the project template view controller .

Related

Iterating 'Active' View Controllers

Ola Folks,
In an iPhone application I am displaying different views by using the addSubView:SomeViewController.view method.
I want to be able to, at the very least, log the view controllers that are in the view hierarchy that is being displayed. I would prefer to be able to get a handle to a specific view controller.
I know how to iterate the views, I just do not see how to access the view controllers of those views. I am looking for something that will give me the type of access to the view controllers that UINavigationController::ViewControllers does.
I thought I could get away with:
for (UIViewController* oVC in [self.view subviews])
but this is not having the intended effect.
If someone has a way of doing this, please share it with me.
-isdi-
A view doesn't keep a reference to its view controller (or know anything about view controllers in general), so you'll have to keep track of that mapping yourself. If you keep all of your view controllers in an array viewControllers, you could do something like:
- (UIViewController *) viewControllerForView:(UIView *)view {
for (UIViewController *viewController in viewControllers)
if (viewController.view == view)
return viewController;
return nil;
}
The standard way for a view to interact with the view controller that owns it is by having the view controller set as the delegate or action target of the view. The view is designed not to have any details about the delegate or action target.
If you have implemented your own view, just add a member to hold a reference to the view controller. Or adopt a delegate model for the view so that it does not matter what class the delegate is.
If you are treating the views as a stack and want to maintain a stack of view controllers along side it, similar to what UINavigationController does for you, you must do so manually. Couple every call of addSubview:viewController.view with a call to [myViewControllerArray addObject:viewController] and remove the viewController form the array when the view is removed from the view hierarchy.

iPhone: How to Trigger the Loading of a View Using a UI Element in a Previous View

I've been reading the Head First iPhone Development book and I understand how to get to a new view from a table but how exactly would I be able to get to a new view or view controller, by just simply pressing a button? Is that even possible?
I mean there are some apps where you click a button, not a table cell and it loads a new view. How exactly is that done? If someone could help out a newbie it would be greatly appreciated!
I think what you're looking for is a modal vew controller. THis presents a modal view like you described on top of everything else. If rootViewController is the view controller that is displaying your current view, and myNewViewController the view controller you want to display modally:
[rootViewController presentModalViewController:myNewViewController animated:YES];
There's plenty of examples of this kind of thing on the net, just search for presentModalViewController
Like bpapa said in the comments, it's hard to be specific without code. However, generally what you want to do is:
Build a navigation controller that contains one original view.
Create a button in your original view using the Interface Builder.
Build a callback method (usually defined with IBAction) that is run when the button is pushed.
In that callback method, create a new view and push it onto the navigation controller the same way you would using a table view cell.
Alternately, if you only want one level of hierarchy, you could use a modal view controller; instead of pushing onto the navigation controller in the last step, just present the modal view controller.
The general answer is that you have an object that manages which view controller loads when.
The most commonly used is the UINavigationController. It is a UIViewController that instead of controlling views, controls other view controllers. It works like a simple stack. You push views you want to display onto the nav's controller stack and when you want them to disappear you pop them off.
A common (though sloppy) way of using a nav is to make it a property of your app delegate. Then anywhere in your app you can references it by:
UINavigationController *nav=[[[UIApplication sharedApplication] delegate] navigationController];
The view controller for the first the user sees is held in the nav's topViewController property. If you want to load a view based on a user action in the topViewController.view, you would have something like this:
- (IBAction) loadNextView:(id) sender{ // Action called by a a UI event such as a button press.
UINavigationController *nav=[[[UIApplication sharedApplication] delegate] navigationController];
UIViewController *nextViewController=...// load from nib, connect with IBOutlet, create programmatically
[nav pushViewController:nextView animated:YES];
}
The first view disappears to be replaced by the next one. To return to the first view, you have a method in the next view controller like so:
- (IBAction) unloadSelf:(id) sender{ // Action called by a a UI event such as a button press.
UINavigationController *nav=[[[UIApplication sharedApplication] delegate] navigationController];
[nav popViewControllerAnimated:YES];
}
... and the nav returns you automatically to the previous view regardless of what that view was.
When you first start out, especially if you use Interface Builder, the structure of the app is largely hidden. Behind the scenes all view controllers and their views exist in a hierarchy of some kind that leads back up to the app delegate. You should train yourself to think in hierarchal terms even if it is not immediately obvious how that hierarchy is constructed.

What causes a UIViewController to become active?

I am sure this is an easy question, but one that has escaped me for some time now.
Say I have a UIViewController, either defined as a root in an XIB or on a stack. At some point in my code I want to replace it with another view controller. Just flat out replace it. How would I do that?
I have tried defining the controller and assigning, but not sure what actually makes it push on the screen with the absence of a navigation controller.
I think when you say that you want to replace the view controller, what you actually mean is that you want to replace the view. Bear in mind that view controllers aren't visible, but every view controller maps to a view, which can become visible by getting added as a subview of a visible view.
Your solution of replacing self.view with the new view controller's view may work in your particular case, but it's probably not the "correct" answer to your question. There are going to be cases where this solution won't work for you.
Let's say you have a simple view based application with no navigation controller and no tab bar controller. In your app delegate you construct an instance of YourFirstViewController, and you call [window addSubview:yourFirstController];. Your view hierarchy now consists of a UIWindow with a single subview -- the view for YourFirstViewController.
Now let's say the user presses a button on that view, which is handled by an IBAction defined in YourFirstViewController. You want to respond by "replacing" YourFirstViewController's view with a view associated with YourSecondViewController. I put "replacing" in quotes because we more commonly present a view by pushing its view controller onto a navigation stack, or calling presentModalViewController:animated: to present the view modally, but let's assume that you've rejected those options for some reason, and you actually do want to manually replace YourFirstViewController's view with YourSecondViewController's view.
This is a simple matter of manipulating the view hierarchy. You want to remove YourFirstViewController's view from its superview (the UIWindow in this case), and you want to add YourSecondViewController's view as a subview to replace it. Your action would therefore look something like this:
- (IBAction)replaceButtonClicked {
UIView *mySuperview = self.view.superview;
YourSecondViewController *secondController = [[YourSecondViewController alloc] init];
[mySuperview addSubview:secondController.view];
[self.view removeFromSuperview];
[secondController release];
}
When we use a methods like -pushViewController:animated: or -presentModalViewController, the receiving controller manipulates the view hierarchy for us. This may make it seem like we're looking at view controllers on the screen, but we're not. We're just looking at a big hierarchy of nested views going all the way up to a UIWindow at the top.
You can present a new view controller modally:
[self presentModalViewController:aViewController animated:YES];
This won't outright replace the current VC, but it will display a new view over the current view.

iPhone - one level up in a TabBar and call a method

I have a NavBar, a TabBar and a SearchBar with a ScopeBar. So the user can perform a search via a remote server. Clicking on one TableRow of the TableView a new ViewController with a xib is pushed into.
It is doing some calculations and it is possible, that I have to dismiss this View(Controller) and I should go back like I am clicking the "back" button in the NavBar.
How could I do this programmatically and call a method in this ViewController, because I have to trigger the search with the saved search term.
Does anyone know this?
Thanks a lot in advance & Best Regards.
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
if you are
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
Use delegation. Have the table view controller create your new view controller newViewController and set newViewController.delegate = self before pushing it onto the navigation stack.
Then in newViewController, just before you popViewControllerAnimated: call some method like [delegate doWhateverWhenNewViewControllerIsPopped: ...]. Ideally you declare a protocol called something like NewViewControllerDelegate and have the controller above implement it.
You can change the back button of NewViewController to something like "Done" or whatever by changing it's left navigation button. (See the properties of UIViewController.)
Hope that makes sense.
I solved the problem, take a look at the comments.

Add A view with Dissolve effect in iPhone

FlipView *fl=[[FlipView alloc] initWithNibName:#"FlipView" bundle:nil];
//fl.view.frame=CGRectMake(50.0,50.0, 300.0,300.0);
fl.delegate=self;
fl.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;
fl.imageName=aCategoryDtl.Tip_Image;
[self presentModalViewController:fl animated:YES];
In above code, A Complete New View Controller is added to screen.
Instead of that, I need to display only single view(UIView) - Not View controller.(UIViewController)
How to implement that?
Thanks in advance for helping me.
The comment is correct, you do not display a view controller, u display that viewcontrollers views, viewControllers are used to manage views, now what i imagine you want is a way to switch from view to view with transitions in a single view controller, there is a sample project called ViewTransition here https://developer.apple.com/iphone/library/samplecode/ViewTransitions/ it contains a view that you can use to switch from view to view with transitions, it might help you