I have three UIViews one below another in my application.The UIView's data is filled by API responses. I want to hide the UIView when it's corresponding API response is nil. I am checking the API response and if it is nil, I am setting [myview setHidden:YES] which hides the view but here is my problem.
Say, if the second view doesn't have any data, it is hidden but the view's coordinate is still occupied and I get big blank space between my first and third view.
To solve this problem, what I did was I used UIView animation method to move the third view in to the second view's coordinate space if second view data is nil.
This works fine for me but this kind of approach adds complexity to my code if I add fourth or fifth view. I am sure there are better solutions than my approach and would be helpful if you can guide me to a better approach than mine.
Thank you
Among the solutions provided I went with the solution provided by Jonah.at.GoDaddy which suited my app. Thank other too for providing great inputs.
Why not use a UITableView with 0-3 cells depending on on how many api responses you have?
What you could do is check the APIs and only do
UIView *APIView1=[[UIView alloc] init];
[self.view addSubview:APIView1];
when you know the API is available.
This could be a good approach for you:
1. Define coordinates for each place you would like to use for placing view and store those coordinates in array A.
2. Each time you add/remove a view to/from superview, add/remove reference to that view to/from array B.
3. After adding/removing references run a loop and assign coordinates from array A to view's frames from array B. Assign them through array ids.
4. Put that loop in UIView animation block.
Now you can use any number of views and remove/add them in which place you want.
Related
I'm creating an app to increase my knowledge.
I have a ViewController which receives information, and with that information he shows some Views. I've already done something like this but in the end that was a mess and had way too much work to change what I've done.
In my attempt I had some views in the same place as others and just changed the hidden property to NO if I didn't want them to be visible, at the time that was the best option for my knowledge(4 months of iOS) and I thought to myself that must be other ways and better ones to do this type of ViewControllers.
So now comes my question.. What is the best way to do a ViewController which can change accordingly to the information he receives??
A base ViewController and the others are subViews from that ViewController??
Example Updated: I can receive N type of news. Some have an image in the top of the view, others have a scrollview like a photogallery to show more than one image, others can have a title with an image and so on.
Others can have text, others one webview to show a video, others can have a collectionView to show some additional information.
What I would do is have several UIView subclasses. So the setup would look something along the lines of:
YourViewController has a view, that is only there to display subviews
Then depending on the data you receive you instantiate one of your UIView subclasses that are made to display the data you need ( you could also design them in interface builder and access them as a property on your UIViewController ) and add it as the subview to use
Whenever the data needs to change again, remove all the subviews and add the newly needed subview.
ViewController.mainView -> removeAllSubViews -> addNewSubView
Alternatively you could have multiple view controllers I guess, but it kind of depends all on how and what you need your views to do.
For instance if you need more than just a display of data, and some interaction / manipulation of your data you might want to consider using multiple ViewControllers
This question maybe already asked in stackoverflow. But, I did not get any clear idea to my scenario.
I have a viewcontroller (Say, MyViewController).
I have a scrollview(Say, MyScrollView) and I have N number of views (Say MyView1, MyView2, ...) in it.
Those views can be scrolled horizontally. Refer the below image for more clarification.
This image was taken from here.
So, the red area is the scroll view that holds multiple views which are yellow color.
Scenario:
I want to call API's for each view, when the API calling and parsing data occurs, I need to show some loading activity indicator in the views. After successful parsing, I need to update corresponding view with UITableView.
Questions:
In my case, the number of views may vary from 3 to 6. Should I maintain 6 separate UITableViews and UIActivityIndicator's?
I tried with three pointers like left, middle & right to hold reference of tableview and activity indicator. But the problem is, before the first three pages are loading, if the user goes to the fourth view, this system will collapse with so many conditions.
Suggestions needed. Confused!!
I think you have to use UIPageViewController with UITableView. it will solve your problem.
I'd like to implement a feed in iOS 7 that is only pulling new data if it appears the user has a chance of viewing it. I know UITableView is a subclass of UIScrollView that does this well, but I can't get the cells to behave the way I'd like - I'd like it to look similar to facebook or twitter's newsfeed where you can reload by pulling down and load more by hitting the bottom.
I've seen other questions such as this but I'm looking for vertical scrolling only, with absolutely no horizontal movement. I'd like to use a method calling (for example) nextTenPostsFromIndex: that instantiates from zero, and applies to whatever index was the last on the screen if hitting the bottom.
My question: Is the best way to go about this to create a UIScrollView, place a subview inside of it, and programmatically space them on the available scrollview space, and apply the same drawing methods again with repopulated data if nextTenPostsFromIndex is called or checkForNewPosts (if pulling down from the top)? Would this be a reasonable way to implement Facebook's iOS7 newsfeed, or others like it?
Short answer: No. You really ought to use UITableView for things like this. You can use a custom cell or whatever you want. If it helps you, also know that since UITableView is a subclass of UIScrollView, you can do anything you would normally do with a scrollview, including assigning a delegate which acts based on the current position when scrolling.
Often, when I'm making my apps, I'm in this situation : I have a UINavigationController, handling the view stack, some UIViewControllers, controlling their respective views...
But when I want to add several custom UIViews in my mainView, I don't know how to manage my code.
Each UIViewController needs to handle one and only one view (wich normally occupy all the screen size), and a view should not control their content (update it a the extrême limit).
You can't neither do this :
[myViewController1.view addSubview:childViewController.view];
So if I want to achieve something like this, what should I do ?
The orange parts have to be 3 instances of the same UIView(Controller?), but with a content depending of a NSObject (User, obviously).
I think this very important to segment your content, this should be an easy problem, but I found a lot of contradictory answers so, what's the best practice to handle this common issue?
Theses orange views should be instances of UIViewControllers in order for it to handle their UITableViewDatasource? Is addChildViewController relevant in this case?
I already found a lot of things which work, but I don't know what should I do...
Also, I'm using xibs.
Thanks in advance if you can help me (and other people I think).
You can do it either way (view or view controller) depending on how you want to handle things. Certainly, you can have one object be the data source for multiple tables, so in that case, you would just add multiple views. If, however, you want to keep your code more compartmentalized, then add view controllers, and have each control its own view -- to do this, you do need to use addChildViewController, and use the methods that Apple describes for creating custom container controllers. Alternatively, you can use container views in a storyboard which makes the process of creating custom container controllers simpler.
You're on the right path... Create separate instances of your subviews, and add them to your view. If you will have more than 3 (for instance, imagine coverview for your music, and you could scroll indefinitely left and right), I'd take a look at UICollectionViewController ... That will help manage cell re-use.
But, if it's just 3, just create three instances with different frames and add them to your view.
Here's how I'd do it:
each orange box will be a custom view (inherits from UIView)
the view will have the label, image and the tableview.
since you are not sure of the number of instances of these views you'd be using, its better to use some kind of tagging, so that you can have one place for the datasource and delegate methods of the tables in these orange views.
in the datasource and the delegate methods, you can make use of the tableView.tag (same as the orangeView.tag property).
I personally dislike having more than one viewController in a view (except the splitVC), probably because I haven't had a such requirement.
I dont see how a uiviewcontroller for orange box would help, over a uiview.
as #James Boutcher mentioned in his answer, UICollectionViews will simplify this issue further.
Why not creating a UIView class and overriding the drawRect method and then adding subView for this class in your myViewController1.view
I'm building an app that has views that are build programmatically. That is, I am fetching data from a database that contains information on things like the number, size and placement of buttons in a view. At some point, there will be code that uses this data to instantiate new subviews and set them up. My question is, where should this code go? The view, the viewController, or somewhere else. It seems to me that this is a grey area regarding typical MVC principles. Should a view accept data, and then know how to draw itself using this data? Or perhaps, a viewController is responsible for building all the various subviews, and then simply adding them to the view.
Thoughts? Thanks.
I agree that it's a gray area. Personally, I make a decision like that based on whether it's the data that needs manipulated or the display of that data. For example, a view controller displaying a date may need to process various dates (ie, a data represented as a DMY struct vs. a date represented as a seconds count from some reference time) into a format suitable for the view, while the view itself may be only capable of receiving one particular format (ie, DMY) and is responsible for displaying that. That's the sort of line I tend to draw between the two -- displaying data (the view) vs. interpreting data (the controller).
In your example of reconfiguring a view I would probably put most of the logic into the controller since it involves interpreting the data. I would design the view to accept configuration details such as how many items to display and what sort of layout format to use (think of a UITableViewCell), but I would design the controller to interpret the data to decide how many items and what to put in the various fields within the view (like a UITableViewController).