How to use a UIButton to Change Views - objective-c

How can I changed the view in iPhone development using an UIButton. I do not want to use a toolbar. Thank you

You must have a root view controller
This root view controller switches between the two views.
Something like this:
- (IBAction)switchViews:(id)sender {
if (self.vc1 == nil) {
self.vc1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[self.vc2 removeFromSuperview];
[self.view addSubView:vc1];
vc2 = nil;
}
else {
self.vc2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.vc1 removeFromSuperView];
[self.view addSubView:vc2];
vc1 = nil;
}
}

Related

Creating multiple subviews inside view controller

I have a view controller named ViewController.
ViewController displays a UIView that has a button on it, which allows me to advance to a second view controller - SecondViewController.
SecondViewController also has a button on it, which allows me to advance to a third view controller.
However, I am having trouble displaying ThirdViewController. When I tap the button in SecondViewController it throws an error:
Warning: Attempt to present ... on ... whose view is not in the window hierarchy!
I have been to a bunch of other sites and posts that address this issue, but cannot seem to find a working solution. I have implemented quite a few solutions, but none seem to work.
Here is my code:
- (void)secondaryView:(UIView *)secondaryView
{
UIView *theView = secondaryView;
UIViewController *viewController = [[UIViewController alloc] init];
viewController.view.frame = [UIScreen mainScreen].bounds;
[viewController.view addSubview:theView];
viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:viewController animated:YES completion:nil];
}
secondaryView is a UIView that I am constructing elsewhere in the application. I add it to the viewController then present the viewController.
Is there any way to dynamically create UIViewControllers, add a UIView to each one, and add them to the window hierarchy?
If you can use navigation in your ViewControllers, you can try the below code
In the place where you call the firstViewController,
-(void)callFirst
{
FirstViewController *first = [FirstViewController alloc] init];
UINavigationController *navigationController = [UINavigationController alloc] initWithRootViewController:first];
navigationController.modalPresentaionStyle = UIModalPresenationFormSheet;
[self presentModalViewController:navigationController animated:YES];
}
And in the FirstViewController button action ,you can write
-(void)callSec
{
SecondViewController *sec = [SecondViewController alloc] init];
[self.NavigationController pushViewController:sec animated:YES];
}
And in SecondViewController you can do the same
-(void)callThird
{
ThirdViewController *third = [ThirdViewController alloc] init];
[self.NavigationController pushViewController:third animated:YES];
}
Try this...And in these ViewControllers you can do whatever coding you want to meet the requirement like
-(void)viewDidLoad
{
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(20,20,400,400)];
newView.backGroundColor = [UIColor redColor];
[self.view addSubView:newView];
}

Modal View Controller Size

I am trying to display a modal view controller, but keep the navigation bar displaying on the iPad. I've tried the different presentation styles but none work. Is it possible to size it, or display it within right on top of a certain view, instead above the whole view controller?
EDIT: Code added:
- (void)buttonPressed:(id)sender
{
RKWebViewController *webViewController = [[RKWebViewController alloc] initWithNibName:nil bundle:nil];
webViewController.request = [NSURLRequest requestWithURL:_link];
webViewController.webView.delegate = self;
webViewController.shouldDisplayDoneButton = YES;
webViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:webViewController animated:YES];
}
Use -
- (void)buttonPressed:(id)sender
{
RKWebViewController *webViewController = [[RKWebViewController alloc] initWithNibName:nil bundle:nil];
webViewController.request = [NSURLRequest requestWithURL:_link];
webViewController.webView.delegate = self;
webViewController.shouldDisplayDoneButton = YES;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:webViewController];
[webViewController release];
navController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:navController animated:YES];
[navController release];
}

How do I switch a new view controller in iOS development?

I am trying to write an IBAction to switch view controllers for my iPhone application:
-(IBAction)changeToView2:(id)sender
{
if (self.view2 == nil)
{
view2 = [[UIViewController alloc] initWithNibName:#"View2Controller" bundle:[NSBundle mainBundle]];
}
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentedViewController: view2 animated:YES];
}
However, I am getting a build error noting that no interface declares "presentedViewController:animated:". Why?
Changing this to "presentViewController:animated:" produces the same error.
The method is presentViewController:animated:completion:, not presentedViewController:animated:
Instead of
[self presentedViewController: view2 animated:YES];
try
[self presentViewController: view2 animated:YES];
[self presentViewController:view2 animated:YES];
Try this your edited code.I think this will be helpful for you.
-(IBAction)changeToView2:(id)sender
{
if (self.view2 == nil)
{
view2 = [[UIViewController alloc] initWithNibName:#"View2Controller" bundle:nil];
}
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentedViewController: view2 animated:YES];
}

NavigationBar not displaying properling when pushViewController

The problem is pretty simple to understand with pictures.
I have a UINavigationController that allow the user to switch between to views.
The first view contains a search bar and a table view like so :
The second is a basic view where information about the cell are display
When I click on the search bar, the navigation controller gets hidden and the search bar is now at the top.
Now, if I click on a cell, it goes to the second views, but the navigation bar is first hidden like below :
And then, it automatically appears like that :
I have tried a couple of things like show the navigation bar before pushing the next view controller but it is quite ugly..
Does anyone know how to have the show the navigation bar directly on the second view (like in the contact application)?
[UPDATE] : Code
AppDelegate.m (I'm talking about navigationcontroller2)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
FirstViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
viewController1.managedObjectContext = [self managedObjectContext];
viewController2.managedObjectContext = [self managedObjectContext];
viewController1.viewController2 = viewController2;
UINavigationController *navigationcontroller1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
[navigationcontroller1.navigationBar setTintColor:[UIColor lightGrayColor]];
UINavigationController *navigationcontroller2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
[navigationcontroller2.navigationBar setTintColor:[UIColor lightGrayColor]];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationcontroller1, navigationcontroller2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
FirstView.m
- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!noResultsToDisplay) {
PinDetailsViewController *pinDetailsViewController = [[PinDetailsViewController alloc] initWithNibName:#"PinDetailsViewController" bundle:nil];
NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
Pin *pin = (Pin *) managedObject;
[self.navigationItem setTitle:#"Pins"];
[self.navigationController pushViewController:pinDetailsViewController animated:YES];
[pinDetailsViewController updateWithPin:pin];
}
}
If you need anything else, just ask but I think it's all there.
Try to use this code in each viewcontroller.
- (void) viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void) viewWillDisappear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
Before you push the new view controller, you should unhide the navigation bar:
[self.navigationController setNavigationBarHidden:NO animated:YES];
I had a similar problem with the position of my navbar. Mine was moving up behind the status bar, and I fixed the issue by manually setting the navbar frame:
-(void)adjustNavBarOrigin
{
CGRect r = self.navigationController.navigationBar.frame;
r.origin = CGPointMake(0, 20); // 20 is the height of the status bar
self.navigationController.navigationBar.frame = r;
}
I had to call this method in a number of places, including viewWillAppear: and didRotateFromInterfaceOrientation:, but it worked a treat :)
Hiding the UINavigationBar can disturb the properties sometimes. Try using the property alpha instead of hidden.

Check if controller has already been loaded XCode

I have the following code to push a new ViewController in a Split View Controller:
Level4ViewController *controller = [[Level4ViewController alloc] initWithNibName:#"ModuleViewController" bundle:nil];
[[detailViewController navigationController] pushViewController:controller animated:YES];
[controller release], controller = nil;
The only problem I have, if I were to run this again, a new controller will show, I would like to be able to go to the view that I had before with all my data.
Can anyone help me out here.
Thanks.
EDIT:
Updated code?
Level4ViewController *controller;
for(UIView *view in self.navigationController.viewControllers)
{
if([view isKindOfClass:[Level4ViewController class]])
{
controller = view;
if(controller == nil)
{
controller = [[Level4ViewController alloc] initWithNibName:#"ModuleViewController" bundle:nil];
}
else {
controller = [self.navigationController.viewControllers objectAtIndex:1];
}
}
}
[[detailViewController navigationController] pushViewController:controller animated:YES];
[controller release], controller = nil;
UINavigationController has a property viewControllers which is a NSArray that hold all the stack that has been pushed to the navigation controller, in this array you can check for your view controller if it is there use that one - you check like this -
Level4ViewController *lvc;
for(UIView *view in self.navigationController.viewControllers)
{
if([view isKindOfClass:[Level4ViewController class]])
{
lvc = view;
}
}
and if you already knows that at which index your viewcontroller is there then you can get it from that index as -
Level4ViewController *lvc = [self.navigationController.viewControllers objectAtIndex:1];
update -
Level4ViewController *controller;
for(UIView *view in self.navigationController.viewControllers)
{
if([view isKindOfClass:[Level4ViewController class]])
{
controller = view;
}
}
if(controller == nil)
{
controller = [[Level4ViewController alloc] initWithNibName:#"ModuleViewController" bundle:nil];
}
[[detailViewController navigationController] pushViewController:controller animated:YES];
[controller release], controller = nil;
If you are using a navigation controller
FirstScreenViewController *firstScreenVC = [self.storyboard instantiateViewControllerWithIdentifier:#"1S"];
if (![self.navigationController.topViewController isKindOfClass:FirstScreenViewController.class])
[self.navigationController pushViewController:firstScreenVC animated:YES];