How to go from a view to another view controller (iphone) - iphone

I have a table view in a view controller (view based application), once a user selects one of the cells it should load another view.. I tried the navigation controller thing but that resulted in a huge failure along with 5-6 hours wasted (it crashes when I try to pushViewController onto the nav. controller). Is there anyway to move from one view controller to the other? Keeping in mind I need to send over the selected cell (a string)
Thanks
Edit:For those curious, I created another project as a navigation based application. Everything is working now.
Thanks all for the help.

You want to implement the UITableView delegate method didSelectRowAtIndex. Here's an example:
- (void)tableView:(UITableView *)aTableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailViewController = [[DetailViewController alloc] init];
[detailViewController setData:[tableViewData objectAtIndex:[indexPath row]]];
[[self navigationController] pushViewController:detailViewController animated:YES];
[detailViewController release];
}
In your DetailViewController.m file, once you're finished with it, simply use
[self dismissModalViewControllerAnimated:YES];
to pop it off the stack.

Related

didSelectRowAtIndexPath method does not load new View in iOS app code is inside

I have my first and normal view controller with a UITableView in it. The items that need to be displayed in those rows work perfectly.
Now comes the issue, if the user selects a row, I want a new view opened.
This view is called SecondViewController. so I have the .h, .m and .xib file named that way and it is of the UIViewController class.
I am using a CustomCell for the tableview could this be the issue?
I have #imported everything normally, and this is the code I am using in the method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:secondViewController animated:YES];
}
What on earth am I doing wrong? I have been looking at questions here for hours but nothing seems to be working.
Are you sure that self.navigationController is not nil?
Try to log it, From your code your problem is probably that, your self.navigationController is nil.
This class is pushed from another or are you starting the navigation in this class? If you are starting it in this class, this is your issue, you need to call the navigation that you created and not the one that is supposed to be with your viewcontroller.

iPhone: TableViewController nibName error with "initWithNibName:bundle:"

I have a dynamic cell UITableViewController and I want to push a specific View Controller when someone taps a cell. So I'm trying to use the pre-made method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
But when I go into the simulator and tap a cell, this is what I get:
"NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle [...] with name 'AffichageVotesQuestionDuMomentViewController'.
Within the method, I have this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
AffichageVotesQuestionDuMomentViewController *detailViewController = [[AffichageVotesQuestionDuMomentViewController alloc] initWithNibName:#"AffichageVotesQuestionDuMomentViewController" bundle:nil];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
}
AffichageVotesQuestionDuMomentViewController is a custom view controller I created myself. I also tried creating another random one and I get the same type of error. If I set the initWithNibName: to nil, it segues to a view controller that has the navigation bar on top, but that's completely black.
Now, I'm a beginning iPhone programmer and have a very poor understanding of nibs, nib names and such. Any help is appreciated. Also, I didn't do any segue in the storyboard or anything, since it seems I'm programmatically creating the view controller and pushing that way. If there's a good way to do it with segue though, I'm okay with that too (but I need to know the row of the tapped cell).
Thank you!
Because you're using IB storyboards you should do something lik this i think:
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"mystoryboard" bundle:nil];
UIViewController* vc = [sb instantiateViewControllerWithIdentifier:#"AffichageVotesQuestionDuMomentViewController"];
... instead of using the "initWithNibName"-initializer which is used for the classical xib-based approach.

iPhone app, navigationcontroller not functioning?

I have 4 tabs in a tab bar. In one of the tabs i want to use a navigation, i.e. when i click an item from the list it should go to some details page about it. I have the list page where i have the navigation bar and the list of items. I can scroll them, but when I click any of them the selection animation happens, console logs the true row value, it even prints log instructions from constructor of the Details page but I can not see the Details page showing up. (btw Xcode 3.2.6 with iOS 4.3)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *rowValue = [myStrings objectAtIndex:indexPath.row];
NSLog(rowValue);
//[Utility setStr:rowValue];
[self.myTable deselectRowAtIndexPath:indexPath animated:TRUE];
//DETAILS PAGE HERE!!!!
RestViewController * rest= [[RestViewController alloc] init];
rest.scoreLabel.text = rowValue;
[self.navigationController pushViewController:rest animated:TRUE];
[rest release];
}
Anybody having any idea? Thanks in advance!!!
You can only use pushViewController: if your view controller stack is being managed by a UINavigationController. If the viewController you are trying to push onto is not being managed by a Navigation Controller, nothing will happen.
It sounds like this is the case if your app. Be sure your View Controller hierarchy is set up this way:
You have a UITabBarController at the top level.
The tab you are working with should manage a UINavigationController
Your tableViewController should be set as the rootViewController of the navigation controller in #2

Search Display Controller does not show searchBar

I created a simple TableViewController using the template offered by xCode.
Then I open the xib file of the TableViewController with Interface Builder and I drag/add a uisearchdisplaycontroller on the top part of the tableView.
xCode automatically creates and link all the outlets.
I save the xib file and I run the app but the searchBar is not displayed!
What else should I do to make appear the searchBar?!?
THANK YOU VERY MUCH!!!
I was having this exact issue. I would drag the search bar, it would "snap" into place on the table as a header, but it would never show.
I found that when you are presenting the next View you need to initWithNibName:
Example:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewSubclass *dvc = [[UITableViewSubclass alloc] initWithNibName:#"UITableViewSubclass" bundle:nil];
[self.navigationController pushViewController:dvc animated:YES];
[dvc release];
}
This is also assuming you dragged the SearchDisplayController into the xib file. It will do all the necessary connections for you.
Hope that helps.

iPhone SDK: Pushed view controller does not appear

I want to add a detail view for one of the cells in my UITableView. I've created a new view controller SyncDetailViewController) and a NIB for the detail view. Didn't make many changes in the new class. Just added a label and outlet for it and connected nib with view controller.
Now I've added this code to my view controller (the one with UITableView, not the new one)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
SyncDetailViewController *dvController = [[SyncDetailViewController alloc]
initWithNibName:#"SyncDetailViewController" bundle:nil];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
}
The problem is that even if I select any row in the UITableView nothing happens. It doesn't produce any errors or warnings. I've added a breakpoint inside this method and it is reached so this code is executed.
Any ideas are much apprieciated.
"Nothing happens" sound so much like "sending a message to nil". Have you, by chance, checked that self.navigationController is not nil? Or that dvController is well initialized?