Change views in a UIViewController - iphone

I have an UIViewController with a UIToolBar at the top with 3 buttons and a UIView, when touch upInside those buttons I have to change the views that the controller has. What can I do to get my porpuse? Thanks in advance.

You probably want to use something like a UINavigationController to control the view stack and then have your button(s) call one of these methods for the Touch Up Inside action:
pushViewController:animated:
popViewControllerAnimated:
popToRootViewControllerAnimated:
popToViewController:animated:
Here is a good uinavigationcontroller-tutorial to look into.

You need to do something like this for each of the actions you set up.
In the .h file of the current viewController:
#import "OtherViewController.h"
#interface MyViewController : UIViewController
{
OtherViewController *otherViewController;
}
#property(nonatomic, retain)IBOutlet OtherViewController *otherViewController;
Then in the .m file of the current viewController you need to add the following for each IBAction (touch up inside).
At the top of the .m file add:
#synthesize otherViewController;
Then make an IBAction and put the following line of code to display the other view:
[self presentModalViewController:otherViewController animated:NO];
In your otherViewController you can dismiss itself by using:
[self dismissModalViewControllerAnimated:NO];
NOTE: The other thing you will need to do is create a UIViewController in Interface Builder for each of the views you plan to display. You need to then go into the identity inspector and set the Class as OtherViewController. You then need to link the IBOutlet to the OtherViewController as normal.
There is a YouTube video tutorial which covers all of what I have mentioned above. It's a nice simple way to get started.

UiViewController view property is the base view you are seeing. It could be set(replaced with another). SO replace the view object of ViewController with another view you created.
UIView * customView = [[[UIView alloc] initWIthFrame:viewFrame] autorelease];
[self setView:customView];
Here self represent the current viewController.

Related

Pushing a UIViewController from a UIView

I need to push a UIView into my UINavigation controller. I am doing it by
[self.view addSubview:showContactFlow];
And on a button click in UIView I need to push another UIViewController over the UIView. From the UIView I am not able to access self.navigationcontroller How can I do this?
Edit:
I have set the UIView as the view of a new UIViewController I am pushing into, the before mentioned UIViewController . Now I would like to know, how to handle the UIView button event inside its UIViewController, in which's view it is set.
Add a UINavigationController ivar to the UIView and assign it to the main view controller's. Then you should be able to access it from the UIView.
Edit:
Your UIView subclass:
// CustomView.h
#interface CustomView: UIView {
// ...
// your variables
// ...
UINavigationController *navController;
}
#property (nonatomic, assign) UINavigationController *navController; // assign, because this class is not the owner of the controller
// custom methods
#end
// CustomView.m
#implementation Customview
// synthesize other properties
#synthesize navController;
// implementation of custom methods
// don't release the navigation controller in the dealloc method, your class doesn't own it
#end
Then before the [self.view addSubview:showContactFlow]; line just add [showContactFlow setNavController:[self navigationController]]; and then you should be able to access your hierarchy's navigation controller from your UIView and use it to push other UIViewControllers.
You should try to work with an MVC approach. So your controller has access to all that stuff and can keep pushing and popping views, so the view doesn't need to know too much about the controller.
Otherwise, and for this case you can solve it fast by using delegation. So:
showContactFlow.delegate = self;
[self.view addSubview:showContactFlow];
So later in the UIView, you can just say:
[self.delegate addSubview:self];
This is gonna work, but it's not likely to be the best approach you should use.
On button click, you can present a view controller like,
-(void)buttonFunction{
ThirdVC *third= [[ThirdVC alloc]initWithNibNme];......
[self presentViewController:third animated:NO];
}
Using Core animation you can make NavigationController's pushviewController like animation on writing code in ThirdVC's viewWillAppear: method.
where do you add the UIButton is it in showContactFlow view or in the ViewController's view??
In regard to the modalViewControllers issue the correct method is
[self presentModalViewController:viewController animated:YES];
the standard animation in upwards

How to get parent navigation controller in UIView

I have created a UITabBarController in my app delegate.
where each tab bar item holds a different UINavigationController that loads a custom UIViewController with a NIB (using -pushViewController:).
Inside one of the navigation controller, I load a custom UIView class with a custom NIB also.
This view is loaded multiple times inside the UIViewController.
The custom UIView has a UIButton, that on the event of touching it, I want to push a new UIViewController on the stack.
Problem is that I 'lost' the UINavigationController that holds the UIViewController.
I know I should use delegates, but haven't figured out who should which class should be the delegate.
Thanks in advance!
Neither .navigationController or .tabBarController will be available for a UIView or UIViewController that's been created but not pushed onto a stack
Either create a property on your View (or ViewController) class that is a UIViewController that is provided optionally after initialization or you could add a third argument to initWithNibName:bundle:
#interface CustomViewController : UIViewController
{
UIViewController *owner;
}
#property (nonatomic, assign) UIViewController* owner;
#end
Then in the owner ViewController:
CustomViewController *cvc = [[CustomViewController alloc] initWithNibNamed:nil bundle:nil];
cvc.owner = self;
It's too bad .parentViewController is read-only, this would be the sensible place for this.
You can get this using UIApplication class. Using this class you can find which viewController is placed at first. Here is the solution link for your problem.

iphone switchview with buttons,how to allow press button one time only?

im newbie developer and creating my first iphone app... and i have one little problem :)
i switching in my program 2 views, secondView is over firstView, and when i press 2 times or more on button to show the SecondView iphone simulator stopping worling and if after i press to show the FirstView he still showing SecondView view :(...
and i need help how to make button to pressing one time only, and if after switch back to FirstView to can again press one time,and shows like presse,now it show pressed only when i tuch it,... i want like buttons in TabBar, and if i use the TabBar is more harder for me i dont know how to resize it to height and add custom background, and change the effect of pushed button
Thanks you very much and sorry for my bad english!.
here is what code i use to switching views with buttons
// FirstView.h
#import <UIKit/UIKit.h>
#interface FirstView : UIViewController {
}
-(IBAction) goToSecondView:(id) sender;
-(IBAction) goToFirstView:(id) sender;
#end
// FirstView.m
#import "FirstView.h"
#import "SecondView.h"
#implementation FirstView
SecondView *secondView;
-(IBAction) goToSecondView:(id) sender{
secondView = [[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
[self.view addSubview:secondView.view];
}
-(IBAction) goToFirstView:(id) sender {
[secondView.view removeFromSuperview];
}
thank you very much!
This:
#implementation FirstView
SecondView *secondView;
... is most likely the source of your crash. You shouldn't define instance variables in the implementation. The compiler may allow it but the runtime will be confused and the instance variable will not be properly retained.
You should define it like:
#interface FirstView : UIViewController {
SecondView *secondView;
}
#property(nonatomic, retain) SecondView *secondView;
...and use it like:
-(IBAction) goToSecondView:(id) sender{
UIView *newView = [[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
self.secondView=newView;
[newView release];
[self.view addSubview:self.secondView.view];
}
For clarity you should also rename FirstView and SecondView to FirstViewController and SecondViewController because they are view controllers and not views themselves.
More generally, what you are trying to do is dangerous and difficult. You don't swap views by adding and removing them as subviews. You need to swap out view controller and their views using a UINavigationController or a UITabbarController. In Xcode File>New Project, there is a Navigation based project and a Tabbar based project templates. Either will provide you most of the code you need to implement a simple app using either controller.
It will be well worth your time to spend a day learning how to use these controllers properly. With your current design, your app will break if it gets much more than two views.

Pop-up modal with UITableView on iPhone

I need to pop up a quick dialog for the user to select one option in a UITableView from a list of roughly 2-5 items. Dialog will be modal and only take up about 1/2 of screen. I go back and forth between how to handle this. Should I subclass UIView and make it a UITableViewDelegate & DataSource?
I'd also prefer to lay out this view in IB. So to display I'd do something like this from my view controller (assume I have a property in my view controller for DialogView *myDialog;)
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:#"DialogView" owner:myDialog options:nil];
myDialog = [nibViews objectAtIndex:0];
[self.view addSubview:myDialog];
problem is i'm trying to pass owner:myDialog which is nil as it hasn't been instantiated...i could pass owner:self but that would make my view controller the File's Owner and that's not how that dialog view is wired in IB.
So that leads me to think this dialog wants to be another full blown UIViewController... But, from all I've read you should only have ONE UIViewController per screen so this confuses me because I could benefit from viewDidLoad, etc. that come along with view controllers...
Can someone please straighten this out for me?
There is no such thing as a view controller being on the screen; its view is on the screen. With that said, you can present as many views as you want on the screen at once.
I would create a new view and view controller. You would not make a UIView be a UITableViewDelegate, you make a UIViewController be a UITableViewDelegate. But instead of doing that manually, instead make your new view controller a subclass of UITableViewController, if you're using iPhone OS 3.x+. You can then present this view controller modally.
You probably want to give the user a chance to cancel out of the selection. A good way to do that is to wrap your new dialog view controller in a UINavigationController and then put a "Cancel" button in the nav bar. Then use the delegate pattern to inform the parent view controller that the user has made their choice so you can pop the stack.
Here's what the code will look like inside your parent view controller, when you want to present this option dialog:
- (void)showOptionView
{
OptionViewController* optionViewController = [[OptionViewController alloc] initWithNibName:#"OptionView" bundle:nil];
optionViewController.delegate = self;
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:optionViewController];
[self.navigationController presentModalViewController:navController animated:YES];
[navController release];
[optionViewController release];
}
Your OptionViewController .h will look like this:
#protocol OptionViewControllerDelegate;
#interface OptionViewController : UITableViewController
{
id<OptionViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id<OptionViewControllerDelegate> delegate;
#end
#protocol OptionViewControllerDelegate <NSObject>
- (void)OptionViewController:(OptionViewController*)OptionViewController didFinishWithSelection:(NSString*)selection;
// or maybe
- (void)OptionViewController:(OptionViewController*)OptionViewController didFinishWithSelection:(NSUInteger)selection;
// etc.
#end
Your OptionViewController.m will have something like this:
- (void)madeSelection:(NSUInteger)selection
{
[delegate OptionViewController:self didFinishWithSelection:selection];
}
Which has a matching method back in your original view controller like:
- (void)OptionViewController:(OptionViewController*)OptionViewController didFinishWithSelection:(NSUInteger)selection
{
// Do something with selection here
[self.navigationController dismissModalViewControllerAnimated:YES];
}
There are plenty of examples throughout Apple's sample source code that follow this general pattern.

How to switch to next view when button is pressed iPhone?

here is a iPhone programming beginner's question:
How do I get to another view by pressing a button in my main view?
I have the following function which is executed when I press a button, and debugging it, he passes there, but my "Warning" view does not show up:
-(IBAction) showWarningView:(id)sender
{
if(self.showWarning == nil){
WarningViewController *nextView = [[WarningViewController alloc] initWithNibName:#"Warning" bundle:[NSBundle mainBundle]];
self.showWarning = nextView;
[nextView release];
}
[self.navigationController pushViewController:self.showWarning animated:YES];
}
My main RootViewController looks like this:
#import <UIKit/UIKit.h>
#import "WarningViewController.h"
#interface RootViewController : UIViewController {
IBOutlet UIButton *button1;
WarningViewController *showWarning;
}
#property(nonatomic,retain) WarningViewController *showWarning;
-(IBAction) showWarningView:(id)sender;
#end
I am using the navigation control of a UITableViewController but what do I have to use to just simply show my other view when I press a button in a view-based application?
Thanks a lot!
edited
if you're using the navigation control of a UITableViewController, you probably have to push the view in you tableviewcontrollers navigation controller
this means you have pass the navigation controller of your tableviewcontroller on to your viewcontroller, then you just push it
e.g.
[self.tableViewControllersNavigationController pushViewController:self.showWarning animated:YES];
(for passing the tableViewController's navigationController on, you might have to create a delegate pattern)
Does the title in your navigation bar change? Maybe you don't have a UIView associated with your UIViewController inside your Interface Builder file.