Linking different views to tableview/viewcontroller without using a navigation controller - ios

I apologize in advance for not being able to efficiently describe my problem statement. But, I shall include a link to an image to better describe what I'm looking for. Basically I have a Viewcontroller with a TableView inside it and I have a toolbar on top with three bar button items on it. Now what, I want is to be able to have a functionality whereby the user taps on any of these buttons and we go to different views. Now, I would like a design where if possible I stay on the same screen and render the information in the tableview below depending on which button has been pressed. Visually, I'm trying to go for an effect where the button that is pressed is shown as being depressed and information is rendered and on pressing a different button a different view is rendered. I'm familiar with attaching different view controllers to all the buttons and then segueing into those viewers. However, I would like to know if there's a way in which I can stay on the same view controller and just use sub views. If there is, how can I do this from storyboards? Again,, I apologize for being verbose, my picture should hopefully be able to tell you what I'm trying to do.
The link to what I'm trying to achieve: http://imgur.com/GM5eH

Well, one basic solution is simply to add these other views in your controller. Now that subview is set to hidden on viewDidLoad:. Then just create an action, and show the subView. You can size that view however you want, and play around with the other view as well.
Now, there might be a better way to do this, but that is how I would do it.
EDIT - Concerning Apple's method on Calender
Now, I have never tried anything like this, so this is all theory.
First, you create three classes. Each with it's own custom view. This view should be the size you need it on the other (Main View). You can set the size to freeform in the Interface Builder.
Once you have that done, you head to your main view. That view will have the three buttons. Set an IBAction for each of those that creates an instance of each view and places it on the screen. (I am not sure how to accomplish this, but I am sure there is a way. Take a look here: Objective-c Adding subViews in my controller
You should dealloc each view after you head to another view as well for memory management.

Related

Creating a reusable control which can be embedded in UIViews and handle its own modals

There's a particular control which I'm trying to build properly. I refer to it as an ImageTile. It's basically a little square box, which, when the user taps it, will present the user (via an action sheet in a popover) the option of selecting an image from the library, or taking a photo. Depending on the response, I then either present the UIImagePickerController inside a popover (for selecting an image) or modally (for taking a new picture). Once they take/select the image, I have a modal view which appears and allows them to edit the picture in a few simple ways. When they finish editing, the modal dismisses, and the original ImageTile, rather than being a blank square box, gets filled up with the user's edited image.
The issue is that this ImageTile control is going to be used profusely throughout several different parts of the application, across numerous View Controller hierarchies, and so on... and I really want it to be a basically totally self-contained unit, such that whenever I stick an ImageTile inside a UIView onscreen, all the above functionality is handled by the ImageTile itself.
Initially, I made it a UIViewController subclass (so it could present modals etc), and just added its view as a subview of a "holder" view onscreen. I know this isn't recommended, as the controller isn't part of the VC hierarchy then... and also, I wound up with some really weird behavior regarding things like autorotation, especially when the camera was involved.
What's the "right" way to implement something like this?
I think what you've done by making it a UIViewController subclass is correct. You should just use the methods that UIViewController exposes for adding child view controllers, such as - addChildViewController:.
You will also note that Interface Builder has a Container View object designed specifically for holding a place in the hierarchy for a child View Controller:

IBOutlets/actions and custom subview

Apologies if this seems a straightforward question! I'm using AVFoundation to build a custom camera app that allows the user to draw on the image after it's taken (similar to snapchat).
I have the camera functionality working. Currently, after the shutter button is pressed I add (as a subview) a standard UIImageView to display the photo taken. Seeing as I want there to be custom options at this stage (including drawing on the image), I proceeded to create my own subclass of UIImageView. I am designing this view in Interface Builder (xib file).
Say for instance I have a button on this custom view, that when pressed, simply deletes the image and takes me back to the camera view to take another image. Can I handle the IBActions for this custom view within the ViewController for my camera view? Is this bad practice?
Any guidance on how best to implement this would be really appreciated! Thanks.
I would not suggest putting the button on your custom view.
I would have the UIViewController display the custom view and also give display a button for your action. Most likely the UIViewController will want to display a UIToolBar at the bottom of the screen which will allow for multiple buttons/actions, but that's up to you.
But why do I say this? Compartmentalization. Your custom view has no need or use for the button. It's meaningless to what your doing... displaying the image. Your custom view will also probably be used in many locations, several of which won't want this button displayed. Even if your custom view had the button it wouldn't know what to do if someone tapped it. It would then have to pass that interaction off the view controller which cares about the action and and handle it. Your custom view will mostly likely have public methods allowing other code to give it directions (like clear or undo).
Depending on the say things go, you may decide you want a custom UIViewController which handles interactions on your custom view. Then any other view controllers could just add it in as a containerized view controller and you could have the same functionality with no code duplication at several places in your applications.

Creating a UIView to be displayed in multiple UIViewControllers

I am creating a simple little popup view, similar to the popup that appears when you push the volume buttons. I would like to display an instance of that popup view in different view controllers. I have been pondering a couple approaches, but I would like to know what is the best approach, taking into account MVC, complexity, and otherwise 'good' practices.
Currently, I am creating and displaying this UIView from within my UIViewController. I justified that approach since it's really a small view and I do a lot of work with it to modify its behavior in that VC, so that code was already going to be in the VC. Essentially, I make a frame, set the background, apply corner radius, add text to it, apply motion effects, then make it fade in then later fade out. I could copy and paste the code into my other VCs but that's obviously a bad approach.
I could create a subclass of UIView and I'm sure I could use drawRect to draw it, but I'm not sure exactly how to add that view to the VC exactly in the middle, unless I drag out a view to my VCs and change its class. But if I do that I can do most everything in Interface Builder anyways, which would be preferred especially if I can use Auto Layout to always keep it centered. But, I'd need to copy and paste that UIView into each VC and hide it - that doesn't sound good.
I could create a subclass of UIView and instead of drawing with drawRect, implement a method that creates the UIView and returns it. Then in the VCs I just call that method and add the view it returns as a subview. I've never done this, and I'm not sure if that's an appropriate approach.
What is a plausible approach to implementing such a view that can be thrown on screen from any of my VCs? Thanks!
Note that this view should always be the same size, in the center of the screen, not tied to any specific VC. It should remain on screen unaffected by transitions and such. It closely mimics the Volume popup.
I would like to display that same popup in multiple view controllers.
I expect that you mean you'd like to have separate instances of that same class in multiple view controllers. A given view can have only one superview, so it can't exist in more than one view at a time.
I'm not sure exactly how to add that view to the VC exactly in the middle
It's easy to center a view in its superview. To center horizontally, subtract the width of the view from the width of the parent. Divide the result by 2. That's your X coordinate. Same goes for the Y coordinate, except that you'd obviously use the heights.
An even easier method is to create a point by dividing the superview's width and height each by 2. Set your view's center property to that point.
What is a plausible approach to implementing such a view that can be thrown on screen from any of my VCs?
Don't try to reuse the same view. There's no need for that, and trying to pass it around between controllers will really complicate your code. Just have any controller that needs to display your popup create its own copy.
Remember, views are the interface to the data that's stored in your model -- they can display that data or let you interact with the model, but they shouldn't store app state themselves. Given that, there's no reason that you'd need to use the very same view in more than one view controller. As long as your pop up gets its data from the right place, you can have as many instances of it as you like.
If your popup really is separate from the content of any of your view controllers, another possible strategy is to use view controller containment. You can create one view controller that handles just the "app-wide" stuff, like this popup, and have it load and unload the various other view controllers as it's children. I'd caution against trying this, though -- it's probably more complicated than you need and surely more complicated than you should attempt right now given that you seem to still be getting your sea legs.
It sounds like MBProgressHUD is what you're looking for. FFCircularProgressView might also help.

Nested UINavigationController

I'm fairly new to iOS development and I want to make something like the attached screenshot.
I've read the nested UINavigationControllers is not a good idea, if not impossible, so I'm more than willing to accept ideas on how to implement what I want.
On my second screen there are two views I want to switch back and forth between (NOT using gestures and push animated), while maintaining a static header and footer.
The only way I know how to do a push segue is with a UINavigationController, hence why I see the need for nested ones.
How do I implement this? I thought that in my second UIViewController I could programmatically create a UINavigationController and add it to self.view, but I ended up doing this:
self.subNavigationController = [UINavigationController alloc] initWithRootViewController:[someVC alloc] init]];
[self.view addSubview: self.subNavigationController.view]
But that only adds the view of self.subNavigationController to self.view, not self.subNavigationController itself.
Like I said, I could easily be way off base on how I should be handling it, any advice is appreciated.
OK, if I've got this right then what you want is an app where you can navigate between different views (like any other app).
This will use a UINavigationController (lets call this "nav1") and this is the bit that you're OK with.
However, you also want a view that will have a static navigation bar. i.e. as far as nav1 is concerned this is only a single part of the navigation through the app. It also wants to have two potential views in there. (the map and the table view) and it will have a static bar along the bottom too (in IB this is a ToolBar I think). Lets call this the "mapAndTableViewController".
You haven't said exactly how this works but you don't want swipe gestures between them. So I'm guessing you will have a button or something to swap between them?
Something like this...
http://www.youtube.com/watch?v=eg6vWGh67zk&feature=youtube_gdata_player
The way I've done this is to use a single view controller with a scroll view. On the scrollview set scrolling disabled and set paging enabled. This sounds odd but this removes the touch scrolling from the scrollview while still allowing code access.
Now, in the mapAndTableViewController you populate the left and the right hand side of the scroll view.
What you do here is now take the two view controllers... mapViewController and yourTableViewController and instantiate them. Then add the views of these controllers in to the scrollview left and right sides accordingly.
Essentially you will have THREE view controllers. One for the scroll view, one for the map view and one for the table view.
Without any further explanation from you I can't really help beyond this as I don't fully understand what it is you're trying to achieve.
Hope this helps...

Using SwipeGester to swap views(of the same instance) - screenshot inside

Here is a screenshot of a view that I have right on my device.
The design issue that I am having here is that the top part of the screen is always going to be static - as far as its placement. The rest of the screen are a row of buttons added to this view programmatically. The arrows represent the idea that you could swipe in 4 directions(from top, from bottom, from left and from right) which would animate a new view onto the screen. This view is the same instance as the view before it. In fact all these views are the same instance but the buttons will be different.( i dont want to get too specific here.)
My design right now calls for pre-loading the views ahead of time. The data for each button for each view will be in core data. I will not know ahead of time how many views there are. One view might just have a view to the right that you can swipe in from the right and that view might have a top and bottom arrow - that would allow you to swipe from bottom or top that would be another view(same UIView subclass). So basically a tree of views.
I guess I am trying to figure out my options. A NavigationController is not really what i want because i have no need for a navigation bar, although in my mind it makes sense that i would have an array of view controllers here each with its view property pointing to each view that is allocated and then as i swipe i would bring in the appropriate view by using the view controller index.(through some animation code)
Another possible option would be UIScrollView but that seems cumbersome and may not be what i would really want.
One of the easiest setups would be to create a XIB file that would consist the top part of the screen and on the bottom an empty UIView that i would programmatically populate with the buttons(and their unique data). The problem that I am havign with this is, is how would i swap the views this way. I guess i could make the rootViewcontroller the first viewcontroller instance with the first view and then swap them.
I guess I am wanting to see if anyone had any questions or suggestions to come up with the easiest(most modular) approach to swiping in different views. Is using an array of view controllers the way to go?
A couple of thoughts:
This screams custom container view controller to me (if iOS 5 and above). See Creating Custom Container View Controllers in the View Controller Programming Guide for iOS.
You talked about using UISwipeGestureRecognizer. You could always contemplate UIPanGestureRecognizer, too. It's nice to have continuous gestures. Think about reading a book where the page swipe tracks your finger, but you can stop and go back, mid gesture. Sure, start with swipe gestures for now, but if your UX lends itself to it, you can always contemplate continuous gestures in the future.
You said that you're planning on "pre-loading the views ahead of time". Generally, given the limited memory capacity of mobile devices, you'd want to be more conservative than that. Maybe load the current view, and the four ones that you're likely to go to in each of the four directions (so that you can present them instantaneously), but leave it at that. Once you go to one of the four possible destinations, then go ahead and release the ones that are not reachable from the current one and tee up the ones that are.
One Xib is enough for you (for this part of your app anyway).
Don't use a UINavigation Controller. The NavController metaphor is one of a stack of cards. You don't have that data structure.
The general idea is one ViewController for one screenful of stuff. If you feel the need for two viewControllers (one for the top part, one for the bottom part) then you are going to have to look at custom container controllers, to ensure that the contained controllers receive their instance methods correctly (viewDidLoad, viewWillAppear, etc). An example container controller managing a pair of viewControllers is the iPad splitViewController. But I don't think you need to do this.
I suggest placing a scrollView on the lower half of the screen and using that to manage your data views. If the upper part of the screen also needs to change (under other conditions) you could even have two scrollViews, one up top, the other below. They can be paged, and contain views that are the exact size of the respective screen portion. They can share their single containing viewController as a common location for their delegate methods.
I can't really help you with more detail, as I don't have a precise enough idea what you want to achieve. Perhaps you should try and implement it one of these ways and come back here with more questions as your ideas become concrete. Start out with the simplest idea (eg scrollView inside a single viewController), only chuck it out when you find you have to break it!
update
following your comments, I do think a scrollView might work for you. I think that managing a stack of view controllers with a custom container controller (as Rob suggests) could get overcomplicated. You will have to create your own custom container controller, the pre-existing ones such as UINavigationController are not appropriate for your data strucure (from what I can gather anyway).
You won't need to manage tons of UIViews, in fact you only need 5 - one for the onscreen portion of the scrollView, one for the screen immediately left and right, and similarly for the immediate one above and below. You can reuse these views as you swipe, much the way that tableViews reuse their cells. The rest of it will be about manipulating your data so the right content is arranged in the views as they come onscreen.
See my answer to this question for some more ideas on this: UICollectionView horizontal continuous loop

Resources