iPhone creating nested views - iphone

I'm noob in iPhone programming.
I want to create navigation-based application with several nested views, when first and second are UITableView-type, and the third is simple UIView with my own interface, which I built in Interface builder.
When I run the App, touch the row on the first table-view I transfer to the second, and when I touch the row on the second, I transfer to the third, but on the third View I don't see my interface, which I built in Interface Builder, just blank screen.
This is part of my code, where I try to call my third view:
if(self.reportView == nil){
ReportTypeViewController *viewController =
[[ReportTypeViewController alloc] initWithNibName:#"ReportTypeViewController"
bundle:[NSBundle mainBundle]];
self.reportView = viewController;
[viewController release];
}
[self.navigationController pushViewController:self.reportView animated:YES];
self.reportView.title = #"Reports";
That's OK, guys.
I've just didn't add text to my button, that lies on my view, that's why it was blank.

You don't need [NSBundle mainBundle];
it should look like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ReportViewController *vc = [[ReportViewController alloc] initWithNibName:ReportViewController bundle:nil];
vc.title = #"Reports";
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}
If you just create the view on demand with the selection it's much easier.

In IB, make sure you remembered to set the Class of File's Owner to ReportTypeViewController, and make sure that the view outlet of File's Owner is connected to your view.

Does ReportTypeViewController really inherit from UIViewController, or is it a UIView, as you say you built in IB?
navigationController pushViewContoller:animated: needs a UIViewController not a UIView. In IB, you can add a UIViewController and move your UIView onto it, or create it programmatically.

Related

[viewcontroller_name tableView:cellForRowAtIndexPath:]: unrecognized selector sent to instance 0x7b3a630'

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.

How to push DetailView without NavigationController on UIViewController

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];

UITableViewController Methods are not being called

As the title says none of my tableview controller methods are being called.
The steps I went through to create my table view are as follows.
1) I created a new file based on UITableViewController and selected the create with xib option. I Named my file myStuffViewController.
2) I have a rootview controller which is a UIViewController. In this view I have a navigation controller that I want to push my tableview controller onto at a certain point.
3) I setup my tableview and nav controller like so
mystuff = [[MyStuffViewController alloc]initWithNibName:#"MyStuffViewController"bundle:[NSBundle mainBundle]];
accountView = [[AccountView alloc] initWithNibName:#"Login" bundle:[NSBundle mainBundle]];
accountViewNavController = [[UINavigationController alloc] init];
accountViewNavController.delegate = self;
NSArray *ar= [NSArray arrayWithObjects:accountView,mystuff, nil];
[accountViewNavController setViewControllers:ar animated:NO];
[accountViewNavController popToRootViewControllerAnimated:NO];
accountView.title=#"Login";
4) Then when a user pushes a button I want to push the table view controller onto the stack like this.
[accountViewNavController pushViewController:mystuff animated:YES];
I've even tried calling [self.tableView reloadData] but none of the methods get called.
Could somebody propose why my table view methods are not being called?
EDIT 1
Just so I'm being as clear as I can be here is what my header file looks like. To be it doesnt seems like I'm missing anything.
#interface MyStuffViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate> {
RemixView *remixView;
NSMutableArray *remixListArray;
TBXML*tbxml;
}
#property(nonatomic,retain)NSMutableArray *remixListArray;
#property(nonatomic,retain)RemixView *remixView;
#property(nonatomic ,retain)TBXML *tbxml;
-(void)fetchRemixList:(NSString *)uid key:(NSString *)k1;
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
#end
Check
You declare in the .h file that you implement the two table view delegates UITableViewDelegate & UITableViewDatasource
In the NIB make sure you link to your delegate in file owner OR if you have created the TableView programmatically make sure you set the delegate ivar also.
Then see if the delegate methods start getting called

How do I create a UIViewController programmatically?

I am working in a app where i have data in UITableView. It is like a drill down application. User will click on a row and will go to next page showing more records in a UITableView. But problem in my case is that i dont know upto how many level user can drill. The number of levels are not fixed. So now i am thinking to create and add the viewcontrollers programmatically. Is it possible?? if yes how?
thanks in advance.
UIViewController *controller = [[UIViewController alloc] init];
controller.view = whateverViewYouHave;
Do you have your own view controller that you coded? In that case you probably don't need to set the view property as it has been set in IB if that is what you used. When you have your controller you can push it onto the navigationController or view it modally etc.
UIViewControllers are always created programmatically. It sounds like you just need to have the same class for each level of view controller, e.g.:
//CoolViewController:UITableViewController
//CoolViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (!self.isAtTopLevel) {
CoolViewController *cvc = [[CoolViewController alloc] initWithRecord:[self.records objectAtIndex:indexPath.row]];
[self.navigationController pushViewController:cvc animated:YES];
[cvc release];
} else {
//do something else
}
}
In this case, thingies would be some sort of recursive NSArray (i.e. an array of arrays).

UITableView with multiple viewcontollers

I just filled my UITableView with Planets. I would like each cell clicked to open into a new Xib (if this sounds like the wrong approach please direct). I can get a secondviewcontroller working, its getting the thirdviewcontroller and fourthviewcontroller working? Thanks.
Place your main view controller (the one with the table) inside a UINavigationController. Then, when the user selects a row, push a new view controller onto it.
The following function will help. As Ben Gottlieb said your main view controller will need to be in a UINavigationController. You need to implement the delegate method for didSelectRowAtIndexPath and this is where you create the new controller for your new view and load it.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
YourViewController *controller = [[YourViewController alloc] initWithNibName:#"YourViewController"bundle:nil];
[[self navigationController] pushViewController:yourViewController animated:YES];
[yourViewController release]; // don't leak memory
}
Based on the row number you can decide which nib to load.