I am working on navigation based application in which i can navigate to many views starting from default UITableView which is starting view in application template.
I have added another UIView and added tableView control on that UIView.
I am calling that view form one of the many views on a button click. Its showing the view but not populating the data in the table. And I also want to handle the event when user taps on the cell of the table of that view. Below is the Code I am using on button click:
if(self.lstView == nil)
{
ListViewController *viewController = [[ListViewController alloc] initWithNibName:#"ListViewController" bundle:nil];
self.lstView = viewController;
[viewController release];
}
[self.navigationController pushViewController:lstView animated:YES];
self.lstView.title = #"Select";//#"System";
[self.lstView.tblList.dataSource fillList];
Below is the fillList function code:
-(NSArray *)fillList
{
NSArray *tempArray = [[[NSArray alloc] initWithObjects:#"Item 1" , #"Item 2" , nil]autorelease];
return tempArray;
}
I am pretty new in Iphone programming and don't have much understanding.
Helping with detailed description and code will be highly appreciated.
Thanks.
try setting tableviews datasource and delegate property
tblList.dataSource=self;
tblList.delegate=self;
Rest of the work you have to do in delegate methods of the table view..
Related
Hi there I am currently testing how to develop an application that has several tableviews with their own parent/child structures that are accessed from a main menu.
I would like to know how to generate the new tableview with the uinavigationcontroller menu but a shared uitabbar as this is the first time I have tried anything like this normally I just stick with apples templates.
Here is a general acitecture of what I am wanting to achieve, any comments suggestions code example would be greatly appreciated and from there I can work through it myself, its more of a question as to where the heck do I start :)
So far I have the main window set up with a UIAction catching the different button clicks, I need to figure out how to allow all children to share the specific UITabbar and then how to set up so that individual branches have their own UINavigationController menu if needed.
this is my UIAction
//Delegate.m
//--- Caputer button clicks ---
- (IBAction)buttonClick: (UIButton *) sender
{
if ([sender isEqual:orangeButton]) {
NSLog(#"orangeButton Pressed");
}
else if ([sender isEqual:blueButton]) {
NSLog(#"blueButton Pressed");
}
else if ([sender isEqual:greenButton]) {
NSLog(#"greenButton Pressed");
}
else if ([sender isEqual:purpuleButton]) {
NSLog(#"purpleButton Pressed");
}
}
If you created a "TabBar based application", then adding the navigationController for the desired tab is pretty easy. Drag the "UINavigationController" inside the "Tab Bar Controller" in the Main Window:
As to "How to generate tableViews", the simplest way, is to create a generic TableView, call it LevelTableView.h/.m file (and it could have its own .xib), where you can add whatever you wish, and then, keep creating new ViewControllers, for instance, in a level of this tableView:
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
LevelTableView *anotherLevel = [[LevelTableView alloc] initWith...];
[self.navigationController pushViewController:anotherLevel animated:YES];
[anotherLevel release];
}
The point is, that this "LevelTableView" is created once, but instantiated various time for each level you want to add, the content is the only thing that changes.
I have exactly something like that. What I do is that I created a different class which I call "TabBarController" and where I initialize all the different views that are going to my tabs:
UIViewController *viewController1 = [[FirstTab alloc] initWithNibName:#"FirstTab" bundle:NSBundle.mainBundle];
UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:viewController1];
UIViewController *viewController2 = [[SecondTab alloc] initWithNibName:#"SecondTab" bundle:NSBundle.mainBundle];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:viewController2];
myTabBarController = [[UITabBarController alloc] init];
myTabBarController.viewControllers = [NSArray arrayWithObjects:firstNavController, secondNavController, nil];
Then when the user clicks on one of the buttons in the main view this is what I do:
- (IBAction)yourAction:(id)sender {
TabBarController *tabBarController1 = [[TabBarController alloc] init];
tabController.selectedIndex = 1; ==> chose which tab you want to show
[[[UIApplication sharedApplication]delegate].window setRootViewController:tabBarController1.myTabBarController];
}
Does the job pretty nicely.
Add the Navigation Controllers into the tabbar controller. Then Hide the NavigationController and according to your schema use toolbars to pop and navigate your views.
So I have an interesting design question regarding an app I'm developing for the iPhone. I am creating an app that manipulates images, and there are different types of manipulations that can be performed. So the user opens the app, and selects what type of manipulation they want to perform, and are taken through a step by step process to perform the manipulation.
A lot of the manipulations are similar, so code can be reused here. So instead of creating a view controller for each window of each manipulation, I decided to create one view and one view controller. The view contains the steps of each image manipulation, and each time it is incremented to the next step, it reorganizes itself appropriately. The view controller is controlled by a navigation controller, and each time the user advances to the next step of whatever image manipulation they're trying to perform (ie pushed a new view controller on the stack), I make a copy of my view object, set it to reorganize its components to the appropriate step, then send it to the new view controller which will display it.
So my question is this, for some stages of the manipulations, I need to add some buttons to a universal toolbar that is attached to view controller (since this is a modal view, this tool bar will have a home button that will enable the user to exit back to the main screen). Basically, I have a couple of questions on how I should approach this:
1) Should I simply add the toolbar to the view that I'm using, instead of the view controller. If so, how would I have the home button on the toolbar exit the modal view?
2) Should I keep the toolbar on the view controller, and have my view return a set of buttons to be added to it when the view loads? Then I guess I would have to list all of my action methods in my view controller?
3) Should I keep the toolbar on the view controller, but send a pointer from the toolbar to my view object, then add the buttons within my view class? Would I be able to add my action methods in my view class then?
Anyhow, sorry if this is complicated, and if you have any follow up questions please let me know.
1) Ok.
For dismissing, does your view have a pointer to the view controller? How about something like this:
[self.viewController.parentViewController dismissModalViewControllerAnimated:YES];
Not sure if I understand exactly how your hierarchy is organized. That's just my guess.
2) That seems kludgy to me. You'd have to define some sort of data structure that describes what the view wants in a button, make a list of them. The view controller has to request that list, go through them.
3) That seems like the best option. But I wouldn't have your view directly add subviews to the toolbar. Create a ToolbarView custom view. Give it some kind of addButton method, with parameters that describe what essential attributes you want the button to have, like title and the target and action maybe. Let the ToolbarView decide what it looks like, where it's positioned, etc.
Can your action methods go on your view class? Yeah I guess, but they shouldn't. The recommended iPhone design pattern is that views shouldn't do anything, they should just show things. Methods that do things should be on view controllers, even if the only thing they do is change what views are being shown.
I finally came up with a solution for this. What I did was create a universal view controller called UIMainViewController that obviously inherits from UIViewController. I implement the toolbar like follows:
- (void) viewDidLoad
{
[super viewDidLoad];
[self assembleToolbarButtons];
[[self navigationController] setToolbarHidden:NO];
[self setToolbarItems: toolbarButtons];
[[[self navigationController] toolbar]setBarStyle:UIBarStyleBlack];
}
- (void) assembleToolbarButtons
{
NSMutableArray *toolbarButtonsTemp = [[NSMutableArray alloc] init];
[self setToolbarButtons: toolbarButtonsTemp];
[toolbarButtonsTemp release];
if ([self mode] == UIMainViewControllerMainMode)
{
UIBarButtonItem *createAPictureButton = [[UIBarButtonItem alloc] initWithTitle:#"Create" style: UIBarButtonItemStyleBordered target:self action:#selector(loadCreateAPictureModalViewController)];
UIBarButtonItem *accountButton = [[UIBarButtonItem alloc] initWithTitle:#"Account" style: UIBarButtonItemStyleBordered target:self action:#selector(loadAccountModalViewController)];
UIBarButtonItem *helpButton = [[UIBarButtonItem alloc] initWithTitle:#"Help" style: UIBarButtonItemStyleBordered target:self action:#selector(loadHelpModalViewController)];
[[self toolbarButtons] addObject: createAPictureButton];
[[self toolbarButtons] addObject: accountButton];
[[self toolbarButtons] addObject: helpButton];
[createACaptionButton release];
[accountButton release];
[helpButton release];
}
else
{
UIBarButtonItem *homeButton = [[UIBarButtonItem alloc] initWithTitle:#"Home" style: UIBarButtonItemStyleBordered target:self action:#selector(exitModalViewController)];
[[self toolbarButtons] addObject: homeButton];
[homeButton release];
}
}
-(void) loadCreateAPictureModalViewController
{
CreateAPictureViewController *createAPictureViewController = [[CreateAPictureViewController alloc] initWithMode:UIMainTableViewControllerModeModal];
UINavigationController *createAPictureNavController = [[UINavigationController alloc] initWithRootViewController: createAPictureViewController];
[[createAPictureNavController navigationBar] setBarStyle:UIBarStyleBlack];
[self presentModalViewController:createAPictureNavController animated:YES];
[createAPictureNavController release];
[createAPictureViewController release];
}
-(void) loadAccountModalViewController
{
AccountViewController *accountViewController = [[AccountViewController alloc] initWithMode:UICaptionDistractionTableViewControllerModeModal];
UINavigationController *accountNavController = [[UINavigationController alloc] initWithRootViewController: accountViewController];
[[accountNavController navigationBar] setBarStyle:UIBarStyleBlack];
[self presentModalViewController: accountNavController animated:YES];
[accountNavController release];
[accountViewController release];
}
-(void) loadHelpModalViewController
{
HelpViewController *helpViewController = [[HelpViewController alloc] initWithMode:UICaptionDistractionTableViewControllerModeModal];
UINavigationController *helpNavController = [[UINavigationController alloc] initWithRootViewController: helpViewController];
[[helpNavController navigationBar] setBarStyle:UIBarStyleBlack];
[self presentModalViewController: helpNavController animated:YES];
[helpNavController release];
[helpViewController release];
}
-(void) exitModalViewController
{
[self dismissModalViewControllerAnimated:YES];
}
So for my app, on each viewcontroller it will have a toolbar at the bottom that have the basic buttons for creating a picture, accessing the account, or accessing help. If one of these buttons is accessed, it will launch a modal view which will have the home button to exit the modal view (when the UIMainViewController is created, one of it's parameters tells it which mode it is in, and thus which toolbar buttons to add.
But the main thing is I created a class mutablearray varialbe to store the toolbar buttons and then the buttons are created in "assembleToolbarButtons". Now any class that inherits from UIMainViewController can override the assembleToolbarButtons in order to add it's own buttons on top of the main ones that have already been added.
As far as what I mentioned initially in using one UIView and having it reorganize itself, and only one uiviewcontroller, I avoided this and instead just created separate view controllers for each step and separate views so as to adhere to MVC more.
In the app that io am creating, i have a custom copy of UISplitView Controller, MGSplitViewController. I have implemented it into my project which started off with the MultipleDetailViews sample code from apple.
I have come across a problem where i cant seem to switch between viewcontrollers. When i push the tableview cells, the detailview controller should change according the the nib assigned, however that doesn't happen.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
/*
Create and configure a new detail view controller appropriate for the selection.
*/
NSUInteger row = indexPath.row;
UIViewController *detailViewController = nil;
if (row == 0) {
FirstDetailViewController *newDetailViewController = [[FirstDetailViewController alloc] initWithNibName:#"FirstDetailView" bundle:nil];
detailViewController = newDetailViewController;
}
if (row == 1) {
SecondDetailViewController *newDetailViewController = [[SecondDetailViewController alloc] initWithNibName:#"SecondDetailView" bundle:nil];
detailViewController = newDetailViewController;
}
// Update the split view controller's view controllers array.
NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
splitViewController.viewControllers = viewControllers;
[viewControllers release];
[detailViewController release];
Normally this code would be enough to change the views in the original Multiple Detail View code.
has anyone run into a similar problem? any ideas?
You're just creating new view controllers. You're not adding them anywhere. You add view controllers to the split view controller by using its viewControllers property.
EDIT: I've used MGSplitViewController, but I never tried to change the detail view like that. I just pushed the new detail view controller onto the navigation controller. Is there a specific reason for wanting to change the detail view controller entirely?
I am working on a app, where I got a tabbar, in the 2nd tabitem, I got a UITableView, when the user presses the row on that UITableView it must navigate to another UITableView(2nd), once on the 2nd UITableView we can then navigate to detailview.
Can anyone help me out how to sort this out.
Help much appreciated.
- (void)viewDidLoad {
UITabBarController * t = [[UITabBarController alloc] init];
ResourcesViewController * c = [[ResourcesViewController alloc] init];
UINavigationController * n = [[UINavigationController alloc] initWithRootViewController:c];
[c release];
NSArray * tabs = [NSArray arrayWithObject:n];
[n release];
[t setViewControllers:tabs];
[window addSubview:[n view]];
//---initialize the array---
listOfResources = [[NSMutableArray alloc] init];
//---add items---
[listOfResources addObject:#"Speeches"];
[listOfResources addObject:#"Publications"];
[listOfResources addObject:#"Images/Photo Library"];
[super viewDidLoad];
}
This is ViewDidLoad method in the rootviewController and I created the next level UITableView called detailtableviewcontroller but can't call it from this method.
I think you might want the following view hierarcy (and corresponding controllers) to deal with this:
UITabBarController
UINavigationController
UITableViewController
UITableViewController
DetailViewController
AnotherViewControllerForTabBarButton
Your UITabBarController switches between views one indentation level below it, one of these views will be the UINavigationController which will provide the navigation between your various table views that you describe (1 and 2), as well as the detail view once you've drilled far enough down.
Hope that helps! :)
There are similar questions here:
How to : Navigation Controller in Tab Bar Controller
Tab Bar Application With Navigation Controller
So before I've managed to work with TabBarViewControllers and create an application using them. However every time I do so the view acts as my main view. This time around I want my TabBarView to be my second view in my application
E.g
First window has a bunch of buttons, when I click one of these buttons I want the second view to show up. This view includes a TabBarViewController.
The farthest I've gotten is to have the button show a view but for some reason it won't show my TabBar view!
Here's the code for my button
- (IBAction)showEvents:(id)sender {
EventsViewController *controller = [[EventsViewController alloc] initWithNibName:#"EventsView" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
}
Any of you guys able to help?
Can't you just in the EventsViewController add the following code in viewDidLoad:
UITabBarController *tbc = [[UITabBarController alloc] init];
tbc.viewControllers = [NSArray arrayWithObjects: vc1, vc2, ..., nil];
Anyway, I found a solution and it was actually quite simple. After creating the Outlet for the TabBarController and linking it together with File's Owner all I had to do was add
self.view = tabViewController.view;
On the viewDidLoad method