How to tell what UIViewController a custom UITableViewCell Xib is on? - objective-c

I want to find out what ViewController a custom Table View Cell is located on. I'm using the UI Hierarchy inspector and I don't see the name of the View Controller, it just says UITableView as the super view of the cell.
Is there a definitive way to inspect a cell and find out what View it appears on top of? If I can't do this in the UIHierachy Inspector, how can I do this?

You can achieve this using Associative References. They are not pretty, and not easy to use but effective on being able to associate an object with another.
Here is a link to a tutorial on how they work.
Associative References
After digesting this you can plan to associate the view controller / view to the tableview cell on iteration (cell for row at index path maybe)
and delegate method did select row at index path you would be able to obtain the associative reference and know what view controller needs to be manipulated.

Related

Pass data from Collection View to Table view

I am a new learner, I am doing an assignment, stuck on this point. My target is when user click on a cell inside collection view, I have to go on table view and pass data to it (Which means that table view will have only one cell with data for selected collection view item).
I know I will go to table view programatically not possible through storyboard(If possible let me know) by using delegate method didSelectItemAtIndexPath
Can you explain the way to open table view from collection view cell click and how I will wrap data to pass to other controller (I think Segue? But how if we cannot use storyBoard). I am not getting clear there is no much help on internet in objective c? If you provide some pathway it will be good and if you explain it would be excellent??? Any helping material will be a plus
I am using XCode11 with objective c and not using XIB files Just working by storyboards
I also think segues is the way to solve your problem.
Set up the segue in storyboard and then inside the didSelectItemAtIndexPath set some variable with the data you want to pass and use performSegueWithIdentifier:sender to kick off the segue. Then override prepareForSegue:sender to transfer this data to the destinationViewController. You could use the sender to transfer the data from perform... to prepare... as well but I suggest for now just use some instance variable.

Cocoa binding search field to tableview in a different nib

I've got a user interface that looks pretty much like iTunes. For purposes of encapsulation, the top area and the main table view are in separate classes with separate nibs. I want to bind the search field from the top view controller to the tableview in the bottom view controller. I've arranged it so there are properties to store the NSArrayController in both classes. The array controller is an array of dictionaries, and the dictionaries have a "search_keywords" key that I want to use to filter the tableview.
Is it possible to set up the search stuff in Interface Builder even though it's in a separate nib? I can't figure out what to put in the various boxes.
If it's not possible with IB, I assume it's possible in code, since there is a view controller with references to both of the sub-view controllers and I can get at the search field, table view and array controller objects through properties on the two classes.
How do I set it up? IB would be best, if it's possible.
What I did was use the NSSearchField in the top view controller as a dummy/placeholder. I create the "real" search field in the tableview's nib, and wire up all the bindings stuff as per normal. Then in the main view controller I grab the search field out of the tableview's nib, and replace the dummy searchfield with the real one using replaceSubview:with:
Now I can continue to use IB to modify the bindings, and it doesn't really matter what's in what nib as it all gets placed properly in the view hierarchy at runtime.
In your concept you cannot bind the search field in IB in the desired way.
Do it like
1. Create an accessor-method (accessorMethodForTextInSearchField or what name you want to use) in the TopClass for the Text in the searchField
2. Import the TopClass.h in the MainClass
3. In MainClass you can use
NSString *searchString = [ NSString stringWithString:[ TopClass accessorMethodForTextInSearchField] ];
4. Now search for searchString in the array

why IB sometimes doesn't add a subview to a view controller

Sometimes IB simply doesn't allow you to add a view as a subview to a UIViewController as illustrated here
If I drag a UIViewController from the object library and try to embed it within Mailbox View Controller.. it doesn't highlight, however it would work fine if I try to add it to the generic View Controller at the bottom (can the fact that Mailbox View Controller have a Customer Class MailboxViewController have anything to do with it?)
I'm pretty sure I can do this programmatically (which is what I'll try next) but I was wondering if there was a reason for this (and if there was a way around it).
update:
this is what i'm trying to accomplish: I was following the steps here to implement a segmented view controller below search bar like in the iphone mail app.. however I kept on getting an error saying that a view can only belong to one view controller at a time.. So what I'm trying to do is basically create a separate viewcontroller, reference it from MailboxViewController as an outlet, make the containing view of my search area the view of this new view controller (this is where i'm getting stuck) and finally make the searchContentsController property of UISearchDisplayController refer to the view of this new view controller. (if this sounds confusing, which I know it does, please refer to this answer)
From your screenshot, the view property of your mailbox view controller is a table view.
A table view in interface builder won't support the dropping of arbitrary views onto it as subviews - where would it put them at runtime? In IB the table has no content, it just has that visual representation of cells to let you know what it is.
You haven't said what you are trying to set up so I can't offer any additional help. Adding a subview programmatically to a table view probably won't give you the effect you're after either - a table view is a UIScrollView subclass, so your new view will either move off screen or get covered up by the table view adding cells.

Is there a better way to load View Controllers from a table view?

This might be a very basic question, but I could not find the answer yet.
I have a UITableView that acts as a menu for my app. Each row on the table view, when selected, opens a different subclass of UIViewController.
At the moment my code works the same way used in the UICatlog example from Apple.
In the main view controller (the table view), each menu item is described in a dictionary in an array (menuList). Each dictionary contains an instance of the UIViewController subclass for that screen and other data about the menu item. When the user selects a row, the didSelectRow atIndexPath kicks in and calls the appropriate view controller, stored in the dictionary at that indexPath.row of the menuList array.
It seems to be very wasteful to alloc and init every single view controller when the table view first loads.
My question is: Is there a better way than the one demonstrated in UICatalog to alloc/init my view controller sublasses only when the associated row is tapped?
(I know I can use a complex if..else structure in the didSelectRow, but this results in an extremely long didSelectRow method and breaks encapsulation. I wonder if there is a cleaner way to do this, allocing and initing the appropriate view controller based on data from the dictionaries)
user1349768 try to use Storyboard, but this feature only works in iOs 4 and higher.
Just a suggestion ... put some reference to each view controller into NSArray and then initiate and segue to them when the row gets tapped on (and just get the reference from objectAtIndex:).
Although I could not find a better way to do this, the memory signature of each allocated View Controller is only 288 bytes. Since the solution suggested by apple is a lot more elegant and scalable then using a switch case statement, I left it as it is.

Can we create an IBOutlet to a View Controller

I have a viewController. That viewController have a containerView that contain another controller. List and map.
I want users to be able to switch between list and map via a button.
So that means the parent view controller (or somebody) need to be able to refer to both the mapViewController and ListViewController.
How can I achieve that?
One way to do so is by using instantiateViewControllerWithIdentifier. However, I do not like identifier. Identifier is prone to mistyping and conflicts. I like Outlet
I read that one possible way to do so is to set a storyboardid. What about if I like the good old outlet?
As I understand it, you have to sub view controllers. What I would do is create a master controller class, which imports all of the other view controllers (think of it as the super class of the entire project) which will then control a empty view, less a button. The background of the empty view will be fed by an "active" view, which will be supplied by either listViewController.h or mapViewController.h This will let the users use both the list/map VC's, and be able to switch between the two.