commit object to next TableViewController - iphone

i got a tableview controller.
if a cell is selected i perform the following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ablogSingleCatTableViewController *singleCatTableViewController = [[ablogSingleCatTableViewController alloc] initWithStyle:UITableViewStylePlain];
// Push the detail view controller.
[[self navigationController] pushViewController:singleCatTableViewController animated:YES];
[singleCatTableViewController release];
}
i want to commit an object to the next view controller that slides in after this row is selected.
do i have to do something like this?
ablogSingleCatTableViewController *singleCatTableViewController = [[ablogSingleCatTableViewController alloc] initWithStyle:UITableViewStylePlain];
[singleTableViewController setMyObject:superDuperObject];
or is there an easier way to do something like that?
i need this object directly after this tableviewcontroller is initialized, to fill it with specific data that belongs to this object.
please give me some advices.
thanks

You can also pass the object to the next view controller when you init the view controller.
To do so, you need to implement your own initializer for the view controller.
For example:
- (id)initWithStyle:(UITableViewStyle)style superDuper:(SuperDuper*)superDuperObject {
if (self = [super initWithStyle:style]) {
superDuper = superDuperObject;
}
return self;
}
Then,
ablogSingleCatTableViewController *singleCatTableViewController =
[[ablogSingleCatTableViewController alloc] initWithStyle:UITableViewStylePlain superDuper:superDuperObject];

Related

iOS 6 pass data from TableView to Detail View

I've a table view with some rows and each row has its Detail Disclosure button.
I want that when the user taps on that button, another table view (customized by storyboard) appears and shows some relative data.
From TableViewController.m
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailViewController = [[detailViewController alloc] init];
//creating a parse object
PFObject *checkin = [_dataSourceArray objectAtIndex:indexPath.row];
//getting data i want
NSString *trainNumb = [checkin objectForKey:#"trainNumber"];
//passing the data
detailViewController.trainNumber = trainNumb;
}
From the DetailTableViewController.m
- (void)viewDidLoad {
[_trainNumberLabel setText:_trainNumber];
}
The problem is that in the DetailTableView the NSString results to be null.
What I'm missing here? Thank you!
If this code actually compiles, this may be your problem:
DetailViewController *detailViewController = [[detailViewController alloc] init];
It should be:
DetailViewController *detailViewController = [[DetailViewController alloc] init];
But you shouldn't be alloc/initing your view controller in the first place. When you customize a view controller in Interface Builder, you've got to instantiate it like this if you want those customizations:
[self.storyboard instantiateViewControllerWithIdentifier:#"MyStoryboardIdentifier"]`
And you've got to set the controller's storyboard ID to "MyStoryboardIdentifier" or whatever identifier you want to use.
Also, as you've indicated in the comments, you've got a timing issue: your detail controller's viewDidLoad runs before you set the train number. A better approach would be to ensure that it works regardless of the sequence:
- (void)updateTrainNumberLabel
{
self.trainNumberLabel.text = self.trainNumber;
}
- (void)setTrainNumber:(NSString *)trainNumber
{
_trainNumber = trainNumber;
[self updateTrainNumberLabel];
}
- (void)viewDidLoad
{
...
[self updateTrainNumberLabel];
}
In other words, you configure your label in a separate method, in this case updateTrainNumberLabel, and when anything that could affect the label happens, e.g. the view loading or the number being changed, you call the method.

Subview-title not displayed properly using storyboard

I have a tableview and a detailview, connected with segues.
I want the detailview's title to be the title of the selected cell.
If I use this code in GradeDetail.m, it works, but is static, of course:
self.navigationController.title = #"Title goes here";
If I use this on the other hand, it doesn't work, but should be dynamic:
self.navigationItem.title = grade.fag;
I have the class Grade imported where necessary and "fag" (means "school subject" in Danish, not gay) property defined in the class.
Here is an excerpt of the code from the tableview:
...
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder]))
{
grades = [[NSMutableArray alloc] init];
Grade* grade = [[Grade alloc] init];
grade.fag = #"Dansk";
grade.mundtlig = #"7";
[grades addObject:grade];
...
}
return self;
}
And the code pushing the detail-view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
GradeDetail *detailViewController = [[GradeDetail alloc] init];
Grade* grade = [self.grades objectAtIndex:indexPath.row];
detailViewController.grade = grade;
//[self.navigationController pushViewController:detailViewController animated:YES];
}
Notice from the above code, what I commented out:
[self.navigationController pushViewController:detailViewController animated:YES];
If I remove the segue in the storyboard, and use this line of code instead, the detail-view shows the correct title, but the view isn't loading correctly. (Read: I don't want to use this line of code, I want it to work in storyboard.)
Please tell me if you want to see more of the code or have some of the above clarified.
You'll need to override -prepareForSegue:sender: to get a hook into the incoming view controller and set any properties required. You'll also need to specify segue identifiers in the storyboard.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"ShowGradeDetails"])
{
GradeDetail *detailViewController = (GradeDetail *)segue.destinationViewController;
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
Grade *selectedGrade = [self.grades objectAtIndex:selectedIndexPath.row];
detailViewController.grade = selectedGrade;
}
}
Maybe try setting the viewController's title: self.title = grade.fag;

How can I move to another view controller when the user clicks on a row?

I am new to iPhone development, and I want to move to another page when the user clicks on a particular row. So, if they click on first row, I want the page to redirect to the first view controller, and so on. Each row has its own view controller.
first of all you don't need to set different view controller for each row (unless you have a very good reason for doing that).
the correct way is to set 1 view controller that will fit all the cells in the raws and change its data according to the selected row.
the way you do that is:
in the - DidSelectRowAtIndexPath function in your implementation file you shuld:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//allocate your view controller
DetailedViewController *detailedViewController = [[DetailedViewController alloc] init];
//send properties to your view controller
detailedViewController.property1 = someProperty1;
detailedViewController.property2 = someProperty2;
//push it to the navigationController
[[self navigationController] pushViewController:detailedViewController animated:YES];
[detailedViewController release];
}
i do recommend that you will start by using apple examples, they are great and there are a lot of them :
http://developer.apple.com/library/ios/#samplecode/TableViewSuite/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40007318
Good luck
#kashyap in didSelectRowAtIndexPath delegate of the UITableView you have to check the conditions that on which indexPath you are clicking and open the viewControllers you had made accordingly
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here -- for example, create and push another view controller.
FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
switch (indexPath.row) {
case 0:
[self.navigationController pushViewController:firstViewController animated:YES];
[firstViewController release];
break;
case 1:
[self.navigationController pushViewController:secondViewController animated:YES];
[secondViewController release];
break;
}//Likewise do for the no of rows you have in your `UITableView`
Hope you get my point.....Good Luck!
Dig into the docs:
http://developer.apple.com/library/ios/#DOCUMENTATION/UserExperience/Conceptual/TableView_iPhone/ManageSelections/ManageSelections.html#//apple_ref/doc/uid/TP40007451-CH9-SW6
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.row) {
case "X"
// Allocate the viewController
[navigationcontroller.pushViewController:"yourviewcontroller" animated:YES];
[release "yourviewcontroller"];
break;
default:
break;
}
}
EDIT: Didn't realize of what you're trying. As shani said, you should not create a viewcontroller for each row if there is no need to. Try changing the datasource for each row but using the same viewcontroller. So forget this implementation.
Easy:
Create and push your view controller in this delegate function:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
It sounds like using a UINavigationController is what you want.
You then push your "first view" on to the navigation controller's stack from the table view, e.g., from you UITableView delegate:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
// indexPath.row == 0
FirstViewController* firstVC = [[FirstViewController alloc] initWithNibName...];
[self.navigationController firstVC animated:YES];
[firstVC release];
...
}
This was the one the things that I got problem when I first start. Here is my code that I use for this.
In subviewcontroller I have my own constructor like
-(id)initWithName:(NSString*)naMe
{
name=naMe;
return self;
}
And in didSelectRowAtIndexPath Delegate method Of UITableView. I alloc it
MyListView mylistview=[[MyListView alloc] initWithName:#"name"];
[self.navigationController pushViewController:mylistview animated:YES];
[mylistview release];
NSInteger row = indexPath.row;
UIViewController *newFrontController = nil;
if (row == 0)
{
YourViewController *yourViewController = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
newFrontController = [[UINavigationController alloc] initWithRootViewController:peopleViewController];
[self.navigationController setNavigationBarHidden:YES];
} else if(row == 1){
//do your code
}
do this in didSelectRowAtIndexPath in tableView.
You can move to another page when user tap on row by simply making segue by "Control Drag" from cell to ViewController and make segue type "Push".

TableView help pushing a custom view

So I am trying to simple traverse to the next level of a table view by doing this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 1) {
FiltersController *aFiltersCont = [[FiltersController alloc] init];
aFiltersCont.displayedFilters = [appDelegate.groupedBusiness allKeys];
aFiltersCont.currentLevel = self.currentLevel + 1;
[self.navigationController pushViewController:self animated:YES];
}
}
is there any reason why this would not be pushing the controller? I had a similar problem before, but solved it by displaying the view modally. However, this time, this is in a popover and needs to slide to the next screen inside that popover. Any Ideas? Thanks in advance.
OK I am going to put some more source up here to try and help...
Inside the main view controller I have this code to make the popover from a button:
// Create and configure the filters controller.
FiltersController *aFiltersController = [[FiltersController alloc] initWithStyle:UITableViewStylePlain];
self.filtersController = aFiltersController;
filtersController.appDelegate = self.appDelegate;
UINavigationController *filtersNavController = [[UINavigationController alloc] initWithRootViewController:filtersController];
UIPopoverController *filtersPopover = [[UIPopoverController alloc] initWithContentViewController:filtersNavController];
self.filtersPopoverController = filtersPopover;
filtersPopoverController.delegate = self;
and then I have the code I first posted in my filtersController class. Does that help at all?
[self.navigationController pushViewController:self animated:YES];
Should be
[self.navigationController pushViewController:aFiltersCont animated:YES];
If you are just displaying your sublcass of UITableViewController inside a UIPopoverController, the UINavigationController will not be created for you automatically. You may need to modify the code where you are creating the UIPopoverController with something like this:
MyTableViewController *table = [[[MYTableViewController alloc] init] autorelease];
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:table] autorelease];
myPopover = [[UIPopoverController alloc] initWithContentViewController:nav];
Then you should be able to push and pop navigation controllers on the stack no problem.
you are pushing self which is a reference to the current view controller. you should change the line with the push to the following if aFiltersCont is the viewController you are trying to drill to.
[self.navigationController pushViewController:aFiltersCont animated:YES];
You might be confused about the index. The first index is 0 not 1, so maybe you want indexPath.row == 0?
Also, you should release aFiltersCont before returning (you're leaking a FiltersController and anything it owns every time you run this method.
So maybe this?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
FiltersController *aFiltersCont = [[[FiltersController alloc] init] autorelease];
aFiltersCont.displayedFilters = [appDelegate.groupedBusiness allKeys];
aFiltersCont.currentLevel = self.currentLevel + 1;
[self.navigationController pushViewController:aFiltersCont animated:YES];
}
}

Load a particular NIB based on tableview selection

I have an initial table view that I created as the initial menu within my app. Obviously each option will access something different including NIBs. Part of the constants for the menu options is the NIB. When each option is pulled from a PLIST, I also include which NIB I would like to be called upon.
Am I missing something or am I just going the wrong way entirely?
Right now a selection does nothing.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableString *targetnib = [[self.menuoptions objectAtIndex:indexPath.row] objectForKey:NIB_KEY];
if (targetnib == #"HospitalDirectoryViewController") {
HospitalDirectoryViewController *hospitalDirectoryViewController = [[HospitalDirectoryViewController alloc] initWithNibName:#"HospitalDirectoryViewController" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:hospitalDirectoryViewController animated:YES];
[hospitalDirectoryViewController release];
}
if (targetnib == #"PhysicianDirectoryViewController") {
PhysicianDirectoryViewController *physicianDirectoryViewController = [[PhysicianDirectoryViewController alloc] initWithNibName:#"PhysicianDirectoryViewController" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:physicianDirectoryViewController animated:YES];
[physicianDirectoryViewController release];
}
}
Try using [targetnib isEqualToString: #"TheNibName"]. In your posted code, you're comparing pointers, not the text.