I'm trying to replicate the functionality of the Music App (the one that comes built-in to all our iPhones/iPods.)
My goal is to have:
1) a Tab-Bar controller at the bottom, where every tab click loads a TableView.
2) Need to make sure that the Tab-Bar never disappears when these various table-views are loaded
3) the Table-Views must enable user to drill-down further and further into other table-views, sometimes 3 or 4 levels deep - but the tab-bar should never go away
4) One last thing: if possible, all the loaded Table-Views should NOT be Navigation Controllers, but regular UIViewControllers that then contain Tables (this is because it doesn't seem possible to resize tables when they're created as "UITableViewController"s, only when they're embedded into regular UIViewControllers, as-in, dragged and dropped out of the Objects Library onto an existing view.)
Anybody out there go some sort of tutorial or even code-template/engine type thing that they can share with us on how to do this?
As far as i understand, you know that it is possible to use UINavigationControllers as UITabBarController's controllers. What is the reason then not to use the navigation controllers ?
That must be completely what you want to do: create tables in the code or link it as the outlets with Interface Builder as you can either use UIViewController or UITableViewController (which inherits from UIViewController) as root and popped to stack of NavController.
Assuming you use XCode 4 here is the link to tutorial
upd:
for the header and footer views it is always better to use UITableView object's tableHeaderView and tableFooterView properties. Then you do not need to calculate the margins and update the view layout manually as it comes automatically.
Regarding the navigation, it is a general practice to set the controller containing the table to be the table delegate itself and then to use the method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
when you select one of the table cells, that method is called and you can create an instance of the next view controller, preconfigure it and then push to navigation stack. Back button comes automatically - and you are done. If you have the table in the next navigation controller, be careful while copy-pasting the methods from the previous-one, i.e. if you have wrang number of rows at
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
your application will crash.
Related
I believe my question is relatively basic but I still haven't found something that directly answers it. I'm trying to learn Objective C and iOS Programming using Storyboards and am in the process of building my first app now. The way I have it set up right now is, I have a Navigation Controller that goes to a Table View controller (controller A). I'm passing an array to my table view and I have the disclosure indicators on the different elements in my array. I also have a second different view controller (controller B) with a table view in it. I want different elements of controller A to transition to different views/controllers etc. I'm familiar with the concept of segues and the prepareForsegue method however, I dont need prepareforsegue here since I'm not really passing anything to the next screen. I just want to be able to click different elements of my Table View in controller A to bring me different views (i.e like controller B and other views that I plan to build). Is there something I'm not seeing or do I need to fundamentally change my design?
Perhaps you're thinking of creating different prototyped cells?
Here's how I understand your question:
I have two (or more) different kinds of cells where I know the view to which they would transition beforehand (when your table queries the delegate).
I would like those cells to go to different controllers rather than the same controller.
If this is right, check it out:
Create a new UITableViewCell in the table by dragging from your kit
of UI objects.
Give each cell a reuse Identifier
Control+Click and drag to your target view controller
You'll get a popup asking what type of segue and what UI object triggers the segue
Then you'll see two segue indicators leaving your UITableViewController
Finally, make sure you dequeue the right type of cell:
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"CellReuseIDA"];
return cell;
I have a tableview with custom section headers. The view for the section header is defined in the storyboard and wired to an instance variable. Is there a way to request a new instance of the view from the storyboard?
In the past I have done this by having the section header defined in its own xib file and getting a new instance by using
[[NSBundle mainBundle] loadNibNamed:#"TimerViewSectionHeader" owner:self options:nil];
UIView *newHeaderView = self.sectionHeaderView;
I dont' think there is a way to do that. Best bet is to put the tableview custom header view in a separate nib and load it like you did in your code sample whenever you need to use it.
I tried to do the same thing and ran into the same problem.
I like to work with storyboards a lot and was impressed how fast I could create a working UI. However, as soon as you need to re-use views it makes a lot of sense to put those into a separate nib along with its UIViewController subclass.
You can then place a generic UIView in all the places where your re-used view should go and add the view using your ViewController:
[myReusableViewController loadView];
[myReusableViewController viewDidLoad]; // You have to handle view callbacks yourself.
[self.myReusableViewPlaceholder addSubview:myResusableViewController.view];
[myReusableViewController viewWillAppear:YES];
So to sum it up:
Use storyboard, it's great
Create the scaffold of your application in the storyboard, along with any static view (like About screens etc.)
Create re-used views in a custom nib + UIViewController subclass and add UIView placeholders in your storyboard.
In another answer I thought about some Pros and Cons of Storyboard
The solution I've come up with for this is as follows:
I have a tableview with multiple prototype cells that displays complex data. There is a segue to a detail view, and a transaction process view.
This first tableview has a search button that displays a new tableview with the results. It needs the same functionality as the main tableview that pushes it; including segues to the detail and transaction progress views so:
On storyboard, select and copy your main tableview. Deselect and paste. Create a push segue from your main tableview to your 2nd tableview; or from where ever you want to navigate to it from. Modify the 2nd tableview as you like. IE: If it requires some UI changes no problem.
Create a new viewcontroller class that is a subclass of the viewcontroller running the main tableview.
Override the data delegate in your subclass to serve up the subset of data you want.
Back in the storyboard, select your 2nd tableview controller and in the identity inspector select your subclass as the custom class.
For this solution to work smoothly, your app really needs to be managing data for the views. You could use prepareforsegue to pass data from 1st tableview to the second, but I've found the app data model far more flexible from numerous points of view.
Unless you have buttons that push to the sub views via segue, your subclass will need to override functions that push via segues with identities. NB Segues must have unique identifiers if you id them at all.
It took a lot of trial and error to figure this out, but once you understand the concept, it's a relatively smooth solution that is quite adaptable and not so bad to implement.
I am not sure about just views, but the way that I was able to get view controllers out of my storyboard is as follows.
UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"IdentifierName"];
From here, perhaps you might be able to use this similarly to how it was once done with nibs.
I've been able to reuse a view in the storyboard just by connecting a transition from one tableview into the one I want to reuse.
so my tableview that I want to reuse is pointed to twice.
It sort of works but the problem I'm running into it setting a variable (using instantiateViewControllerWithIdentifier) in my app delegate to my table view that is getting reused.
It seems that if I reuse it, the storyboard is creating 2 instances of my tableview and the one I get with instantiateViewControllerWithIdentifier isn't the one I want.
I'm not really sure if this is the proper way to do it. But I assume many others are doing this somehow. With the custom table cells in storyboard I suspect lots of people want to reuse their views.
For example: We want to reuse the view(include subviews) in storyboard shown below.
The best solution I know so far is clip and paste the view related code to the New Singe View file without losing the information.
Detailed steps are as follows
Step 1: Rename the view we want reuse. Just prepare for step 2.
Step 2: Open storyboard as source code in order to clip the XML code we need
Step 3、4: Search and clip the code we need
Step 4.5(Not needed): Open as Interface Builder to see the view removed
Step 5、6: New XXX.xib and paste the code we clipped just now
Step 7: Important. Insert code<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/> to XXX.xib source code.
Warning: Do this before open it as Interface Builder! Otherwise, you will see wrong size and layout waring.
[![step 7][9]][9]
Step 8: New XXX.swift to connect the XXX.xib
[![step 8][10]][10]
Step 9: Add the view anywhere we want
[![step 9][11]][11]
I get warning: "You need at least 10 reputation to post more than 8 links."
Can you support me to upload the remaining 3 screenshots?
In examples I have seen of UINavigationContoller and UITableView, switching to next view is usually triggered by tapping into that cell and pushing a different UIViewController on top of stack, but what I want is to switch to next view by pressing a next button in bottom of page, which I want it to load the same UITableViewController again but with different contents in each cell.
-Can I put that next button on bottom of page? and where
-Can I call the same controller (but showing different contents) and put on top of stack using the UINavigationController?
Because I want to be able to browse back previos pages.
You would generally do as you highlighted first and push a new UIViewController subclass onto the UINavigationController.
A UIViewController is supposed to manage one screens worth of content. If you plan on breaking that convention by presenting different information you are essentially going to have duplicated if statements to decide whether the user should be viewing the content from before or after the button was tapped.
UINavigationController's are good for hierarchal data where the content becomes more specific as you drill down. The UINavigationController will also manage the stack so that you can go to previous pages.
To achieve what you want to achieve (stated here) you should be using a UINavigationController with your custom subclass of UITableViewController when the user submits questions you receive your xml, parse it and then instantiate a new instance of your UITableViewController subclass and push it onto the stack.
You can add a button in the footer view of your table view. To achieve this have a look at tableView:viewForFooterInSection:. Then add an action to that button which allocs and inits the view controller with the new content and pushes it onto the stack.
Just make your UITableViewDatasource returning your number of rows plus one in the last section you have in –(NSUInteger)tableView:numberOfRowsInSection:. Set any content for that cell before returning it to the tableView.
Then only push a new UIVIewController, when the user touches that last cell.
You could also make the delegate of the UITableView returning nil on :
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath for the cells you don't want the user select.
I am new to iPhone development, and I am working on modifications to an existing iPhone app. The high-level overview of the issue is this.
I have a button displaying a pop-up containing a UITableView.
I'd like to click on a menu item and display a second UITableView with sub-items, including a back option. If the user clicks back, they go the original UITableView. If the sub-item has additional sub-items underneath it, it should (when clicked) launch another UITableView with these options. There is also a back button as a menu item that will allow the user to navigate to the previous menu displayed.
The challenge here is that I am not using a navigation controller. the original developer only wants to add UITable Views to the stack, add transitions between them as you go from one menu to the other. Most of the tutorials I have seen and tried utilize a navigation controller and Interface Builder to associate the UITableViews.
Right now, I have an XML data source populating the menu, and when I click on a menu item, the titles change correctly, but still uses the same UITableView to display the options - this has consequences of course, as some of the sub-items may not fit on a screen.
any thoughts on how this can be done? I can post some code if necessary, although I think the general description should be able to ring a bell with one of you smart guys!
This can be done in numerous ways.
I haven't done this first one, but you can probably create a UINavigationController and set its view to the appropriate frame (inside the bubble) hide the navigation bar and set the action of your back button to pop the current view controller.
Another method is to have multiple tableviews on one controller, the delegate and datasource methods have the UITableView as an argument so you can distinguish them when setting the height of your rows, headers etc and when returning a UITableViewCell.
The way I've chosen to deal with such configurations is to have one UITableView and have only the datasource change. When you reload, insert, delete rows or reload the whole table, you can change anything you want depending on the current datasource level. The easiest none animated way is to reload the whole table.
a) If your "options" go off-screen height wise (you want fixed height) table change the - (CGFloat)tableview:(UITableView *)table heightForRowAtSection:(NSInteger)section return value
b) If your "options" go off-screen length wise either make your cell's default textLabel flexible: cell.textLabel.adjustsFontSizeToFitWidth = YES; cell.textLabel.minimumFontSize = 14; or have custom cells (lookup subclassing UITableViewCell, which is recommended) for each datasource level.
If you subclass TableViewCells remember to have different dequeue cell identifiers for each level, so the table doesn't provide you with another level's cell class.
For the "stack" of tableviews or datasources, you can have an NSMutableArray with addObject for push and removeLastObject for pop.
For animations, UITableViews can animate their rows/sections for 3. (see documentation for insert, delete, reload - Rows/Sections UITableView class reference), for 2. you can even have UIView (if not CoreAnimation as Grimless suggested) animations, that move the current tableview to the left (setFrame:) and the next tableview from the right (setFrame offscreen before animation and then in place in the beginAnimation-commitAnimation clause), but make sure you add the tableviews in a container view (the bubble interior) that clips its subviews.
Oi. This is gonna be a tough one. My suggestion would be to maintain your own stack implementation. Then, use CoreAnimation to add/remove UITableViews from your main view controller to get animated effects. So whenever the user clicks on an element in the current table view, the appropriate controller creates a new controller and table view, and then your custom navigation controller pushes the old one onto the stack, removes the old table view from the main view, sets the new controller as the current one, and adds the new table view to the main view. Kinda messy, but I think it will work.
I have a three view tab bar app the second view of which I want to contain a navigation controller. in the navcontroller the first/root view will be a custom uiview containing a uitableview that when you touch a cell will push another custom uiview in to disclose details about the touched cell.
I have found the documentation on doing this but it is not making sense to me or seems to be flying over my head. The docs say that you have to create the uiviewcontroller located in the navcontroller views or at least refer to them programatically. I have been using Interface builder and have become quite comfortable using it, so doing it programatically scares me a bit.
Also, This piece of code from the documentation seems troubling:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:myNavigationController.view];
}
above taken from "ViewController Programming for iPhoneOS" apple documentation
wouldn't this load up and display the UINavigationView immediately?
one problem is I dont want to display the navView immediately. the navController/stack is a secondary tab. So how and where do I impliment my navController(right now I have it instaciated in my delegate(which I think is correct)? I've been able to load up a basic UInavigationController with a navigation bar and a blank view, --minus the custom content view, through interface builder but I'm at a loss as to how to populate the custom content views.
Hope this makes sense.
Any help would be appreciated,
Nick
The first thing to understand is how UINavigationController works. It pushes UIViewControllers, not views. So, when something happens in your second tab (where the UINavigationController lives) you will push a UIViewController onto the stack.
This is typically done in:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
Which is part of the UITableViewDelegate Protocol.
So, when tableView:didSelectRowAtIndexPath is called, you need to figure out which UIViewController to push onto the stack. You can either load this View Controller from a nib, or create it programatically. Since you feel comfortable with IB, I would suggest loading it from a nib.
I would not worry about trivia like "where should I instantiate my UINavigationController?" right now. First, get it working. Then worry about where things "should" go.
It might be best to get the UINavigationController stuff working in a separate project, then fold it into your main project. This lets you ignore lots of little details while you focus on the Navigation Controller.