usings static buttons to access different view controllers views - objective-c

As the titles states I am looking to have a main view controller with a few buttons. Depending on which button was pressed I would like to load a view of a separate view controller in a contained field below the buttons where I can still access the buttons and the content in the view. How should I attempt to accomplish this?

All you have to do is add the viewcontroller's view as you would any other view. So it would look something like;
ViewController *vc = [[ViewController alloc] init...];
[vc.view setFrame:whatever frame you want];
[self.view addSubview:vc];
And just have that called in your button's action method.

This sounds like a job for a UITabBarController

Related

How do I determine the identity of a UITableView within a Storyboard

I have created a TableView within the Storyboard. I don't want to control-drag links between this and files as programmatically, this is easier to maintain.
However, I don't know how I can determine what the identity of the TableView is within the Scene.
Is there a way of naming this, or somehow determining this sop that I can dynamically change the behaviour of it?
In fact, this sort of applies to everything in the Storyboard. I don't know how to name anything.
Use tag, under the View category:
Every view (control) has it. You can use viewWithTag method of UIView to get the control you need, like this:
UIButton *button = (UIButton *)[self.view viewWithTag:1]
If you look in your storyboard, when selecting a view controller there is a screen like this in the "Identity inspector" tab:
After assigning a storyboard ID to your view controller and activate the "Use Storyboard ID" option, you can instantiate it programatically with:
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"MyViewControllerID"];
[self presentViewController:vc animated:YES completion:nil];

How to set index of UIViewController as background

I'm trying to set background to the entire app from the code:
UIViewController *controller = [[UIViewController alloc] init];
controller.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background#2x.png"]];
self.window.rootViewController = controller;
[self.window makeKeyAndVisible];
In the Storyboard I have a simple button in the main UIViewController. In the AppDelegate.m I set that code above, but, I background has added, they is in front of the text that is in the Storyboard.
I want to know how can I set that background from the current index to the last index in the stage.
You can create a subview, set the background image in that subview, frame it to the right size and then send that subview to a position "behind" everything else:
[mainView addSubview:backG1];
[mainView sendSubviewToBack:backG1];
Well, wenn you use a storyboard at all, AND declare it in your project settings as THE storyboard of your app, then you are not supposed to create and set the root view controller in your app delegate.
Both of them is perfectly fine, but one is the interface builder/storyboard option and the other is the programmatical way. Both ways just don't agree!!!
If you want to go for storyboards, which I encourage you to do, then give your root view controller an ID and in aoru ypp delegate method didFinishLoadingWithOptions: fetch it from the storyboard and set its background view then.
Have in mind: When you use a background view then you need to set all the background colours of all your view items (that may be tables, table cells, cell content views etc. pp.) to clearColor.
When you set an initial view controller in code (in AppDelegate.m, under didFinishLaunchingWithOptions), then it overrides the root view controller in your storyboard.
You can either set your view controller's background in the storyboard, or initialise it like you did - but then you need to create all other UI elements in code as well.

Stopping navigation bar from scrolling with table

I have a UITableViewController which successfully lists in the table view data fetched from a CoreData entity.
I want to have a navigation bar at the top with a couple of buttons to navigate around, but when I add the navigation bar it appears to sit inside the table, bellow any listings, and therefore scrolls wit the table when flicking through the table list.
As it is a UITableViewController I cannot make the table area smaller it seems and so I have no idea how to add the navigation bar outside of the table.
Any advise on how to make the fix the navigation bar in place so that it does not move.
Here is the code use to set the navigation bar:
- (void)viewDidLoad
{
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0, 1024,44)];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:#"Observation details"];
[navBar pushNavigationItem:navItem animated:NO];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(backButton)];
navItem.leftBarButtonItem = backButton;
UIBarButtonItem *detailsButton = [[UIBarButtonItem alloc] initWithTitle:#"Observation Details" style:UIBarButtonItemStyleBordered target:self action:#selector(detailsButton)];
navItem.rightBarButtonItem = detailsButton;
[self.view addSubview:navBar];
}
Don't drag the navigation bar and drop on the table view. That way you are setting that view (a navigation bar) as the header of the table view.
To do what you are trying to do, the easiest way is to use a navigation controller with the table view as its rootTableView.
myNavController = [[UINavigationController alloc] initWithRootViewController:myVC];
[self presentViewController:myNavController ...
You can also use a navigation bar without a navigation controller. For that, you have to set a View with a navigation controller at the top and a table view filling the rest of the view.
There are several ways to achieve this. I try to describe the one of which I believe it is easiest based on what I guess is your current level of experience.
Create a new empty view with a separate view controller.
Add two subviews to that view of the type container view. These views are designed to carry views that have their own view controllers. One of these containers is on top and has the size of the navigation bar and contains the navigation bar etc.
For that one you could create a separate view controller. However, I see no reason why that could not be the same view controller. (File's owner in IB)
It may well be a separate instance/class. That is up to you.
The remaining view is occupied by the second container view. This one contains a UITableView for which a UITableViewController (-subclass) is the File's Owner.
Unless you are more familiar with the view hierarchy and view controller hierarchy you should not break with the pattern (as given by default sceleton code and tutorials etc.) of having a 1:1 relation between table view controlers and table views. A UITableViewController has a view property as every UIViewController has. But for a UITableViewController that must be a UITableView and nothing else. The standard UITableViewController is not capable of dealing with a UIView as view property that contains a table view and other views as siblings. Plus it is not a good idea of adding more subviews to the table view with respect to its scrolling behaviour etc.
By separating them you can deal with the table and still take examples straigt of tutorial code plus you can have a navigation bar on top that is not influenced by the table at all.
However, I should mention that you could get all of this much easier by using a proper navigation controller. That would bring in your navigation bar for free. You must have good rasons for not doing so.

Mimic modal window to allow for tapping outside of modal window

I am using a Split View Controller and showing a modal window when a button is tapped in the master pane. I need to be able to dismiss the window when the user taps outside of the bounds of the window.
I am currently using presentViewController, which I have read does not allow for taps outside of the window.
I think I need to present a view controller myself and setup a gesture recognizer to handle the closing from there... the trouble is, I don't know where/how to present the view controller or where to attach the gesture recognizer to in an SVC.
I setup my view controller like this:
SearchViewController *searchViewController = [[SearchViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:searchViewController];
[navController setNavigationBarHidden:NO];
From there I don't know how to make the view controller appear without using presentViewController. I have tried the following (each separately):
[[self navigationController] addChildViewController:navController];
[self.view.window.rootViewController addChildViewController:navController];
[self.presentingViewController addChildViewController:navController];
[self.presentingViewController.presentingViewController addChildViewController:navController];
How do I present my navcontroller, and which view would I add a gesture recognizer to?
A UIPopoverViewController will work.
Alternatively, you can add a child view controller to your RootViewController. . (Check out UIViewController containment for lifecycle handling).
Basically just this in your root view controller:
- (void) presentSemiModalViewController
{
//Tint-out the background or blur it with some effect
_semiModalViewController = viewController;
//Choose the frame you'd like to use here, and an animation you'd like to use to present it
[self.view addSubView:_semiModalViewController.view];
[_semiModelViewController willMoveToParentViewController self];
}
If the RootViewController is not yours (eg a UINavigationController) you can use a category, but to retain the _semiModalViewController you'll need to use an associative reference (ie add a "property" to the category to store the modal VC while its in use). For info on that see here: Associative References Info
PS: You might want to choose a better name my "semi-modal", but you get the idea ;)

How do I make a reusable XIB with it's own UIViewController?

I am trying to create a reusable "picker". It is basically like a keypad of a phone. Since I will be using this a lot in my iPhone app, I have been frustrated with trying to make it appear.
It is in its own XIB file and has its own UIViewController subclass as the FileOwner.
However, when I instantiate this with:
MonthPickerViewController *mpvc
= [[MonthPickerViewController alloc] initWithNibName:#"MonthPicker"
bundle:nil];
Nothing happens on the screen. Yet is does fire the -viewWillAppear methods, etc.
So, what am I doing wrong either in code or in InterfaceBuilder that is preventing my view to appear?
Are you pushing the view controller?
[[self navigationController] pushViewController:mpvc animated:YES];
Or are you adding the view controller's view as a subView of your current view?
First, make sure you've hooked everything up right inside Interface Builder. An easy gotcha is to forget to connect the View object up to the view outlet of your UIViewController subclass.
Then, as Adam says, you need to actually display the view. Assuming you're doing this inside the code of another view controller, you'd need something like the following if you just wanted the new view to appear ontop of your current view:
[self.view addSubview:mpvc.view];
Of if you are using a navigation controller to stack views:-
[[self navigationController] pushViewController:mpvc animated:YES];