Loading UITableView to a View in another ViewController - objective-c

In my storyboard file I have 2 viewcontrollers. A UITableViewController and a ViewController.
I've completed all the needed coding for both of them. But I need to load the UITableView (and its data) inside a view in the ViewController.
Any suggestions?

Yes you can do this by adding UItableViewController as its ChildViewController like this
UItableViewController*vc1 = [[test1 alloc]initWithNibName:#"SecondViewController" bundle:nil];
//add to the container vc which is self
[self addChildViewController:vc1];
//the entry view (will be removed from it superview later by the api)
[self.view addSubview:vc1.view];
Swift:
let tableViewVC = UITableViewController(nibName: "SecondViewController", bundle: nil)
addChildViewController(tableViewVC)
view.addSubview(tableViewVC.view)
Hope it will help you.

Related

Using a segue from a UIViewController with UItextfield to populate a UITableViewController

Yes I am new to Xcode and coding in general. I did a bunch of research and figured out how to go from UItableviewcontroller to the viewcontroller then using the "unwind segue" to populate the table, which is not exactly what I want.
I would like the initial scene to be the viewcontroller rather than the table and as such the unwind segue isnt working. I've been trying to rearrange things for days but to no avail.
Any help would be greatly appreciated.
ViewController.m
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (sender != self.saveButton) return;
if (self.labelField.text.length > 0) {
self.toDoItem = [[YYTData alloc] init];
self.toDoItem.itemName = self.labelField.text;
TableViewController.h
#interface YYYTableViewController : UITableViewController
- (IBAction)unwindToList:(UIStoryboardSegue *)segue;
Tableviewcontroller.m
- (IBAction)unwindToList:(UIStoryboardSegue *)segue
{
YYYFirstViewController *source = [segue sourceViewController];
YYTData *item = source.toDoItem;
if (item != nil) {
[self.toDoItems addObject:item];
[self.tableView reloadData];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc] init];
[self loadInitialData];
Segues are used to navigate through viewControllers that are connected in a UINavigationController stack. To have viewControllers added to this stack, you have to first manually create segues in storyboard. You can access the viewControllers in your navigation stack with methods such as performSegueWithIdentifier:. In order to have your viewController be the first viewController in your navigation stack, do the following in storyboard:
1.) Delete all viewControllers that you have.
2.) Drag and drop a UINavigationController.
3.) Drag and drop a UIViewController (will be your first viewController and I will refer to it as viewController).
4.) Set the class of viewController to the class of your view controller.
--This is done by clicking on the viewController (in the left panel are of storyboard) and navigating to the identity inspector (in the right panel area of storyboard) and entering the name of the viewCotroller's class.
5.) Now you set the rootViewController: of navigationController to viewController by clicking on the navigationController (in the left panel area of storyboard), then looking in the connections inspector (last icon in the right panel of storyboard). Here you will see rootViewController, you'll right click from rootViewController and drag a line from it to your viewController (in the left panel of storyboard).
After completing these five steps, you can add a tableViewController in storyboard create a segue for it, and everything should work as you'd like it to.

Go back from Xib to different ViewController in StoryBoard

I've StoryBoard and xib in my project.
I know how to go to xib from storyboard, this is the code:
ChosenPlacesViewController *cpvc = [[ChosenPlacesViewController alloc]initWithNibName:#"ChosenPlacesViewController" bundle:nil];
[cpvc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:cpvc animated:NO];
But how do I go from the xib to different ViewController in my StoryBoard?
I know I can use:
[self dismissViewControllerAnimated:NO completion:NO];
But I want to go to different ViewController(TabBar StoryBoard), not the last one.
you should find out more about navigation view controller. It depends what is your hierarchy of view controllers. There is a viewControllers property. You can set new order of controllers, last one will be visible.
More info: http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html

how to access the UITabBarController when its not the initial view?

I'm having problem with accessing my tabbarcontroller in the storyboard when its not the initial view. So basically there is an initial view in the storyboard which leads to the tabbarcontroller. I want to change the color of the tab but i dont have access to it! I know that it can be added to the delegate if its the initial view but in this case its not the initial view in the storyboard! I read somewhere that I have to override a method in the first view but there was no detail about it!
If you are in a viewcontroller in the storyboard then this code:
UIStoryboard *storyboard = self.storyboard;
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"OtherViewController"];
Will get the other viewcontroller youw want and you could do things such as:
set yourself as delegate for it:
[(OtherViewController *)vc setDelegate:self];
display it:
[self presentViewController:vc animated:YES completion:nil];
I think you could use this to get the other viewcontroller and then access the tabbar from it.
Sorry if I misunderstood what you are trying to do.

EXC_BAD_ACCESS at removeFromSuperview - using ARC

In one of my ViewControllers (ViewController A), I have the following code:
AlertViewController *aViewController = [[AlertViewController alloc] initWithNibName:#"AlertViewController" bundle:nil];
[self.view addSubview:[aViewController view]];
[self.view bringSubviewToFront:[aViewController view]];
And in AlertViewController, I have a button and when the user clicks on it, I have:
[self.view removeFromSuperview];
Whenever I click the button, the result is EXC_BAD_ACCESS.
I'm unable to figure out the problem. My project is using ARC and ViewController A is part of a navigation controller stack if that info helps.
The problem here is that the UIView doesn't own its UIViewController. In the first block of code you held the UIView around by adding it to a subview, but let the UIViewController go away. The UIView from a UIViewController is special, you can't let this happen.
Make sure the UIViewController that created the UIView lives as long as the view does.

Adding custom UIViewController with UITableView as a subview

I'm trying to create custom UIViewController with a UITableView, load the UIViewController using a xib file and add the view as a subview to another UIView.
The hierarchy is like this:
UIViewController
UIView << add custom UIViewController's view
UIView
UIView
Here's my xib view hierarchy and settings:
UIView
UITableView
Connection in IB:
File's Owner:CustomTableViewController
Outlets:
view connected to UIView
tableView connected to File's Owner
delegate connected to File's Owner
datasource connected to File's Owner
I have both UITableDataSource and UITableDelegate implemented.
When i tried to add the view as a subview, it crashed ...
- (void)viewDidLoad
{
[super viewDidLoad];
CustomTableViewController* controller = [[CustomTableViewController alloc] initWithNibName:#"CustomTableView" bundle:[NSBundle mainBundle]];
[self.viewContainer addSubview:controller.view];
}
What am i missing?
Sounds Like something is not retained that should be. Set up an exception breakpoint and turn on zombies to find it. See askers results above.