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];
}
}
Related
I'm a beginner in Objective C and I have an issue between my Table View Controller and ViewController. When I click on the cell nothing happens, I don't go in my view controller to have more details about my cell.
I use a push segue and I don't forget to give it an identifier and to do this code part in my TableView:
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showDetails"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
ViewController *destViewController = segue.destinationViewController;
destViewController.details = data [indexPath.row];
}
}
My link between my two views is between the cell prototype and the ViewController window.
in didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"showDetails" sender:self];
}
in prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showDetails"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
ViewController *destViewController = segue.destinationViewController;
destViewController.details = data [indexPath.row];
}
}
I am trying to set a dynamic NSString value each time a table cell is pressed in my app. I am able to pass the variable to my custom UINavigationController with the code below -
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *page = [menuItems objectAtIndex:indexPath.row];
[[segue destinationViewController] setGrabPage: page];
}
The problem is that grabPage only gets set in the NavigationController, and not the top view displayed within the navigation controller. How do I pass this value through to the top view?
If your destinationViewController is a UINavigationController, you should cast it into a UINavigationController and get the rootViewController of the navigation controller.
Cast it to the type of this first controller and you're good to go
EDIT: Added sample code
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *page = [menuItems objectAtIndex:indexPath.row];
UINavigationController *nav = (UINavigationController *)[segue destinationViewController];
MyViewController *vc = (MyViewController *)(nav.viewControllers[0]);
[vc setGrabPage: page];
}
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 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
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog((#"This is didSelectRowAtIndexPAth"));
DetailViewController *detailViewController = [[DetailViewController alloc] init];
detailViewController.myDictionary = [self.placeArray objectAtIndex:[indexPath row]]
// This MyDictionary NSdictionary is declared in other viewController which is populated here ..
NSLog(#"My Dictionary: This is a MasterView one %#",detailViewController.myDictionary);
UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
detailViewController = [mystoryboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
//The below NSlog is showing the content , its not null still in next viewcontroller same variable showing null content
NSLog(#"My Dictionary: This is a MasterView two %#",detailViewController.myDictionary);
[self performSegueWithIdentifier:#"showDetail" sender:self];
}
Use PrepareForSegue for this purpose
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
DetailViewController * detailViewController = (DetailViewController*)segue.destinationViewController;
detailViewController.myDictionary = [self.placeArray objectAtIndex:index];
}
From did select just use the prepareForSegue method(Hope you have already used.) Set the presenting view controller option via xib or programmatically and just stop there.prepareForSegue will call automatically while presenting the view while using storyboard.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
DetailViewController *detailViewController = [[DetailViewController alloc] init];
detailViewController.myDictionary = [self.placeArray objectAtIndex:[indexPath row]];
}
edit you did_select_method and try.