I'm creating a table view application.
I have to display a web view when a cell in the table view is touched.
But the tableView's navigation controller is showing a nil value.
And it is not pushing to the detailsViewController.
Anyone please help me to resolve this issue.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NewsDetailsViewController *detailsVc=[[NewsDetailsViewController alloc]initWithNibName:#"NewsDetailsViewController" bundle:nil];
NSString *launchURL = [nf.newsStories [indexPath.row] objectForKey:#"link"];
detailsVc.url = launchURL;
NSLog(#"View controllers before pushing : %#",self.navigationController.viewControllers);
[self.navigationController pushViewController:detailsVc animated:YES];
NSLog(#"View controllers after pushing : %#",self.navigationController.viewControllers);
}
NavigationController Value in debugger
I guess the problem is with creating your controller which this line
NewsDetailsViewController *detailsVc=[[NewsDetailsViewController alloc]initWithNibName:#"NewsDetailsViewController" bundle:nil];
Try to replace it with this one
NewsDetailsViewController *detailsVc= [[NSBundle mainBundle] loadNibNamed:#"NewsDetailsViewController"
owner:self
options:nil][0];
Anyway I advise you to move to using Storyboards and Segues
Related
I have a Storyboard which loads a XIB with a TableView inside.
When the user selects a row from the TableView a new XIB should be loaded with the following method pushViewController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
[tableView deselectRowAtIndexPath:indexPath animated:YES];
RecipeDetailViewController *recipeDetailViewController = [[RecipeDetailViewController alloc] init];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
recipeDetailViewController.navTitle = str;
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:recipeDetailViewController animated:YES];
}
The loaded new XIB should show the following View (with a button inside):
Instead it loads a black view like below:
I checked whether the view is connected to the viewcontroller. From my point of view everything is fine. In IB it the view is connected the File's Owner:
I solved it. All I had to do is to close XCode and reopen the project. After that all worked just fine!
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.
I'm working on a navigation-based application.
My rootViewController contains 3 cells
-When the first one is pressed a UIViewController is pushed (THIS WORKS)
-The problem is with the 2nd and 3rd cells that are supposed to push a UITableViewController
The app is running with no errors and it is NOT crashing at all, but when i navigate to the tableview, an empty table is viewed with no elements in it.
Is the problem with this part of the code?? :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *detailViewController = [[UIViewController alloc] initWithNibName:#"Introduction" bundle:nil];
detailViewController.title=#"Introduction";
UITableViewController *myPictures = [[UITableViewController alloc] initWithNibName:#"Pictures" bundle:nil];
myPictures.title=#"Pictures";
UITableViewController *myImages = [[UITableViewController alloc] initWithNibName:#"ImagesViewController" bundle:nil];
myImages.title=#"Images";
// Pass the selected object to the new view controller.
if (0 == indexPath.row)
[self.navigationController pushViewController:detailViewController animated:YES];
if (1== indexPath.row)
[self.navigationController pushViewController:myPictures animated:YES];
if (2== indexPath.row)
[self.navigationController pushViewController:myImages animated:YES];
[detailViewController release];
[myPictures release];
[myImages release];
What you're doing it very wrong (apart from your original problem). Why are you instantiating each view controller and then only using the one based on the current 'cell selection'? This will slow your app down depending on how much time it would take to load each of these separate views. You should only instantiate the relevant view controller inside the "if (indexPath.row == 2) {" block.
Apart from that there are many things wrong about your approach. What you are doing will never work since instantiating a generic UITableViewController (even if you supply your own nib) will obviously only show you an empty view. You need to have a class of your own which the nib is tied to as a delegate which will then supply the TableView with data.
I believe when you created these nib files (for example "Pictures") xcode also give you a 'PicturesViewController.h and PicturesViewController.m" files? If so, you need to write the appropriate code there and ensure the Tableview in the "Pictures" nib file has its 'datasource' and 'delegate' set to 'PicturesViewController'. Then when you want to show that view do this instead:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 1) {
...
} else if (indexPath.row == 2) {
PicturesViewController *myPictures = [[PicturesViewController alloc] initWithNibName:#"Pictures" bundle:nil];
[self.navigationController pushViewController:myPictures animated:YES];
[myPictures release];
}
}
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];
}
I am doing a book kinda application using a viewBased application. The mainView acts as the backgroundView for loading pages. Each page is a different view and have its own nib. When i swipe on a page(actually on the mainView which have loaded the current view/page), that page will be removed and the next/previous page will be added.
[currentPage.view removeFromSuperview];
[self.view insertSubview:nextPage.view atIndex:0];
I have added a popoverController with a barbutton in this mainView. Its intialized with a tableviewController class named popClass. PopClass is another UITableViewController class which will act as the parent for the popViewController.
self.myPopController = [[PopController alloc] initWithStyle:UITableViewStylePlain];
self.myPopOverController = [[UIPopoverController alloc] initWithContentViewController:myPopController];
When I select certain row in the popover, I want the backgroundview(mainView) to load the page corresponding to the number of the row.
For that I have written a function in bookView(mainView) Controller class. And is calling it from the popOver's parent class when i select certain row.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger toPageNumber = indexPath.row + 1;
[viewController changeToPage:toPageNumber];
}
Function and the lines of code are executing but nothing is happening. Even the log lines are printed but no action takes place. The code is the same for removing a page and adding another page which is working successfully when i swipe the view/page.
What is the problem? OR is there anyother way to make the view change in mainView by using the popover?
You need to have refernce of the background view controller in the popover controller.
In the PopController controller you should have a delagate like:
#interface PopController: UITablViewController{
//// other varibales;
id delegate;
}
#property (nonatomic, retain) id delgate;
Now from the class in which you are showing the popovercontroller
you should add this line:
[myPopController setDelegate:self];
Then you can easily acces any method of the main view controller class. By using
[delegate callTheRequiredMethod];
Hope this helps.
Thanks,
Madhup
It's hard to tell what's going wrong without looking at the code. That said, here's what I'd do:
(1) When a row is selected in mainView, present the popoverController modally:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
PopoverViewController *vc = [[PopoverViewController alloc] init];
[self.navigationController presentModalViewController:vc animated:YES];
[vc release];
}
(2) When something changes in the popoverController, for example a row is selected, set a value in the parentViewController (which should be the MainView):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.parentViewController.value = someValue;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
(3) Dismiss the popoverController where-ever you choose by calling:
[self dismissModalViewControllerAnimated:YES]`