I'm currently trying to spawn a new instance of the same view - using the following code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
InventoryController *inventoryController = [[InventoryController alloc] initWithNibName:#"InventoryView" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self];
[navigationController pushViewController:inventoryController animated:YES];
[inventoryController release];
[navigationController release];
}
Problem is that it's not working...
I don't get any errors or anything - it just doesn't do anything.
Any ideas?
#PengOne has it right... you're creating a navigation controller and then releasing it, and there's nothing to prevent it from being deallocated. Additionally, you haven't added the nav controller's view to the window, and you haven't set the nav controller as the window's root view controller, so there's no way for the views controlled by controllers in this particular navigation stack to ever be seen.
Try this: Create a navigation-based project in Xcode. You don't need to add any code -- just create the project so that you can look at the code that's provided. You'll see that the app delegate has a retain property for storing the nav controller, and the nav controller is set as the window's root view controller.
If your current controller is already a part of UINavigationController hierarchy then you must not create a new navigation controller - use the existing one instead (note that every UIViewController has a reference to its parent UINavigationViewController if it exists):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
InventoryController *inventoryController = [[InventoryController alloc] initWithNibName:#"InventoryView" bundle:nil];
[self.navigationController pushViewController:inventoryController animated:YES];
[inventoryController release];
}
Related
I am having an app which has a UITableViewController which is my settings page. I am pushing the UITableViewController with a presentModalViewController using self.navigationController (using storyboard ID). however each time I try to see that page, it's showing exception. After reading few posts I tried implementing two methods
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [super tableView:tableView
cellForRowAtIndexPath:indexPath];
return cell;
}
**my .h File**
#interface Setting : UITableViewController<UITableViewDelegate,UITableViewDataSource>
I have done all UI settings in IB so I didn't change anything in above two implemented methods.
In mainviewcontroller from where I am pushing the view to UITableViewController, I am using the below code
Setting *nextController = [[self storyboard] instantiateViewControllerWithIdentifier:#"setting"];
[self presentModalViewController:nextController animated:YES];
Setting *dvc = [[Setting alloc] init];
[self.navigationController pushViewController:dvc animated:YES];
As I already set all UIs in IB why do I need to implement those methods? At least I can seen the view correctly.
It looks like you're trying to initialize the same viewController twice. You don't need to alloc] init] after you instantiateViewControllerWithIdentifier. At least, from my experience you don't. Try this:
Setting *nextController = [[self storyboard] instantiateViewControllerWithIdentifier:#"setting"];
[self.navigationController pushViewController:nextController animated:YES];
That will "push" the nextController with the storyBoardID of "setting" from the right into your existing NavigationController.
However, using my intuition, I believe you want to present a settings view modally, that has it's own NavigationController. In that case, try this code, which wraps the Settings ViewController into a NavigationController, and presents that whole thing modally, so you can navigate within settings:
Setting *nextController = [self.storyboard instantiateViewControllerWithIdentifier:#"setting"];
UINavigationController *navcont = [[UINavigationController alloc] initWithRootViewController:nextController];
navcont.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:navcont animated:YES completion:nil];
Alternatively, you could do all of this in the Storyboard itself. Select your settings view controller, and go to Editor Menu > Embed In... > Navigation Controller. Then make a segue from your button to the navigation controller that holds the settings controller. Set the segue to "Modal" and you're all done.
I have a ViewBased App. I added a UITableView on one of the UIViewControllers. It shows the data and I implemented all the delegate methods etc. My problem is when I want to show the detailView it just doesn't happen. My code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailViewController =[[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
NSLog(#"DidSelectRowAtIndexPath");
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
I see that I need a navigationController but I don't have one and I was unsucessful trying to add one programatically. I don't have one in my appDelegate either, so my question is do I need to add one to show the detail view? If yes, please give me a code sample how to do that.
If not, what other way is there?
I am new to iOS so I am a bit lost here.
Please help!
To add a navigation controller programmatically just for this detail view, you need to something like this:
UINavigationController * controller = [[UINavigationController alloc] initWithRootViewController:detailViewController];
[[detailViewController] release];
[self presentModalViewController: controller animated: YES];
If you want to use pushViewController, you need to already have a navigation controller surrounding the view you're starting with.
You need to add the Navigation Controller FIRST, then your master table becomes the root view controller of the nav controller, then when you tap a row in the table, you push another view controller onto the nav stack.
How does your master table get into the app in the first place? If you're using a nib, it's super easy to just change out the view controller for a nav controller with the old view controller added as a child of the nav controller.
You can create one programmatically by working within your app delegate's application:didFinishLaunchingWithOptions: method like so:
UITableViewController *tableViewController = [[[WhateverYourSubclassVCIsCalled alloc] init] autorelease];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tableViewController];
window.rootViewController = navController;
[window makeKeyAndVisible];
When I push a view onto my view controller
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AnotherViewController *anotherViewController =
[[AnotherViewController alloc] initWithNibName:#"AnotherView" bundle:nil];
[self.navigationController pushViewController:anotherViewController animated:YES];
}
the RIGHT BUTTON ITEM of the navigationController DISAPPEARS.
Is there a way to make the self.navigationController.rightBarButtonItem STAY THERE like in the "Notes" application that comes with the iPhone?
I don't believe you can without using undocumented classes. However, if you set the rightBarButtonItem property to an item that looks the same, there shouldn't be a big difference.
You can do that with
self.anotherViewController.navigationItem.rightBarButtonItem = self.parentViewController.navigationItem.rightBarButtonItem;
after you´ve pushed the ChildViewController
I have an application with multiple views. I can click on different buttons to select different views, one of which displays a table. When I select a cell on that row, I want to display a new view. I see a log line indicating that view was loaded, but the screen doesn't change.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *targetCustomCell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(#"in didSelectRowAtIndexPath");
ItemDetailsViewController *detailsController = [[ItemDetailsViewController alloc] initWithNibName:#"ItemDetailsView" bundle:nil];
[self.navigationController pushViewController:detailsController animated:YES];
[self.window addSubview:[detailsController view]];
[detailsController release];
detailsController = nil;
}
In the ItemDetailsViewController, I have
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"in ItemDetailsViewController.viewDidLoad ");
}
I see both log lines, but the screen doesn't change. Why isn't the view changing? Is there some other way to change the view?
What happens if you try to present your details controller using [self presentModalViewController: detailsController animated:YES] instead of the navigation controller's pushViewController? Also, adding the view to the window shouldn't be necessary. Pushing the view controller should be sufficient.
Try adding NSLog([self.navigationController description]) and see if self.navigationController is nil. (Might happen if your current controller isn't a sub-controller of a UINavigationController). Passing a message to a nil object just silently fails, so there's a good chance that's the problem.
Hope that helps!
EDIT: It's usually not necessary to subclass UINavigationController. Check out this page in Apple's docs: Apple Docs: Using Navigation Controllers It explains the setup pretty well. If you're creating your "root" view controller programatically in your application delegate, you'd want to do something like this:
// set up main view controller
DrawingBrowserController *controller = [[DrawingBrowserController alloc] init];
// create a navigation controller that "wraps" the controller
navigationController = [[UINavigationController alloc] initWithRootViewController: controller];
[controller release];
// Add the navigation controller's view to the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
Basically, you're "wrapping" your root view controller in a navigation controller. If you're using Interface Builder, you can also set things up there.
Did you make sure your navigation controller is the root view of your window?
You usually should do it like this in the applicationDidFinishLaunching method:
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
Additionally, you should remove [self.window addSubview:[detailsController view]];, as it is not necessary.
I have an UIViewController, this controller is contained in a navigationController.
I add an UITableViewController in this viewController. I would like to call a pushViewController method when I press on a cell of my tableView.
I tried this :
UITableViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
FirstView *myViewController = [[FirstView alloc] init];
[f myViewController];
}
UIViewController (FirstView)
-(void)pushIt
{
SecondView *sCont = [[SecondView alloc] initWithNibName:#"SecondView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:sCont animated:YES];
NSLog(#"didSelect"); // is printed
[sCont release];
sCont = nil;
}
But nothing happen. I put NSLog() to my pushIt method and I can see it. So I don't understand why I can't push it.
Any idea?
UIViewController has a property named navigationController that will return a UINavigationController if one exists for the view controller its called from.
Using this property, you can push view controllers onto the navigation stack from your table view's didSelectRowAtIndexPath: method.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SecondView *sCont = [[SecondView alloc] initWithNibName:#"SecondView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:sCont animated:YES];
[sCont release];
}
The reason your current code isn't working is probably because of the following:
You already have an instance of FirstViewController, as you said you have added a table view as its subview
You try and create a new instance of a FirstViewController when the user taps a cell, which isn't on the navigation stack, so trying to push a view controller onto the stack from there doesn't work, because the navigationController property returns nil.
You alloc and init myViewController but never push it to navigation or window or whatever else, then you push sCont to myViewController, that isn't present at window. First, try not using myViewController, next try to push myViewController to navigation before pushing sCont into it.