uitableview inside multiple view controllers - ios

I have to 'repeat' a uitableview into 2 or more view controllers. Like using a uitableviewcontroller with its methods inside view controllers using the methods/delegates/datasources from the uitablewviewcontroller.
Example: I've a uitableviewcontroller wich displays a twitter feed with its own style, so I need this table in more than 1 view controller and maybe just changing the twitteruser in each view controller (just an example).
TRY: What I've done is to create (with storyboard) a uiviewcontroller and a uitablewviewcontroller, both with their own methods. And on uiviewcontroller viewdidload, try to add the uitablewviewcontroller.tableview as a subclass. and this works! But the result is an empty table. I tried to set the delegates/datasources but it ddnt work..
-(void)viewDidLoad
{
[super viewDidLoad];
//Add Table as subview
thetable *t = [[thetable alloc] init];
[self.view addSubview:t.tableView];
t.tableView.frame = CGRectMake(0, 100, 320, 2000);
t.tableView.backgroundColor = [UIColor blueColor];
}
This code works, but just displays an empty uitable inside the view controller..
*thetable is a uitableviewcontroller object (.h, .m, and storyboard view)
*Just set the backgroundcolor to check if there's anything on screen
*using ios6
Thanks!

Make sure you are implementing the table view delegate and datasource methods in thetable.Also try [t.tableView reloadData] after adding the tableView to the view controller's view.

Related

How can I add a common view to multiple ViewControllers in iOS Swift?

I'm kinda new to iOS development.
I want to have a custom View and some Labels inside it. And In some viewControllers of my app on a button click I want to add/show that View
at the bottom of that viewController.
As far as I'm manually adding the view in storyboard in all the viewControllers in which I want the view to display. But this is not efficient. How can I add this view in viewControllers programmatically on button click?
Make one BaseViewController class inherited with UIViewController
Now create method named as designFooter in BaseViewController
func designFooter() {
var viewFooter: UIView = UIView(frame: CGRectMake(0, self.view.bounds.size.height - 50, self.view.bounds.size.width, 50))
viewFooter.backgroundColor = UIColor.redColor()
self.view!.addSubview(viewFooter)
}
For Swift 4, 5.1:
func designFooter() {
let viewFooter: UIView = UIView(frame: CGRect(x:0, y:self.view.bounds.size.height - 50, width:self.view.bounds.size.width, height:50))
viewFooter.backgroundColor = UIColor.red
self.view.addSubview(viewFooter)
}
Now inherit this BaseViewController to you ViewController where you want to add footer, and on button click just call self.designFooter()
If this subview you want to add has some dynamic content or has much of its own logic, you might want to employ view controller containment, specifically not only add a subview, but add a controller associated with that subview, too. So, you can have a scene in your storyboard for this subview that will appear on the bottom, and associate it with its own view controller. Then, when you want to add it, you'd do something like:
let child = storyboard!.instantiateViewController(withIdentifier: "storyboardid")
addChild(child)
// set the `frame` or `constraints` such that it is in the correct place, perhaps animating it into place
view.addSubview(child.view)
child.didMove(toParent: self)
And when you want to remove it:
child.willMove(toParent: nil)
child.view.removeFromSuperview()
removeChild(child)
Personally, if this can really appear and disappear from any scene in my app I actually embed the whole app in a container view controller. Then this popping of the child in and out only has to be done once, on this master container view controller.
For example, consider this storyboard:
That is an embedded "container view" (the storyboard equivalent of view controller containment, discussed above). And I can then have a label animate in an out (by animating layoutIfNeeded after changing the height constraint of some view with a label). Then, this bottom view can animate in and out regardless of which view controller's view is currently visible:
Just create a UIView and call addSubview:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];//change this frame for your purposes
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(10,10,50,10)];
[l setText: #"My label"];
[view addSubview: l];
[self.view addSubview: view];
As far as adding this to multiple view controllers... I suppose if you had a lot of different view controllers with this same UIView you could create a subclass of UIViewController called CustomViewController. In that class add the above code to viewDidLoad. Then, subclass CustomViewController in all the view controllers with this particular view, and they will automatically add it for you.
Edit:
If you want to design the view in interface builder, make a custom subclass of UIView. Let's call this CustomView. Design it in a nib and add any code you want. Then, whenever you want to create that view, simply call CustomView *cv = [[CustomView alloc] initWithFrame:...] and then do [self.view addSubview:cv];

How to SubView a ViewController into a SubView of another ViewController

I have two ViewController.
One ViewControllers contain a UITableView. And another contains a UIButton.
I have created a SubView Programmatically.Now i want to SubView the ViewController which contains the UITableView in other when i Press UIButton.
I searched all over the net but cannot find any stable solution.
Currently i am trying this:
bodyView =[[UIView alloc]initWithFrame:CGRectMake(0,120,containerView.frame.size.width,120)];
bodyView.backgroundColor = [UIColor redColor];
CustomTableVC *tableVC = [[CustomTableVC alloc]init];
[tableVC willMoveToParentViewController:self];
[bodyView addSubview:tableVC.view];
[self addChildViewController:tableVC];
[tableVC didMoveToParentViewController:self];
[containerView addSubview:bodyView];
You cannot.
You can only use the view property of your UIViewController to assign into UIView associated in your second UIViewController which is not recommended because UIViewController as per MVC pattern holds lot controller stuff which includes populating the view and resolving the inputs/touch, which is an overhead in your (using multiple of viewcontrollers without needed) case.
You need to use one UIViewController. Add UITableView only in it, and UIButton only in it. You only use one controller and multiple views.
The other approach, if you do not want to change your code, may also use ContainerView. But in that case you need to create separate ViewControllers for UIButton and UITableView. And if you want to fetch data inbetween the ViewControllers, that will be a huge pain and also a bad software design with so much coupling and less encapsulation.
i have tried this one and it's working for me.
#IBAction func moveToOther() {
var otherController = OtherViewController()
var bodyView = UIView(frame: CGRectMake(0,120, self.view.frame.size.width, 120))
bodyView.backgroundColor = UIColor.redColor();
bodyView.layer.borderWidth = 1.0
let tblCntrl = UITableViewController()
bodyView.addSubview(tblCntrl.tableView)
bodyView.clipsToBounds = true
otherController.addChildViewController(tblCntrl)
tblCntrl.didMoveToParentViewController(otherController)
otherController.view.addSubview(bodyView)
self.navigationController?.pushViewController(otherController, animated: true)
}
You should use only one view controller that contains both the table view & UIButton.
By default hide the table view.
Just hide the button and show the table view when the button is clicked.

Add view behind tableview in UITableViewController

I'm trying to add this custom control below my tableview in a TableViewController:
https://github.com/zogieosagie/RMEIdeasPullToSortControl
In the example the creator gives, the control is implemented using a ViewController and an added tableview, but I want to use it in a TableViewController. I have created and initialized it as shown in the example but I cannot get it to show up behind the table. Any ideas?
Here is a screenshot of the control above my tableview: https://www.dropbox.com/s/ojfpacxelcy9cqm/Photo%20May%2028%2C%208%2057%2035%20PM.png
Here is my code in the viewDidLoad method:
[self.tableView setBackgroundColor:[UIColor clearColor]];
self.rmeideasPullDownControl = [[RMEIdeasPullDownControl alloc] initWithDataSource:self delegate:self clientScrollView:self.tableView];
self.sortTitlesArray = [[NSArray alloc] initWithObjects:#"Listed from A - Z", #"Listed from Z - A", #"Brand value: HIGHEST - LOWEST", #"Brand value: LOWEST - HIGHEST", #"Founded: OLDEST - NEWEST", #"Founded: NEWEST - OLDEST", nil];
CGRect originalFrame = self.rmeideasPullDownControl.frame;
self.rmeideasPullDownControl.frame = CGRectMake(0.0, 45.0, originalFrame.size.width, originalFrame.size.height);
//It is recommended that the control is placed behind the client scrollView. Remember to make its background transparent.
//[self.view insertSubview:self.rmeideasPullDownControl belowSubview:self.tableView];
[self.tableView addSubview:self.rmeideasPullDownControl];
[self.tableView sendSubviewToBack:self.rmeideasPullDownControl];
Table view controllers do not lend themselves to managing anything other than a table view. In a table view controller the content view of the view controller is the table view.
You should not try to add other views as subviews of a table view.
Those 2 things combined mean that you can't do what you are trying to do.
Instead, you should create a regular UIViewController. In your storyboard, add a container view to the view controller's content view. Create a UITableViewController as a separate scene, and then control-drag from the container view onto the table view controller. That will set up an embed segue, so your table view controller becomes a child view of the regular view controller. Now you can do whatever you want to the main view controller's content view, including adding other views behind the table view.
Do you mean that you are using a Table View Controller on the storyboard? Or do you mean that your backing code is a subclass of UITableViewController?
I haven't used this project before but I'm guessing you are using a Table View Controller on the storyboard, in which case there is no backing view for the RMEIdeasPulldownControl to attach to (the top-level view is a UITableViewController). If you look in the example it needs to be attached to a scrollview (like a table view) but it needs to be inserted into a view (like a UIView)
If you meant the second one then I'm not sure, UITableViewControllers are subclassed from UIViewControllers and are really very similar, so I can't imagine any trouble arising from that.
It isn't possible directly, but you can create UIViewControllerClass with relevant storyboard UIViewController
add a MyUIView in hierarchy then UITableView next to MyUIView
attach datasource and delegates for UITableView and use MyUIView as per your requirement.

tableView initwithframe and storyboard

I'm trying to add a tableview as subview to my tableViewController, but I want to setup the cells in storyboard. It will be a static tableview.
This is the code for calling the tableview on button click.
- (IBAction)botaoAdicionar:(id)sender {
AtividadesPraticadasTableView *tableview = [[AtividadesPraticadasTableView alloc] initWithFrame:CGRectMake(0, 170, 320, 320) style:UITableViewStylePlain];
[self.view addSubview:tableview];
}
In the tableview class I have this:
#implementation AtividadesPraticadasTableView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
Now, I have a viewcontrollerin storyboard with a tableview, which the class of the tableviewI changed to this file AtividadesPraticadasTableView. It has three static custom cells in storyboard, therefore it opens a blank default tableview.
What am I missing?
Static table views are entirely contained within the storyboard, and require information from the storyboard to display their content.
You've defined a static table view controller in the storyboard, populated it and set the tableView's custom class to your custom class, but when you want to add the table view you are just instantiating a table view of that class. That isn't going to get any of the information you've added to the storyboard.
In addition, the static cells information is read and understood by the UITableViewController, not the UITableView, so you are at the wrong level there too.
You need to do the following:
Get a reference to the storyboard, either from your original view controller's storyboard property (if it is on the same storyboard as your static table) or using storyboardWithName:bundle:.
instantiate the table view controller from that storyboard, using instantiateViewControllerWithIdentifier:. This will create a table view controller object containing all your static cells
Add this as a child view controller to your original view controller, using addChildViewController:
Add the table view controller's tableView as a subview
It may be simpler to add a container view in the storyboard to hold this view, and reveal it when the button is pressed, as Mike Slutsky suggested - this will do all of the instantiating and adding and child view controller-ing work for you, but the principle is still the same.
Also, adding a table view as a subview to a table view controller sounds very dodgy - a table view controller already has a table view as its view, and you can't really add subviews to that.
The thing your missing is the association between the programatically instantiated tableview and the UITableView that you put in your storyboard. You cannot just draw UITableViews in your storyboard and start instantiating new UITableViews in the controller's code and expect xcode to know which UITableView you wanted to use from the storyboard. Use an IBOutlet to connect a global variable for the controller to the UITableView in the storyboard, then the controller will know what you're trying to refer to. If you want that UITableView to appear on a button click, simply set the UITableView to hidden by default in the storyboard and then unhide it when the button is pressed.
The thing You are missing called manual. Check this protocol for TableView dataSource https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITableViewDataSource
P.S. Here good tutorial for storyboards http://maniacdev.com/ios-5-sdk-tutorial-and-guide/xcode-4-storyboard

Manage uiview and uiviewcontroller

I created an uiview , that contained , many textfields , and I have an uiviewcontroller with xib file responsible for making signature .Is it possible to add this viewcontroller to my uiview in order to have this componant of drawing signature in the footer of my uiview ??
- (void)init { //parent view
...
FooterViewController *fvc = [[FooterViewController alloc] initWithNibName:#"myNibName" bundle:nil];
[self addSubview: fvc.view];
fvc.view.origin = CGPointMake(0, self.frame.size.height - fvc.view.frame.size.height);
}
this is how you can create your viewController from xib file and add its view to the main view. assuming this is what you are trying to do.
If you are using storyboards, the easiest way to go is to drag a Container View object from the Object library (where buttons, labels, etc. are) to your view controller's view. This will create a child view controller that you can handle separately and that will be already resized to mirror the size of the container view controller.

Resources