I have a table with 5 cells and if the user clicks my second cell for example, I want to show another table or another page.
Is using
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath`
the way ?
Here is an example of navigating to another view controller when the user clicks on the second cell (assuming you have a navigation controller):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row == 1){
MyViewController * vc = [[MyViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}
}
sample Link :http://www.tutorialspoint.com/ios/ios_ui_elements_tableview.htm
if (indexPath.row ==1)
{
//Second cell index path its move another class xib
classname *yourcontroller = [[classname alloc] initWithNibName:#"xib file name" bundle:nil];
yourcontroller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:helpcontroller animated:YES completion:nil];
}
If you are using a Storyboard, you may skip tableView:didSelectRowAtIndexPath: and use prepareForSegue:sender: instead:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"__SEGUE_IDENTIFIER__"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
SomeOtherViewController *vc = (SomeOtherViewController *)segue.destinationViewController;
// assign something to vc
}
}
Related
I am new to IOS Objective C Programming. I am trying a very simple app. My main page is standard View Controller where i have a simple UITableView. What i am trying to do is to make a different view controller to come up when user select different cell on the Table View. Currently i populate 3 simple lines. I have searched for many historical discussion but can't seem to work (from segue options to simple instantiate view options). I give a snip of my code and hope that anyone can point me what i am doing wrong here.
Storyboard
Ensure one are you embed with NavigationController or Not, if Not,
just do the following way
XCode menu --> Editor --> EmbedIn --> Navigation Controller, do like
you get the output of
Step-2
In here we can navigate with two types
Connection Through
Connection less
Connection Through
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *SegName = #"";
if (indexPath.row == 0)
{
SegName = #"firstSegumeName";
}
else if (indexPath.row == 1)
{
SegName = #"secondSegumeName";
}
else if (indexPath.row == 2)
{
SegName = #"thirdSegumeName";
}
[self performSegueWithIdentifier:SegName sender:self];
}
Connection Less
if use `storyboard ID` with connection less
for example
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *otherViewCon;
if (indexPath.row == 0)
{
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:#"yourfirstIdentifierName"];
}
else if (indexPath.row == 1)
{
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:#"yoursecondIdentifierName"];
}
else if (indexPath.row == 2)
{
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:#"yourthirdIdentifierName"];
}
[self.navigationController pushViewController:otherViewCon animated:YES];
}
Create an array with segue names in the order in which you want to refer to the view controllers from your tableview and then in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
you can call
[self performSegueWithIdentifier:[array valueAtIndex:indexPath.row sender:nil];
If its through XIB :
Your_ViewController *vc = [[Your_ViewController alloc] initWithNibName:#"Your_ViewController" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
If its through StoryBoard :
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main_storyboard" bundle:[NSBundle mainBundle]];
Your_ViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:#"storyboardID"];
[[self navigationController] pushViewController:vc animated:YES];
If its through Coding :
Your_ViewController *vc = [[Your_ViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
For Your questions :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Put above code here according to your requirement....
}
Try this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
firstViewController *u = [[ContactDetailsVC alloc]init];
u= [self.storyboard instantiateViewControllerWithIdentifier:#"firstView"];
[self.navigationController pushViewController:u animated:YES];
}
Replace last 2 lines of didSelectRowAtIndexPath with:
UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:myView];
[self presentViewController:navC animated:YES completion:nil];
I want to user click table row then after it user will go to other page. The page has Storyboard ID "Other View" and Restoration ID "Other View".
But I can't go to purposed view
This is my code :
- (UITableViewCell *)tableView:(UITableView *)tabel cellForRowAtIndexPath:(NSIndexPath *)indeksBaris
{
static NSString *simpleTableIdentifier = #"TampilanCell";
TabelBeritaCell *cell = (TabelBeritaCell *)[tabel dequeueReusableCellWithIdentifier:simpleTableIdentifier];
NSLog(#"nilai : %d", indeksBaris.row );
//detecting NIB of table view
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableView" owner:self options:nil];
cell = [nib objectAtIndex:0];
//Go To View Controller which have identifier "Other View"
UIViewController *otherViewCon;
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:#"Other View"];
[self.navigationController pushViewController:otherViewCon animated:YES];
cell.judulBerita.text = [listBerita objectAtIndex:indeksBaris.row];
return cell;
}
This code doesn't work :
UIViewController *otherViewCon;
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:#"Other View"];
[self.navigationController pushViewController:otherViewCon animated:YES];
UPDATED
I have insert new code. I use didSelectRowAtIndexPath method but it doesn't work :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *otherViewCon;
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:#"Other View"];
[self.navigationController pushViewController:otherViewCon animated:YES];
}
Try the method didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"ShowDetail" sender:tableView];
}
If you want to pass a variable or perform an action before it segues, do the following:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"ShowDetail"]) {
//Do something
Detail *detailController = (Detail*)segue.destinationViewController;
}
}
If you want to know how to name a segue, here is a great tutorial from apple docs: Naming Segues
Your code seems to be correct. just i feel problem in finding UIStoryboard object .replace following things may this work fine.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *otherViewCon;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"your_storyboard_name" bundle:nil];// like Main.storyboard for used "Main"
otherViewCon = [storyboard instantiateViewControllerWithIdentifier:#"Other View"];
[self.navigationController pushViewController:otherViewCon animated:YES];
}
You should write navigation code in
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
this method.
Or you can directly go to another view controller by drawing push segue in storyboard.No need to write code.
How to make a push segue when a UITableViewCell is selected
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * storyboardIdentifier = #"storyboardName";// for example "Main.storyboard" then you have to take only "Main"
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardIdentifier bundle: [NSBundle mainBundle]];
UIViewController * UIVC = [storyboard instantiateViewControllerWithIdentifier:#"secondVCStoryboardID"];
[self presentViewController:UIVC animated:YES completion:nil];
}
Try this its help you. didSelectRowAtIndexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexpath.row==0)//means you click row 0
{
UIViewController *otherView=[self.storyboard instantiateViewControllerWithIdentifier:#"otherview"];
[self presentViewController:otherView animated:YES completion:nil];
}
else
{
UIViewController *detailView=[self.storyboard instantiateViewControllerWithIdentifier:#"detailView"];
[self presentViewController:detailView animated:YES completion:nil];
}
}
in here showDataVC is a my new file name for second ViewController.
in a first created a object of new file and initialize.
and initialize showDataVC with object to use object in pushviewcontroller.
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
showDataVc *objshowDataVc = [self.storyboard instantiateViewControllerWithIdentifier:#"showDataVc"];
[self.navigationController pushViewController:objshowDataVc animated:YES];
}
Implement your code in didSelectRowAtIndexPath method. And please read the documentations before writing code n research before you put questions here.
The following code is in my menuViewController.m. Now I want to go to another view when touch on a specific cell. What should I do to go to contactViewController?(I am using storyboard)
storyboard image:
https://drive.google.com/file/d/0BwOYR2GMJ7l8U1lYYVFVTWVkOG8/edit?usp=sharing
code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row==2)
{
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Set the title of navigation bar by using the menu items
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController;
destViewController.title = [[menuItems objectAtIndex:indexPath.row] capitalizedString];
if ( [segue isKindOfClass: [SWRevealViewControllerSegue class]] ) {
SWRevealViewControllerSegue *swSegue = (SWRevealViewControllerSegue*) segue;
swSegue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc) {
UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
[navController setViewControllers: #[dvc] animated: NO ];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
};
}
I want to go to another view when touch on a specific cell.
For this, I'd do it this way:
Step 1:
Drag from TableViewController to ContactViewController:
Step 2:
Select segue and specify the Segue Identifier (Show attributes Inspector tab in the right side bar)
I have named the Segue Identifier as SegueTestID
I chose Push as my style but it seems you might need Modal
And the corresponding code (in your MenuViewController.m) should be something like:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 2) {
[self performSegueWithIdentifier:#"SegueTestID" sender:self];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//following lines needed only if you need to send some detail across to ContactViewController
if ([segue.identifier isEqualToString:#"SegueTestID"]) {
ContactViewController *destinationViewController = segue.destinationViewController;
destinationViewController.strTest = #"Check";
//where strTest is a variable in ContactViewController. i.e:
//"#property (nonatomic, strong) NSString *strTest;"
//declared in `ContactViewController.h`
}
//...
}
PS: It seems you have alot in your -prepareForSegue: already.
Obviously... you'll need to hook things up properly.
In Storyboard , Do this ( In your case its contactviewcontroller ) give the name identifier name to contactViewController whatever you want as shown in image for showRecipeDetail
and you can go to the contactviewcontroller
and then
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showRecipeDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
RecipeDetailViewController *destViewController = segue.destinationViewController;
destViewController.recipe = [recipes objectAtIndex:indexPath.row];
}
}
In above method it shows how to pass the data from current viewcontroller to destviewcontroller
where We simply set the property (i.e. recipeName) in the RecipeDetailViewController to pass the recipe name. Obviously, you can add other properties in the detail view controller to pass other recipe-related values. ( In your case it will be data you want to pass to contactviewcontroller)
When a segue is triggered, before the visual transition occurs, the storyboard runtime invokes prepareForSegue:sender: method of the current view controller. By implementing this method, we can pass any needed data to the view controller that is about to be displayed. Here, we’ll pass the selected recipe object to the detail view controller.
I am trying to go from a UITableView with prototype cells to a detailviewcontroller of the item I selected on.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"historyToDetail"])
{
BYFHistoryDetailViewController *controller = (BYFHistoryDetailViewController *)segue.destinationViewController;
controller.workOut = [[BYFWorkOut alloc] init];
controller.workOut=_selectRow;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BYFHistoryTableViewController *detailViewController =[[BYFHistoryTableViewController alloc] init];
NSArray *items = [[BYFworkOutStore sharedStore] allItems];
BYFWorkOut *selectedItem = items[indexPath.row];
_selectRow = selectedItem;
}
What is not happening is the transition from the table to detail I have a push segue from the prototype cell to the details.
What am I missing?
You are doing quite a lot wrong here. When using segue's you don't create an instance of the class. You simply call:
[self performSegueWithIdentifier:#"MySegue" sender:self];
This will use the segue you have defined in the storyboard. Where MySegue is the segue ID you created.
When you want to pass in data you use the callback
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
BYFHistoryDetailViewController *vc = (BYFHistoryDetailViewController *)[segue destinationViewController];
vc.workOut = selectedItem;
}
But using this callback will mean you will need to store selectedItem somewhere after you click the row so you can access it here.
EDIT
Your code seems a bit odd here also.
You set workout to a new object.
detailViewController.workOut = [[BYFWorkOut alloc]init];
Create another object from data.
NSArray *items = [[BYFworkOutStore sharedStore] allItems];
BYFWorkOut *selectedItem = items[indexPath.row];
And then assign the new object, overwriting the previous one.
//give detail view controller a pointer to the item object in row
detailViewController.workOut = selectedItem;
There is no need to have the first line of code at all
EDIT 2
If you only going to be using the one selected item at a time. you can do this in your UITableViewController class.
#implementation MyTableViewControllerClass
{
BYFWorkOut *_selectedItem;
}
inside didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *items = [[BYFworkOutStore sharedStore] allItems];
_selectedItem = items[indexPath.row];
}
EDIT 3
I've modified the code you posted here. You didn't add the first line of code i posted. Please look at this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"historyToDetail"])
{
BYFHistoryDetailViewController *controller = (BYFHistoryDetailViewController *)segue.destinationViewController;
controller.workOut = _selectRow;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *items = [[BYFworkOutStore sharedStore] allItems];
_selectRow = items[indexPath.row];
[self performSegueWithIdentifier:#"historyToDetail" sender:self];
}
You need to name your segue and call the method:
[self performSegueWithIdentifier:#"MySegue" sender:self];
So I have the didSelectRowAtIndexPath method into a UITableViewController.
The TableView goes to a UIViewController which is controlled by a NavigationController.
I want to send the indexPath.row to the class called by the NavigationController, is there any way to send a parameter to another class?
My app scheme is:
TabBarController ---> NavigationController ---> TableViewController ---> UIViewController
If you are using storyboard:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UITableViewCell *cell = sender;
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// Pass any objects to the view controller here, like...
[vc setIndexPath:indexPath];
}
}
else
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
YourViewController *vc = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
[vc setIndexPath:indexPath];
}
}