I'm new in iOS programming, and I want to do a simple thing. I saw several topics about my problem, but I don't understand why my code doesn't work...
I created a UIViewController subclass called details2ViewController, with a .xib
In my main view called ViewController.xib, I have a tableView
In ViewController.m, I added on the top : #import "details2ViewController.h"
In ViewController.m, I modified the didSelectRowAtIndexPath method like this :
details *det = [[details alloc] init];
[self.navigationController pushViewController:det animated:YES];
There is no warning, but no effect when I click on a cell... I precise that I'm working without mainStoryBoard.
note : Here's my previously post about this problem.
(Sorry if my english is awkward, I'm french... Thanks for your help !)
First of all check that you have properly wired your TableView's delegate to your ViewController's File Owner or not. TableView's -didSelectRowAtIndexPath method is a delegate method.
Secondly, I am not getting why are using details as a Class name when you already have imported details2ViewController.h. So, It looks like you should use details2ViewController instead details and your code should look like this :
details2ViewController *det = [[details2ViewController alloc] initWithNibName:#"details2ViewController" bundle:nil];
[self.navigationController pushViewController:det animated:YES];
Make sure that your ViewController is indeed embedded in a UINavigationController. If not then your self.navigationController will be nil.
Or you can go through An Introduction To UINavigationController.
if you are using Storyboard you can use this code.
- (void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
YourViewController *yourViewController = [storyboard instantiateViewControllerWithIdentifier:#"StoryBoardID"];
[self.navigationController pushViewController:yourViewController animated:YES];
}
Don't forget to set animated:BOOL to YES in last line.
You have to first define a property of navigation controller in your AppDelegate.h:
#property (strong, nonatomic) UINavigationController *controller;
Then in the AppDelegate.m file do this in the didFinishLaunchingWithOptions: method :
details2ViewController *controller=[[details2ViewController alloc] initWithNibName:#"details2ViewController" bundle:nil];
self.controller = [[UINavigationController alloc] initWithRootViewController:controller];
}
self.window.rootViewController=self.controller;
[self.window makeKeyAndVisible];
return YES;
Then in the details2ViewController.m in the didSelectRowAtIndexPath:
details2ViewController *det = [[details2ViewController alloc] initWithNibName:#"details2ViewController" bundle:nil];
[self.navigationController pushViewController:det animated:YES];
Do let me know if it work.. :) Thanks.
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 have a primarly code based iphone sample app that works. It is Navigation based app. This sample app code comes with a RootViewController (.h and .m). I am trying to import another
#import "TopView.h"
...
#pragma mark UITableViewDelegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *viewController;
TopView *TopViewController;
switch (indexPath.row) {
case PDF:
viewController = [[[PDFExampleViewController alloc] init] autorelease];
[self.navigationController pushViewController:viewController animated:YES];
break;
case TopView:
tableviewController = [[[TopView alloc] init] autorelease];
[self.navigationController pushViewController:viewController animated:YES];
break;
default:
viewController = [[[UIViewController alloc] init] autorelease];
[self.navigationController pushViewController:viewController animated:YES];
}
}
I am getting SigAlerts. I am trying to import the following simple TableView Xib based sample code.
http://www.icodeblog.com/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/
Above UITableView based one works by itself. My head spinning trying to debug the SigAlerts and setter errors. Any help on importing above UITableView into the main app would be greatly appreciated.
What is TopView? Is this a UIViewController? If so please change the name as it is misleading. From the name it looks like a UIView subclass
If this is not a UIViewcontroller, you cannot push it like the following. A UIView cannot be pushed on to a UINAvigationController.
case TopView:
tableviewController = [[[TopView alloc] init] autorelease];
[self.navigationController pushViewController:viewController animated:YES];
Also what are the values of these which you are using in the Switch-Case
PDF
TopView
In addition there is a typo?
tableviewController //supposed to be topViewController??
Another one, from next time onwards name your instance variables starting with small letter
i.e topViewController and NOT TopViewController
I have a class called CataloguesEtTarifsPDFViewController which displays a tableView.
Now, inside this class I wanna create a UINavigationController and set the root of the UINavigationController this class:
Here is what I did:
CataloguesEtTarifsPDFViewController.h
UINavigationController *navigationController;
in the implementation file
CataloguesEtTarifsPDFViewController.m
- (void)viewDidLoad
{
CataloguesEtTarifsPDFViewController *catalog =[[CataloguesEtTarifsPDFViewController alloc] init];
loadingView.hidden=YES;
navigationController = [[UINavigationController alloc] initWithRootViewController:catalog];
}
When I click on the table cell I do this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
WebViewController *webViewController = [[WebViewController alloc] init];
[self.navigationController pushViewController:webViewController animated:YES];
[navigationController release];
}
But when I run and click on the tableView nothing happens...not view is showed up!!!Where am I going wrong?
IMPORTANT: I don't have a delegate file.Only CataloguesEtTarifsPDFViewController.h CataloguesEtTarifsPDFViewController.m and CataloguesEtTarifsPDFViewController.xib.
EDIT:
- (IBAction)showViewCataloguesEtTarifsPDF:(id)sender{
// Remove view to middleView
[mainMenuViewController removeViewsToMiddleView];
// create view controller
cataloguesEtTarifsPDFViewController = [[CataloguesEtTarifsPDFViewController alloc] init];
cataloguesEtTarifsPDFViewController.mainMenuViewController = mainMenuViewController;
// hide/show header button
[mainMenuViewController.headerViewController showMainMenuButton];
// highlight footer tabbar button
[mainMenuViewController.footerViewController.footerTabBar setSelectedItem:mainMenuViewController.footerViewController.footerTabBarItemMyAudi];
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:cataloguesEtTarifsPDFViewController];
[self presentModalViewController:navigationController animated:YES];
}
You should probably read this: http://www.iosdevnotes.com/2011/03/uinavigationcontroller-tutorial/
Or this: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007457-CH1-SW1
Or watch this: http://peepcode.com/products/iphone-view-controllers-part-i
That'll give you the basics and you'll be well equipped to answer your question and fix your code :)
PS: You don't have to explicitly create and set a UINavigationController in your view controller. If the view controller is embedded within a navigation controller, the navigationController property of your view controller instance will automatically find it and return it.
What you're doing in viewDidLoad is wrong.
Wherever you are showing the CataloguesEtTarifsPDFViewController, you should wrap it in a UINavigationController there and show the UINavigationController instead.
This will make sure the navigationController property of UIViewController will be set.
In your tableView:didSelectRowAtIndexPath: method you should release the webViewController variable instead of the navigationController property.
I'm trying to use a UINavigationController but I'm uncertain how. Up till now (for about a year), I've been using presentModalViewController and dismissModalViewController to present/dismiss view controllers.
So, this is what I did. My main view controller (the first one that shows on launch) is called MainViewController, and it extends UIViewController.
So I made this launch function in my app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MainViewController *controller = [[MainViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
And in my MainViewController's viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Title";
self.navigationController.navigationBar.tintColor = [Constants barColor];
....more code...
}
But, in my MainViewController, I'd like to present another view controller called SecondViewController, which needs a UINavigationBar with a back arrow button. So do I make SecondViewController extend UIViewController and do the same thing by setting the title and backButton in the viewDidLoad method? And how do I present it? What should I do to accomplish this?
You'll need to set a root view controller up, it's easiest starting from the apple template.
Here's where the magic happens:
UIViewController *controller = [[UIViewController alloc] initWithNibName:#"MyNib" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
The nav controller does all the work for you (back buttons, titles, animations) - it keeps track!
My workflow is this:
Setup MutableArray in the viewDidLoad, add controllers to it, e.g:
NSMutableArray *array = [[NSMutableArray alloc] init];
MyCustomViewController *customView = [[MyCustomViewController alloc] initWithNibName:#"nib" bundle:#"nil"];
customView.title = #"Second Level";
[array addObject:customView];
self.controllers = array;
Then in your delegate:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
UIViewController *childControllerToBe = [controllers objectAtIndex:row];
[self.navigationController pushViewController:childControllerToBe animated:YES];
}
This, along with a lot more can be learnt by reading a decent beginner book such as Beginning iPhone Development
Also, apple docs are always good :)
UINavigationController is a subclass of UIViewController, but unlike UIViewController it’s not usually meant for you to subclass. This is because navigation controller itself is rarely customized beyond the visuals of the nav bar. An instance of UINavigationController can be created either in code or in an XIB file with relative ease.
Please visit "How to add UINavigationController Programmatically"
You should Push it onto the navigation stack.
This Lecture by Stanford's iPhone Course will teach you a lot about Navigation Bars. (It's a quick read)
Basically at the heart of it you need this code:
[self.navigationController pushViewController:SecondView];
You can use PopViewController to go back programmatically, but the Back Button is automatically created.
Here's some source code from the Lecture. It covers exactly what you are having issues with.
I am having problems with getting a detail view to load using pushViewController. At first, I thought pushViewController was not working. BUT, then I tried using a different view controller and it worked. I can tell from tracing that the problem view controller is never loaded at all. In other words, I tried to eliminate the possibility that there was some error in the view controller by NSLoging in that object and I never see anything.
Does anyone have any ideas?
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
/*
NSLog(#"hsitkjsdfkljlkd");
if (childController == nil)
childController = [[salesViewController alloc] initWithNibName:#"salesView" bundle:nil];
//NSUInteger row = [indexPath row];
[self.navigationController pushViewController:childController
animated:YES];
*/
/*
//modal = blocking
salesViewController *otherVC = [[salesViewController alloc] initWithNibName:#"salesView" bundle:nil];
//must set sale type
otherVC.strSaleType = #"Voice";
[self presentModalViewController: otherVC animated:YES];
//No close. By design, it is up to the otherVC to close itself
*/
//tipCalcViewController *detailViewController = [[tipCalcViewController alloc] initWithNibName:#"tipCalcView" bundle:nil];
salesViewController *detailViewController = [[salesViewController alloc] initWithNibName:#"salesView" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
Just Check the
- (void)viewWillAppear:(BOOL)animated
{
}
of the salesViewController.
you are doing something wrong in this..
put the debugging point in the viewWillAppear and run it. you can get the error line..
just try it......Surely it will work for u...
salesViewController *detailViewController = [[salesViewController alloc] initWithNibName:#"salesViewController" bundle:nil];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
Also make sure you are giving the IBOutlet connection to UIView.
In my case I had several IBOutlets that I removed from the Header file and forgot to remove the connection to these non-existing outlets in Interface Builder. So removing the obsolete outlets fixed the problem in my case.
When you're pushing from ViewController1 to ViewController2 then the code will be used like this so try this code,
ViewController2 *vw2=[[ViewController2 alloc]initWithNibName:#"ViewController2" bundle:nil];
[self.navigationController pushViewController:vw2 animated:YES];
The Above code may be written on Click event of button or on didSelect delegate of UITableView