navigation from tableview to another view - iphone

I am working in a table view app using story board . I want to perform event on selection
of cell in table view . So when I select a cell element , a next view(detailview) should be
open for displaying further details .
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.dv = [[Detail alloc]initWithNibName:#"Detail" bundle:nil];
dv.dic = [self.arr1 objectAtIndex:indexPath.row];
[self.view addSubview:dv.view];
}

Instead try this:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.dv = [[Detail alloc]initWithNibName:#"Detail" bundle:nil];
dv.dic = [self.arr1 objectAtIndex:indexPath.row];
[self presentViewController:dv animated:YES completion:nil];
}
And if you want to dismiss use this code:
[self dismissViewControllerAnimated:YES completion:nil];

You should use a navigation controller and when selecting a row, push the detail view into it.
To create navigation controller: selected the tableview controller and go Editor / Embed in / Navigation Controller.
Then do:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// dv = [Detail alloc] initWithNibName:#"Detail" bundle:nil];
dv = [storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
dv.dic = [self.arr1 objectAtIndex:indexPath.row];
[self.navigationController pushViewController:dv];
}
The commented line is if you're not using storyboard.
Or you can do as Abdullah suggested, but implement a dismiss button with
[self dismissViewControllerAnimated:YES completion:nil];

In the storyboard drag a segue from the viewcontroller icon at the bottom of your view to the next viewcontroller you want to segue to. Make sure that your view is embedded in a navigation controller. Press the arrow that appears between your tableviewcontroller and the target view controller and make sure you label it with a segue identifier. Then in the method you are describing make sure they selected the correct index path with an if statement and write
[self performSegueWithIdentifier:#"theNameOfYourIdentifierFromTheStoryboard"];
That being said, I think that there is a large body of information you should research.
Read this: http://www.appcoda.com/use-storyboards-to-build-navigation-controller-and-table-view/ as a start.

Related

Split-View application, how to change header for popover?

I've got a split-view application. When in Portrait orientation, there is a popover, it has a title "Root View Controller", how can I change it? moreover, how to skip popover when I select a cell? thanks. Here is the screenshot:
in the ViewController set
self.navigationItem.title = #"The text you want";
and to "skip" the popover when a row is selected do sometime like this
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.navigationController dismissModalViewControllerAnimated:YES];
}
Find the view controller that will be the master view in the UISplitViewController and set the title in the viewDidLoad method like this:
self.title = #"Set Title";
Since the master view will eventually modify the detail view you can place a method in the detail view controller to dismiss the UIPopoverController after you select a row. Below is an example.
So in DetailViewController(Detail View) implement a method like this
- (void)setDetailDescription:(NSString *)text {
// Put code to update the detail view here
[self.popoverController dismissPopoverAnimated:YES];
}
And then in your RootViewController(Master View) implement this code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *dc = [self.splitViewController.viewControllers objectAtIndex:1];
[dc setDescription:#"Update detail view"];
}
Take note that your setup will vary so you will have to adapt this code to your project.

How can I move to another view controller when the user clicks on a row?

I am new to iPhone development, and I want to move to another page when the user clicks on a particular row. So, if they click on first row, I want the page to redirect to the first view controller, and so on. Each row has its own view controller.
first of all you don't need to set different view controller for each row (unless you have a very good reason for doing that).
the correct way is to set 1 view controller that will fit all the cells in the raws and change its data according to the selected row.
the way you do that is:
in the - DidSelectRowAtIndexPath function in your implementation file you shuld:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//allocate your view controller
DetailedViewController *detailedViewController = [[DetailedViewController alloc] init];
//send properties to your view controller
detailedViewController.property1 = someProperty1;
detailedViewController.property2 = someProperty2;
//push it to the navigationController
[[self navigationController] pushViewController:detailedViewController animated:YES];
[detailedViewController release];
}
i do recommend that you will start by using apple examples, they are great and there are a lot of them :
http://developer.apple.com/library/ios/#samplecode/TableViewSuite/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40007318
Good luck
#kashyap in didSelectRowAtIndexPath delegate of the UITableView you have to check the conditions that on which indexPath you are clicking and open the viewControllers you had made accordingly
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here -- for example, create and push another view controller.
FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
switch (indexPath.row) {
case 0:
[self.navigationController pushViewController:firstViewController animated:YES];
[firstViewController release];
break;
case 1:
[self.navigationController pushViewController:secondViewController animated:YES];
[secondViewController release];
break;
}//Likewise do for the no of rows you have in your `UITableView`
Hope you get my point.....Good Luck!
Dig into the docs:
http://developer.apple.com/library/ios/#DOCUMENTATION/UserExperience/Conceptual/TableView_iPhone/ManageSelections/ManageSelections.html#//apple_ref/doc/uid/TP40007451-CH9-SW6
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.row) {
case "X"
// Allocate the viewController
[navigationcontroller.pushViewController:"yourviewcontroller" animated:YES];
[release "yourviewcontroller"];
break;
default:
break;
}
}
EDIT: Didn't realize of what you're trying. As shani said, you should not create a viewcontroller for each row if there is no need to. Try changing the datasource for each row but using the same viewcontroller. So forget this implementation.
Easy:
Create and push your view controller in this delegate function:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
It sounds like using a UINavigationController is what you want.
You then push your "first view" on to the navigation controller's stack from the table view, e.g., from you UITableView delegate:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
// indexPath.row == 0
FirstViewController* firstVC = [[FirstViewController alloc] initWithNibName...];
[self.navigationController firstVC animated:YES];
[firstVC release];
...
}
This was the one the things that I got problem when I first start. Here is my code that I use for this.
In subviewcontroller I have my own constructor like
-(id)initWithName:(NSString*)naMe
{
name=naMe;
return self;
}
And in didSelectRowAtIndexPath Delegate method Of UITableView. I alloc it
MyListView mylistview=[[MyListView alloc] initWithName:#"name"];
[self.navigationController pushViewController:mylistview animated:YES];
[mylistview release];
NSInteger row = indexPath.row;
UIViewController *newFrontController = nil;
if (row == 0)
{
YourViewController *yourViewController = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
newFrontController = [[UINavigationController alloc] initWithRootViewController:peopleViewController];
[self.navigationController setNavigationBarHidden:YES];
} else if(row == 1){
//do your code
}
do this in didSelectRowAtIndexPath in tableView.
You can move to another page when user tap on row by simply making segue by "Control Drag" from cell to ViewController and make segue type "Push".

How to open a new UITableView when clicked on a table cell and add navigation controller dynamically

Basically I add a subview to the main window. This subview has a search bar and a table view. The search bar works and it displays some data on the table view cells. Now when I click on a cell it should load another view with a navigation bar (w/ back button) and a table inside that view. How can this be done? I tried the following code and it doesn't work, it gives me an error saying - "'-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "DetailViewController" nib but the view outlet was not set.'
I tried to set the outlet's in the DetailViewController.nib file but it just wouldn't let me.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[UITableViewController alloc]initWithNibName:#"DetailViewController" bundle:nil]];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
}
Basically you push the detail view controller onto the navigation controller.
I published a sample code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailContactViewController *detailViewController = [[DetailContactViewController alloc] initWithNibName:#"DetailContactView" bundle:nil];
detailViewController.contact = [contacts objectAtIndex:indexPath.row];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}

iphone : SDK disclosure button not response from first TableView to second detail TableView?

How can I pass the first tableview row data to second detail view row
I use
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
or
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
to show up disclosure button in each cells
it can ,I also add actions in
- (void)tableView:(UITableView *)tableViewaccessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
NSLog(#"disclosure button %# touched",indexPath.row);
}
But when I touch the disclosure button I got nothing shows up in console
Than I add actions in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// carry Alarm Name text into sub-table-view to look for detail time and text info
NSLog(#"Alarm Name = %#", [alarmName objectAtIndex:indexPath.row]);
NSLog(#"Index Path Raw# = %i", indexPath.row);
// Navigation logic may go here. Create and push another view controller.
AlarmDetailTableViewController *detailViewController = [[AlarmDetailTableViewController alloc] initWithNibName:#"AlarmTableViewController" bundle:[NSBundle mainBundle]];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
detailViewController = nil;
}
It can notified on console mode
But I think it always appear the same detail view when I touch any rows in First Level TableView
How to show a new detail view with data from Level one ?
for example
To make the tableView1.row0.text = detail view.title
and show other data under the title
if you need the code I can upload it
my boss said it's not big secret inside...
Thanks a lot
Perhaps the problem is with your:
- (void)tableView:(UITableView *)tableViewaccessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
which should be reading:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
Ok,if you tapped on accessory button than show another detail view
we need to add a detail view
First create a "new navigation based application",ex FirstView
right click on the class folder and choice "add new File" choice a new view you wanna display
ex I selected a new tableview named "DetailView"
than first go to the DetailView.m go give a section and row numbers
or declare the layout
than back to FirstView.h
add #import DetailView
go to FirstView.m
find this method - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
and use DetailView sub class to create a new view
add the codes like this
DetailView *detailView =[[DetailView alloc]init];
detailView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:detailView animated:YES];
you can add any view you want to display

Change to another view from tableview with a navigationcontroller placed in a tabbarcontroller

I recently found a good tutorial about how to place a navigation controller within a tabbarcontroller("The nib way").
http://twilloapp.blogspot.com/2009/05/how-to-embed-navigation-controller.html
I continued with the second step and added a tableviewcontroller to the navcontroller.
What I don't understand, is how I can use the navigationbarcontroller from within my tableviewcontroller and e.g - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
What I want to do is when a user selects a row another view should slide in with the back button and everything that a navigationcontroller provides.
Thanks in advance!
Have you started with Apple's SimpleDrillDown sample? The specific code in question is this routine:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
/*
Create the detail view controller and set its inspected item to the currently-selected item
*/
DetailViewController *detailViewController = [[DetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
detailViewController.detailItem = [dataController objectInListAtIndex:indexPath.row];
// Push the detail view controller
[[self navigationController] pushViewController:detailViewController animated:YES];
[detailViewController release];
}
The following was what I was looking for:
[[self navigationController] pushViewController:detailViewController animated:YES];
[detailViewController release];
Since I've specified the RootView Controller for my NavigationControllers View, self answers to navigationController.