I am using tableView in a viewcontroller1 and implementing a didSelectRowAtIndexPath for moving to viewController2 with some data and displaying it on a tableView on viewController2...for now everything is ok, but then when I implement the code on viewController2:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath) {
// some code
}
After I selecting a row its selected and colored in blue but never stop's in the method didSelectRowAtIndexPath.
What can be the problem?
You should set not only the data source of the table view to your view controller, but the delegate also:
tableView.delegate = self;
You need to set the delegate of your tableView, either in your storyboard or XIB file, or programatically.
Related
I have a UIViewController that contains a UITableView. The table view contains a custom UITableViewCell. The custom cell was built in interface builder and has a nib. In my main storyboard, I dragged a segue from the custom table view cell to the destination view controller. I set up the bare bones essentials in prepareForSegue, set a break point, but it never gets called.
I'm not that accustomed to using a UITableView in a view controller. I usually use a UITableViewController, but requirements dictate using the table view in a view controller. My initial assumptions is that most methods of doing things would be nearly identical, but I'm finding that not to be the case.
I tried setting the segue from the view controller itself and using didSelectRowAtIndexPath, and though it worked, the transition to the destination view controller was jerky.
Can anyone suggest what I might be missing in order to cause the prepareForSegue method to fire?
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
GaugeViewController *destination = [segue destinationViewController];
[destination setGaugeID:#"1"];
}
Thanks!
You need to refer to the identity of the segue in the Storyboard, something like this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
GaugeViewController *destination = segue.destinationViewController;
if([segue.identifier isEqualToString:#"yourSegue"]) {
NSLog(#"prepareForSegue called");
[destination setGaugeID:#"1"];
}else {
// do something else
}
}
Also don't forget to set the Identifier in the Storyboard.
Remember that push segues are used with Navigation Controllers and a modal segue can be dragged from view controller to view controller.
I have this code to navigate from my tableview to another tableview. But it doesnt navigate to the right tableview. It seems that it creates another view that it navigates to and not the one on my storyboard that I want to go to.
Is this because I create an object?
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath
{
ValjKupongViewController *vk = [[ValjKupongViewController alloc]init];
[self.navigationController pushViewController:vk animated:YES];
}
How can I write this the right way so that I navigate to my view in storyboard that have the class ValjKupongViewController?
Very grateful for help!
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath
{
[self performSegueWithIdentifier: #"My Custom Segue" sender: self];
}
In your StoryBoard
Step 1 Ctrl and Drag the mouse from the
View Controller on the left to the View Controller on the right.
View Controller on the left = The View Controller you are transitioning FROM.
View Controller on the right = The View Controller you are transitioning TO.
Step 2 Select push as the type of Segue
Step 3 Now your View Controllers will have the Segue between them (as seen below)
Step 4 You need to name the Segue to be able to identify it.Click on the Segue, then go to the attributes inspector and set the Identifier. (In the example it is set to My Custom Segue.
Make sure this is the same as in the code above.
i.e. [self performSegueWithIdentifier: #"My Custom Segue" sender: self];
Is this because I create an object?
Yes you are allocating and initialising a ValjKupongViewController object each time didSelectRowAtIndexPath is called. With this line of code
ValjKupongViewController *vk = [[ValjKupongViewController alloc]init];
You then transition to it with this line
[self.navigationController pushViewController:vk animated:YES];
I'm trying to add a subview using storyboard. It's being displayed correctly, but there's no button (IBAction) in subview that works correctly, even with empty code, the app always crashes when clicked.
TableViewController.m
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Load SubView
SubviewViewController *subview = [self.storyboard instantiateViewControllerWithIdentifier:#"SubviewViewController"];
[self.view addSubview:subview.view];
}
...
SubviewViewController.m
- (IBAction)btnCloseSubview:(id)sender
{
// Crashes the app when clicked, even empty as it's.
}
You shouldn't just add another view controller's view to your view like that, it can get you into trouble. When I've tried that, it sometimes works and sometimes not. The proper way is to also add the controller as a child view controller of TableViewController:
SubviewViewController *subview = [self.storyboard instantiateViewControllerWithIdentifier:#"SubviewViewController"];
[self addChildViewController: subview];
[subview didMoveToParentViewController:self];
[self.view addSubview:subview.view];
You probably need to set the frame of subview's view as well (subview.view.frame = self.view.bounds if you want it to be the same size).
As an alternative, you could create a view, not a view controller, in a xib file, and add that as a subview of your view. In that case, you would set the file's owner of the xib to TableViewController, and put the button's method in TableViewController.
Look into UIContainerView, it handles sub-view controllers. You will need to handle your own communication between parent and child view controllers, but other than that it will work like you need it to for a sub-view, as long as your sub-view has the proper outlets and actions set up in the storyboard with it's view controller.
Have a look for more in-depth information on container views.
I am currently facing a problem that I wanted to segue back to the same view controller by click on a table view cell. For your information, my table view is generated programatically and each of the cells are using NIB files to control.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath (NSIndexPath *)indexPath
{
if( indexPath.row == 0 ){
NSLog(#"Clicked");
}
}
When I clicked on the cell at position number 0, the log does came out. So now I wanted to segue back to the same view controller instead to the others. From the storyboard, it is impossible for me the drag the segue it is because the view controller is only have a scroll view.
E.g: Inside View Controller A have 5 cells, when I clicked on the first cell, it will segue back to View Controller A and pass data.
Please advise ya if you need to see more of my codes. Thanks viewer!
If I understand you correctly, you want to select a row in your tableView and have it performSegueWithIdentifier back to this same/current UITableViewController?
To do this:
In storyboard, ctrl+drag from the exposed table view cell to the yellow view controller icon in the black bar below. This creates the segue to self view controller.
Click on the segue, and add an identifier; e.g. "segueToSelf"
In your didSelectRowAtIndexPath method, add the following:
[self performSegueWithIdentifier:#"segueToSelf" sender:self];
I was using initWithNibName to push the cells to detail views but I want to use single storyboard rather than having numerous nib files. The below code is supposed to push the view to subcategoriesViewController when the cell is clicked. the cells are dynamic from json data.
Its not giving any errors. Do you think its an issue with navigation control? Just to test, I added a button on this view and it works; when I click it, it does push to next view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dict = [rows objectAtIndex: indexPath.row];
subcategoriesViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"subcategoriesViewController"];
controller.CATNAME = [dict objectForKey:#"C_NAME"];
controller.CATNUMBER = [dict objectForKey:#"CAT_ID"];
[self.navigationController pushViewController:controller animated:YES];
// [controller release];
}
Okay I think I have found the cause; but don't know how to fix it.
The tableview in this controller is actually a subview of a viewController. I duplicated these files and created a new view which is a TableViewController. This code, when used in the TableViewController does the job, but it doesn't do it when used in a controller where the tableview is a subview of a ViewController.
I want the table to be a subview because I want to put an image above the table. So, I think the problem here is this line:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
It does not reference the table in the view. Can anyone tell me how to modify this so it would reference the table within the view?
If didSelectRow... is not being called then your view controller is probably not set as the delegate of your table view.
This is set up for you automatically when using a UITableViewController but you have to explicitly do it when using a standard view controller.
An answer to your problem is often that self.navigationController is in fact nil because there is no navigation controller for your view. You perhaps have to get a pointer to the navigation controller of your projet and push your view on it.