I'm building an iPhone app, with the following structure:
I have the MainViewController which consists of 2 views (like split screen).
The first view of them, has a button. On tap, a UItableView (ResultTableViewController) appears in the second (of the above) view:
-(IBAction)buttonTapped:(id)sender {
if ([(UIButton *)sender tag] == 0) {
ResultsTableViewController *childViewController = [[ResultsTableViewController alloc] init];
childViewController.tableView.delegate = self.results;
[self.results.view addSubview:childViewController.tableView];
}
}
So I have a UItableView as a sub-view of a UIView.
The problem is that pushViewController() in didSelectRowAtIndexPath() of ResultTableViewController does not work (self.navigationController is nil).
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailsViewController *detailView = [[DetailsViewController alloc] initWithNibName:#"DetailsViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:self.detailView animated:YES];
}
I have tried many of the solutions I found, but nothing works.
In my MainWindow.xib, I have only MainViewController added, is that the problem?
Thanks in advance!
You are adding the view of the child controller to your controller's view, not pushing the child controller onto your navigation stack. Due to that, your child controller's navigation controller will be nil, since it wasn't put into the navigation controller.
Is this what you're going for?
-(IBAction)buttonTapped:(id)sender {
if ([(UIButton *)sender tag] == 0) {
ResultsTableViewController *childViewController = [[ResultsTableViewController alloc] init];
childViewController.tableView.delegate = self.results;
[self.navigationController pushViewController:childViewController animated:YES];
}
}
[self.navigationController pushViewController:self.detailView animated:YES];
above line of code will push the detailview on navigationcontroller stack. Check whethere your tableviewcontroller is on that stack ?
Ok, I found it.
I had to declare my MainViewController as UINavigationControllerDelegate and create a secondary NavigationController in it. I push the viewController in my new navigationController and that's it.
Related
I'm using JTRevealSidebar V2 with UITableView on the left slide bar.
I don't know how to push to other ViewController by sending a message.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.sidebarDelegate) {
NSString *text = [self.leftList objectAtIndex:indexPath.row];
if ([text isEqual:#"Warenkorb"]) {
NSLog(#"Ist warenkorb");
// How to push/create/bring2top view of msCartViewController Identified by "Cart"?
// NSLog works
}
}
}
How can this be done?
In your rootviewcontroller, you would allocate a new UIViewController and then push it onto the UINavigation stack.
For example:
UIViewController *myViewController = [[UIViewController alloc] init];
myViewController.title = #"My First View";
myViewController.view.backgroundColor = [UIColor redColor];
//to push the UIView.
[self.navigationController pushViewController:myViewController animated:YES];
Remember to do this in your root view controller!
Hope this helps.
Maybe, your problem is how to get an instance from storyboard, as I see you call your msCartViewController "Cart" in storyboard.
Or Maybe you cannot get your navigation controller for pushing.
For first Problem
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
msCartViewController* vc=[storyboard instantiateViewControllerWithIdentifier:#"Cart"];
For Second Problem, you need to store your navigation controller pointer to someplace you can visit, like in a dictionary property in a singleton. just like
// at first make a singleton with a dictionary property. here I call it YourNavigationCenter
UINavigationController *navigationController = [YourNavigationCenter sharedCenter].navigationDictionary[#"yourNavigationName"];
[navigationController pushViewController:self animated:YES];
If these solution have not catch your point, please make a comment.
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.
The other day I asked about using a UINavigationController as a child of a UIViewController. I got that working via the answer. Now what I'm trying to do is push a controller onto the nav stack. When a table cell is touched, I do the following:
- (void) showSetup {
NSLog(#"Showing Setup");
SetupViewController *controller = [[SetupViewController alloc]initWithNibName:#"SetupViewController" bundle:nil];
self.setupViewController = controller;
self.setupViewController.title = #"Setup";
[self.navigationController pushViewController:self.setupViewController animated:YES];
[controller release];
}
I can see the log statement in my console, but the view never changes. Am I missing something?
Hmmm, well it's a bit tricky without knowing the details of your implementation -- I assumed that you implemented your navigation controller as in the linked article. Also although you give no details it sounds like you've added a table view controller somewhere along the line, so I made the UIViewController conform to the UITableView protocols to handle everything in one place:
#interface SOViewController : UIViewController<UITableViewDelegate,UITableViewDataSource > {
UINavigationController* navController;
}
- (IBAction) pushMe:(id)sender;
#end
I dropped a button on the SOViewController's view in IB and wired the pushMe: action to it. I also created another UIViewController-based class called JunkController and dropped a "Junk" label on it's view in IB -- that's all I did in IB. In the SOViewController's viewDidLoad:
navController = [[[UINavigationController alloc] init] retain];
navController.navigationBar.barStyle = UIBarStyleBlackOpaque;
navController.toolbarHidden = YES;
UITableViewController* tvController = [[UITableViewController alloc] init];
UITableView* tv = [[UITableView alloc] init];
tvController.tableView = tv;
tv.delegate = self;
tv.dataSource = self;
[navController setViewControllers:[NSArray arrayWithObject:tvController]];
In the pushMe: action implementation:
[self presentModalViewController:navController animated:YES];
Implemented the tableView delegate and datasource methods; for selection:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"row selected");
JunkController* junk = [[JunkController alloc] initWithNibName:#"junkcontroller" bundle:nil];
[navController pushViewController:junk animated:YES];
[junk release];
}
This should yield an app that surfaces a screen with a "Push me" button. When that button is pressed you should get an animated modal navigation-based table view -- mine had one row in it that contained a label "select me". Touching this row should animate the junk controller into view.
There is no need to make setupViewController a declared property in this view controller. Also, I could be mistaken but I thought "controller" was a reserved name in Cocoa, I'd change that name. So make sure you have registered with the UITableViewDelegate and use - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath to hook into and push your new view controller as follows:
SetupViewController *detailViewController = [[SetupViewController alloc] initWithNibName:#"SetupViewController" bundle:nil];
detailViewController.title = #"Setup";
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
Goodluck!
I've got UITableViewController and the body of the tableView:didSelectRowAtIndexPath: method is following:
if (indexPath.row == 0) {
MyViewController *myViewController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
[self.navigationController pushViewController:myViewController animated:NO];
[myViewController release];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
Unfortunately when I select the first row in the table view then my view in not displayed. When I debug I can see that it goes into the if statement, view controller is initialized and pushed onto the stack, but the view does not appear.
Any ideas?
Thanks!
Sounds like the MyViewController.xib is not wired up correctly. Make sure that "File Owner" is set to the correct class and that it's view property connects to the view.
-pushViewController:animated: loads the view of the viewController provided to it but if there is no view it doesn't complain.
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.