Using bringSubviewToFront in Ipad scrollview - ipad

In my Ipad app, I have a split view in which the detail view is a scroll view...
I have 3 subviews to the scrollview which are tableviews...
How do I use - (void)bringSubviewToFront:(UIView *)view to bring one of the subviews of the scrollview to the front when an action is performed? (since Views are "stacked" in the order they're added with the last one on top). Should I write the code in the subview or in the detailViewController and how do I call it?

You write the code in the controller.
The controller should have access to the table views through IBOutlet properties. Or, if you didn't set them up using a nib, the controller should have been the one to create them.
If a button tap, for example, is responsible for showing a particular table view, the button's action handler method is where you call the bringSubviewToFront: method.
HOWEVER: It sounds like you have three table views on top of each other and are using bringSubviewToFront: to show the current one, and they are all inside a UIScrollView.
Each UITableView contains a UIScrollView. Don't put a scrollview inside a scrollview, they will fight over tracking touches and things will get weird. Just put the three table views inside a plain UIView.
Instead of bringSubviewToFront:, you ought to consider hiding the inactive views (call setHidden:). That way, the hidden views won't be considered part of the responder chain (won't get sent events).

Related

Drag & Drop in MAster-DetailView Controller in iOS

I want to drag from the masterview tableview Cell to DetailView.
I try with touchesBegan & touchesEnded method but not working.
Can you please help me for this?
Thank you
This is actually not easy. You can start by
Adding a pan gesture recognizer (UIPanGestureRecognizer) to the root view controller's view (UIWindow.keyWindow!.rootViewController!.view).
When the pan starts (i.e. user touches the screen), loop through the master view's table view's visible cells to see if the point is inside any cell by using UIView's convertPoint:fromView:. You may need to adjust timing to avoid interfering with table view's scrolling and tapping.
If a cell contains the pan's point, create an "indicator view" (that shows that user is dragging) and add it to the root view controller's view, on top of everything else and position it properly, e.g. under user's finger.
When the pan changes (i.e. user moves his finger), update the indicator view's location.
When the pan ends (i.e. user releases his finger), check if the point is inside the detail view and do whatever you need to do.
Check this out. It demonstrates how to do drag drop within a view. Your problem is more complicated as it involves different view controllers, hence the touch handling must be done at a level higher than both master and detail view controllers.
Why are you using touchesBegan & touchesEnded methods? If you have the tableView you should use didSelectRowAtIndexPath delegate method. And you can also use segues if you are using storyboards.

MVC: should i add and implement touch gestures in the View Controller or my custom subclass of UIView

I have this custom subclass of UIView, called productCardView, which is pretty simple and has some UIImageViews and UILabels as it's subviews. I add the subviews and set them up the - (void)drawRect:(CGRect)rect method and everything sits nicely.
In my view controller I get some data from a remote server and so populate the productCardViews that should be seen. the purpose is when the user taps on each of these cards the program goes to a target url(a NSURL property of each card).
THE QUESTION IS according to basics of MVC, should I add a UITapGestureRecognizer to the view, inside my implementation of the productCardView or in my view controller?
If I am to add it in the view controller, Basically I would put the appropriate code in the viewDidLoad method, where I create instances of cards, but in case I should implement it in the View itself, where should I put the code? (in -(void)drawRect:(CGRect)rect?)
Whether the tap is on the card or on the view controller you should be loading the url from the view controller.
So, that means either...
Card feels the tap and calls a function in its delegate (the view controller) that then loads a URL.
or
Tap gesture recogniser (in the view controller) gets the tap from the card and open the URL.
If the card is a control used in several places (or if you have several on the screen) you might be better making the productCardView a subclass of UIControl instead of UIView. (UIButton, UISlider, etc... are all subclasses of UIControl).
There isn't much you need to change but you can do something like...
[productCardView addTarget:self action:#selector(cardTapped:) event:UITouchUpInside];
just like a button.
You then handle the touch in the card view and trigger the action for the event UITouchUpInside.

How to have the same subView in different ViewControllers in iOS?

I have 1 subView that should be visible in two different View Controllers in my app (it is a main button in my app with a badge that shows new messages and other relevant information to the user).
What is the best way to achieve this?
Do I have to duplicate the subView in both View Controllers?
UIView's addSubview
addSubview:
Adds a view to the end of the receiver’s list of subviews.
(void)addSubview:(UIView *)view
Parameters
view
The view to be added. After being added, this view appears on top of any other subviews.
Discussion
This method establishes a strong reference to view and sets its next responder to the receiver, which is its new superview.
Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.
So don't try to do that .
You should simply have two instances of the view that look the same and have them in the same location.
You might want to have central creation method (that also updates all buttons) and target for this button.
Create Your SubView`s CustomView And respectively add the view to your view controllers ,then save the information with NSUserdefualts in one viewController and get it from another ViewController.

Resizing UIPopoverController according to number of ChildViewControllers

I am trying to create a popover that looks like the below
Basically, it has three main sections - a header which has some labels in it, a body and a footer which has 2 buttons. The body is made up of a variable number of ChildViewControllers.
Question
1) how should I go about doing this? I am thinking in viewWillAppear I call a web service which asynchronously returns me the number of ChildViewControllers to create. In the callback method I then create the ChildViewControllers, put them into an array and also add them as ChildViewControllers using addChildViewController. Will it be a problem adding ChildViewControllers outside of viewDidLoad or viewWillAppear etc and in the callback method?
2) this view controller is going to appear in a popover. how can I resize the popover in the view controller such that I still get to keep my header and footer (essentially only the centre portion with the varying number of childViewControllers gets adjusted.
in the end I implemented this using a UITableViewController. The header is a UIView implemented as a table header. While the two buttons are put into a UIView as the table footer. ChildViewControllers are put into UITableViewCells. I then disabled scrolling on the UITableView.
This method made it very easy to achieve the variable height of the center portion while retaining header and footer.

How to place a button below a UITableView managed by UITableViewController

I have a screen with a table view managed by a controller based on UITableViewController. I would now like to affix a button to the bottom of the screen so that it does not scroll along with the table view cells.
Replacing the UITableViewController with a plain UIViewController is not an option, as I want to include a UIRefreshControl. From what I have read, using a UIRefreshControl without a UITableViewController is currently not possible without resorting to any hacks / relying on undocumented behaviour.
I tried using a UIToolbar provided by the navigation controller which my table view controller is contained in, but there are two problems with that approach:
I only want the toolbar with the button to be present in the top level table view. However, I have not found an elegant way to only show the toolbar for the top level table view. I want the toolbar to animate out of the screen to the left together with the table view when I drill down.
I have not found a way to increase the height of the toolbar. Would I have to put the button in a dummy UIView?
This is approximately what I am aiming for:
http://pic.rockmynews.com/img/free-iphone-app.png
Another idea is using a container view controller containing the button at the bottom and the table view controller above it as its child view controller. But that seems a lot of work for something this simple.
So what is the best way to do this? Any recommendations?
Create the UIView container view. But also create a UIViewController subclass to manage the container. Then, add your existing table view controller as a child view controller and add the table view as a subview. Now you can control all of the subviews and positions while still having automatic refresh control operation.
Just use
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
and return a UIView for the desired button. and
tableView:heightForFooterInSection
and return the footer height
If you aren't willing to use a container view controller, you could use a third-party pull to refresh control like https://github.com/samvermette/SVPullToRefresh instead. This has the added benefit of being backwards compatible with iOS 5, if you want to support it.
You can use a UITableViewController. Just override viewDidLoad and then resize the tableView to make room for your extra view and then add your view programmatically. Make sure to set the right resizing mask so that things look correct on different screen sizes. (Easily tested in the Simulator by trying it out on the iPhone4 and iPhone5)

Resources