Overlapping UIViewController subview content despite separate navigation controllers - objective-c

I have a custom UIViewController called ProductDetailViewController, it has lots of subviews and each of the subviews' content is populated by data requested from a remote server.
There are two ways you could end up looking at a product detail view: in one tab of a tab bar controller you can browse to a product and one gets pushed onto the stack of the navigation controller specific to that tab, or in the other tab you can scan a barcode and a ProductDetailViewController gets pushed onto a separate navigation controller within that tab.
The strange thing happening is that if you have a product detail up in one tab, and then bring one up in the other, when you switch back to the first tab you see overlapping duplicate subview content, as in there are two labels/product images/tableviews stacked on one another within the one view.
FYI:
Nothing ever goes wrong with the second instance you push onto a nav controller stack, it's always the first one that this happens to, as if subviews were added into the existing controller as well as the new instance. I don't think there are two entire product detail views being stacked - the view has a solid background color, so it would hide the one beneath. I'm quite certain I'm pushing to separate nav controllers. At first I thought maybe the incoming data was being sent to both controllers, but that wouldn't account for multiple instances of UI elements overlapping.
Has anybody ever run into anything like this? It's a first for me for sure, and it's driving me nuts.
Screenshot of overlapping product name label subviews:

Somewhere in your code you have a lot of [myview addSubview:anothersubview]
Probably you duplicating subviews when you updating ProductDetailViewController.
Try to insert next code before u start to populate all subviews
[[myview subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];

The issue is that many of the subviews aren't actually created until data is received, and the application makes use of NSNotificationCenter to determine when data has been returned and what class and function should receive it. I should have mentioned the use of NSNotificationCenter initially, but it hadn't crossed my mind that it could be this type of problem.
I send a string to my API response notification class as an identifier, and by changing that from being the same between all ProductDetailViewControllers, to a unique value by concatenating the requested URL to my string, my problem has been solved.

Related

How come my uitextfield(s) keeps being disabled after model?

I made an app for my wife to track inventory. it has a couple of view controllers to enter a new item, display all items, search items, and display selected item. After switching through each view a couple of times (no particular order) all of the textfields become disabled (you cannot select them to change the value) across ALL of the view controllers. The buttons still work however. I have it setup so that when the user enters info into a particular cell, it saves it into memory so that if they switch views and then come back, the cells will get the data in the viewdidload method. Please Help!
*edit - I am using storyboard with a bunch of segues (i'm new to programming and self taught, so idk if this is the right way or not)... here is a pic of my storyboard
http://i.imgur.com/aT3PR0i.png
Ok, with a look at your storyboard, I think I see your problem. If your storyboard looks like somebody spilled spaghetti on it, it's not designed correctly. Specifically, it looks like you're going "backwards" (to controllers you've already been to) using segues -- that's not good. SEGUES ALWAYS INSTANTIATE NEW CONTROLLERS! So, if you go around in a circle a few times, you're adding more and more controllers to your project. To go backwards using segues, you need to use an unwind segue -- it's the only one that is an exception to the rule that segues always instantiate new controllers. The other ways to go backwards, without segues, are popping with navigation controllers, and dismissing modal controllers.
I can't really diagnose your immediate problem without a lot more information, but redesigning the storyboard using unwind segues would probably fix your problem.

Using a shared TableView for 3 of my tabs

I want to use the same TableView for 3 of my tabs instead of using 3 identical TableViews. I created three navigation controllers (one for each of the tabs) and linked them to the same Table View Controller But if I run the app with the storyboard like the picture below, it works for the first one of the sharing tabs, but for the other two I get a black screen where the tableView should be. So I want to know if it is even possible to make it work with this setup?
I'm trying this, so I don't have to make a litte change in the tableview 3 times.. The 3 tabs are populated with the same data too, just filtered differently, so just filter the array depending on which parent navigation controller would be simple I suppose.. But I need to know if this is a possible way of sharing view or not.
I would suggest a different approach. Just have three different table views. But since the question is not about being the right approach, I would say that the best way to do it is to do it by code, removing the tableview from the super view (The view controller's view) and moving it to a new view controller when the delegate from the tabbarcontroller is called. Keep in mind that you will also have to assign the delegate and datasource for each view controller.

Reference to source view controller and destination view controller at the same time

I have a series of UIViewControllers throughout my application. Most of them have the navigation bar but some of them hide it.
The problem is that sometimes as you transition between a view with or without navbars to another view with or without navbars there is a black box that replaces the navbar during the transition. This problem was discussed here: Hiding a UINavigationController's UIToolbar during viewWillDisappear:
This solution is fine and it does get rid of the black box, but I really don't want what was described as a "Cheshire Cat" disappearance. I've tried myriad solutions using prepareForSegue, ViewWillAppear, viewWillDisappear, etc. The best I can do is change the scenario in which the black bar shows up.
By this I mean, there are four combinations of view transitions between the two navigation bar states (hidden vs. not-hidden):
Hidden - Hidden
Hidden - Not Hidden
Not Hidden - Hidden
Not Hidden - Not Hidden
No matter what solution I've tried, at least one of those combinations results in my black box rearing its ugly head. The problem I have is that I've been unable to find anywhere that I can get a reference to the source view controller and the destination view controller when popping a view off of the navigation controller's view stack.
If I could get both references in the same event, I could simply determine what the combination is and handle the behavior appropriately like I would in prepare for segue.
Now, I know that "it's not possible" is a reasonable (and even a probable) answer, but I won't accept that as a solution alone. If it is indeed not possible, I'd like thoughts on a reasonable alternative. For example, I could handle all view controller popping manually (including the default back button) and thus could get the "upcoming controller" from the navigation controller's stack.
I would just prefer a solution using built in APIs or at least a solution where my controllers didn't have to be aware of their own navigation bar states.
Thanks a lot,
Patrick
I think UINavigationControllerDelegate is what you're after. It declares two methods:
-navigationController:willShowViewController:animated:
-navigationController:didShowViewController:animated:
All you need to do is set yourself as the delegate of the parent navigation controller and implement these methods to be notified of incoming view controllers.
Having said that, I've never needed to resort to this for hiding and showing navigation bars. Strictly speaking, view controllers where the navigation bar will always be visible never touch the navigation bar's visibility. When I'm moving into a view controller where it needs to hide, that view controller is responsible for hiding and setting it back to its prior state before disappearing. Following these standards has proven reliable for me.

Adding navigation controller to custom made table view

I want my table to show up only half way from the bottom of the screen, the upper half meant for an image. To do this, I added a table view through code using CGRectMake specifying the (x,y) co-ordinates I wanted it to start at.
Now, I want each row of this table to navigate to a new view. How do I make the navigation controller push my new view? I tried to add a navigation controller to my main view, but that its helping any.
Please advise on the best way to do this.
Check out the UITableView Class Reference, specifically the methods under the header Managing Selections:
The method you most likely want is – selectRowAtIndexPath:animated:scrollPosition:. You can push a view controller onto the stack from this method based on the selected row. To push onto the stack, you will need to set up the UINavigationController, then call
[self.navigationController pushViewController:viewController animated:YES];
Apple has a nice tutorial on this topic, complete with many examples.

Can I use UITabBarController as a simple viewController switcher?

I'm creating an iPad app based on a UINavigationController (with the bar hidden) so I can push and pop other viewControllers for navigation around the app. However, I am now wanting to add a section in which there are two viewControllers that I want to be able to switch between, so in other words they are side-by-side, rather than hierarchical.
Is it okay to use a UITabBarController for this? I am aware that on the iPhone it is recommended they are used only at the root level of the app, but since this is an iPad app I wondered if I could use it? Also, I guess I need to create an empty viewController, create a UITabBarController within it and set the delegate to it, then add the two viewControllers to it... So in effect I will have a viewController within another viewController, and when I have done that in the past the results have been very flaky.
Can I do it this way? The only other way I can think of doing it is to have two plan UIViews within a UIViewController, but that also means I shouldn't really put any business logic in them (bad MVC!), and not being able to will be a right pain in the a**.
Any ideas?
Thanks!
:-Joe
EDIT: I also need to be able to swipe-animate between the two VCs within the TabBarController, AND have a menubar over the top which doesn't animate... Can I do this?
Sure.
I do this kind of thing all over the place in an app I'm working on. I actually have several different types of "toolbars" that can be optionally shown at different times.
What I do is create a "parent" member in my toolbar's class - and when a button is pressed, I have the toolbar call a method in the parent class to do whatever needs to be done - (i.e. display another view).
This avoids the whole mess of creating a view inside another view (or viewcontroller inside another viewcontroller - or whatever) - the toolbar can take the button hits, but all the views are opened by the root view/controller.
Hope this helps/makes sense!