i want to be able to select a item in my tableview and get to the correct view.
say i have two items in my uitableview. one has a value of ubuntu other has a value of arch- in my plist from arraA i have a SystemID which holds the text Ubuntu/Arch, never both
now when i select the one with ubuntu i want it to push to a new view where ubuntu stuff is
and if i go back and select arch it will again push me to a view where arch stuff is
so far i made this and it's not working as intended:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *segueName = nil;
if ([[arrayA valueForKey:#"SystemID"]isEqualToString:#"Ubuntu Linux"]) {
ActionViewController *controller = [[ActionViewController alloc] initWithNibName:#"Ubuntu Linux" bundle:nil];
controller.Serverinfo = [arrayA objectAtIndex:indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
}
else if ([[arrayA valueForKey:#"SystemID"]isEqualToString:#"Arch Linux"]) {
ActionViewController *controller = [[ActionViewController alloc] initWithNibName:#"Arch Linux" bundle:nil];
controller.Serverinfo = [arrayA objectAtIndex:indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
}
[self performSegueWithIdentifier: segueName sender: self];
}
also tried prepare forsegue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([arrayA valueForKey:#"Arch Linux"])
{
ActionViewController *controller = (ActionViewController *)segue.destinationViewController;
NSIndexPath *indexPath = [self.mainTableView indexPathForSelectedRow];
controller.Serverinfo = [arrayA objectAtIndex:indexPath.row];
NSLog(#"hostname = %#", controller.Serverinfo);
}
if ([arrayA valueForKey:#"Ubuntu Linux"]) {
ActionViewController *controller = (ActionViewController *)segue.destinationViewController;
NSIndexPath *indexPath = [self.mainTableView indexPathForSelectedRow];
controller.Serverinfo = [arrayA objectAtIndex:indexPath.row];
NSLog(#"hostname = %#", controller.Serverinfo);
}
}
arrayA is a NSMutableArray that read from a plist that you can see here
Replace your method to:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([arrayA[indexPath.row][#"SystemID"]isEqualToString:#"Ubuntu Linux"]) {
//ActionViewController *controller = [[ActionViewController alloc] initWithNibName:#"Ubuntu Linux" bundle:nil];
// Make sure you replace Storyboard with your storyboard name
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
ActionViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"Ubuntu Linux"];
controller.Serverinfo = [arrayA objectAtIndex:indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
}
else if ([arrayA[indexPath.row][#"SystemID"]isEqualToString:#"Arch Linux"]) {
ActionViewController *controller = [[ActionViewController alloc] initWithNibName:#"Arch Linux" bundle:nil];
controller.Serverinfo = [arrayA objectAtIndex:indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
}
}
You don't need to call [self performSegueWithIdentifier: segueName sender: self]; in didSelectRow because you already push your view in didSelectRow method. If you call it you puts the view twice.
Ooh boy, you're off in the weeds.
First, stop talking about pushing a "view." The term view and view controller are NOT interchangeable. A view is an object that appears on the screen. A view CONTROLLER is an object that manages a collection of views (usually the entire screen.)
You push a view controller, not a view. The view controller then displays it's content view on the screen.
Now, as to your code.
The if statement in your tableView:didSelectRowAtIndexPath: method is messed up.
if ([[arrayA valueForKey:#"SystemID"]isEqualToString:#"Ubuntu Linux"])
You don't want to use valueForeKey on an array. That method attempts to call valueForKey on every object in your array, and what it returns is an array of the results.
Arrays are indexed by a numeric index, not a key. You want an array as the data source for a table view.
What you want to do is to fetch the object at your selected row from your array.
In order to help you further, though, you need to explain what you have stored in your arrayA. Is it an array of dictionaries? An array of strings?
Also, you are mixing NIB based view controller code with storyboard code. You can certainly use both in the same program if you need to, but as a beginner you probably should not.
In your tableView:didSelectRowAtIndexPath method you're creating a view controller with initWithNibName, pushing it, and then calling performSegueWithIdentifier. You want to do one or the other, not both.
Use storyboard with prepareForSegue
Related
I an initialising a new view controller by tapping the cell of current controller. All I want to pass the object at cell to next controller by reference so that if I change the object in new controller it should change at original place.
I have tried much to get answer about it but could not get anywhere.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString * storyboardName = #"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
RecentItemDetailViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"RecentItemDetailViewController"];
vc.station = self.stations[indexPath.row];
[self.navigationController pushViewController:vc animated:YES];
}
All I want to get self.stations[indexPath.row] object in vc.station by reference so if I change vc.station then self.stations[indexPath.row] should also be changed.
self.stations and vc.station both are NSMutableArray type.
You already did it correctly. What you are most likely missing is reloading table view when you come back.
You can override view will appear method. Then check what is the selected row on your table view. Reload that row.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if(self.tableView.indexPathForSelectedRow) {
[self.tableView reloadRowsAtIndexPaths:#[tableView.indexPathForSelectedRow] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
// Or just reload the whole thing
[self.tableView reloadData];
}
But this naturally assumes you are never ever assigning to station property inside the RecentItemDetailViewController. You can call things like [self.station add...] but never self.station = ....
For pushing a view controller I'm using following code. On tap of UITableViewCell need to push DetailViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row)
{
NSDate *object = self.objects[indexPath.row];
DetailViewController *controller = [DetailViewController new];
[self.navigationController pushViewController:controller animated:YES];
}
}
Now problem is I get a jerk while animating. Refer to screenshot
its because of transparency. just set a background color for your viewcontroller. thats all.
Most probably you're doing something "heavy" on the main thread, while loading DetailViewController.
Could you show us what are you doing in DetailViewController?
I think there is issue with allocating object and pushing same object.
Do the following changes, it will work fine.
Your code :
DetailViewController *controller = [DetailViewController new];
[self.navigationController pushViewController:controller animated:YES];
Replace :
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main"
bundle: nil];
MyViewController *controller = (MyViewController*)[mainStoryboard
instantiateViewControllerWithIdentifier: #"<your Storyboard ID>"];
it will work fine at my end.
Hope this help you.
I'm having a bit of a problem with my app, I have these view controllers below:
I have the locations view controller with an array of locations and I have a locations array in the Menu Table view controller, I need to pass the array from Locations to Menu Table, but I dont know how, can somebody Help me? I have tried this coding below:
-(void)pushMenuTableView
{
UIStoryboard *storyboard = self.storyboard;
UINavigationController *menuTableController = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:#"MenuTableViewController"];
MenuTableViewController *menuController = (MenuTableViewController *) menuTableController.topViewController;
[self.navigationController pushViewController:menuTableController animated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MenuTableViewController *mvc = [[MenuTableViewController alloc]init];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Location *selectedLocation = [_locations objectAtIndex:[path row]];
[mvc setLocation:selectedLocation];
[self pushMenuTableView];
}
You are using two different MenuTableViewController objects. One you create using alloc/init and give the Location to and another that you obtain from the navigation controller and then push. You need to pass the data to the controller you're going to display instead of one you make temporarily.
How about using only one navigation controller and moving through the view controllers using segues?
You could try something like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyboard = self.storyboard;
MenuTableViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"MenuTableViewController"];
Location *selectedLocation = [_locations objectAtIndex:indexPath.row];
vc.location = selectedLocation;
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:nc animated:YES completion:nil];
}
Im working with table view controllers within story board.
I currently have one tableview controller embedded in navigation controller in my storyboard. This table view controller is linked to an ObjectiveC file.
Through code i can display another table view controller with the correct data using the tableView didSelectRowAtIndexPath method.
code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
WorkoutType *workoutType = (WorkoutType *)[self.fetchedResultsController objectAtIndexPath:indexPath];
WorkoutSetViewController *detailViewController = [[WorkoutSetViewController alloc] initWithWorkoutType:workoutType];
[self.navigationController pushViewController:detailViewController animated:YES];
}
But if i try to add another table view controller in storyboard, associate it with an objectiveC view controller file and connect the first TVC to it, That table view controller is not shown upon running the code.
I need to customise the second table view visually and have tried to use a segue push...as follows:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"show"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
WorkoutType *workoutType = (WorkoutType *)[self.fetchedResultsController objectAtIndexPath:indexPath];
//WorkoutSetViewController *detailViewController = [[WorkoutSetViewController alloc] initWithWorkoutType:workoutType];
WorkoutSetViewController *destViewController = segue.destinationViewController;
destViewController.workoutType = workoutType;
//[self.navigationController pushViewController:detailViewController animated:YES];
}
}
But when i run this code, the second table view controller doesn't even load although the application doesn't crash.
Whatever I am doing wrong must be simple, any help would be appreciated.
Thank You
Simple You have to navigate through below code. (No need to set storyboard when you working in same storyboard ViewController)
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexpath.row==0)//for first row click
{
WorkoutSetViewController *objWorkoutSetVC=[[WorkoutSetViewController alloc] i nitWithNibName:#"WorkoutSetViewController" bundle:nil];
[self.navigationController pushViewController:objWorkoutSetVC animated:YES];
}
else if(indexpath.row==1)//for second row click
{
//Put code in which screen you have to navigate
}
}
Do NOT Alloc init your new view controller. Use this code instead:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
WorkoutSetViewController *detailViewController = (WorkoutSetViewController *)[storyboard instantiateViewControllerWithIdentifier:#"coolID"];
[self.navigationController pushViewController:detailViewController animated:YES];
For MainStoryboard write your storyboard name and for coolID write your view controller id name which you set in your storyboard.
I am trying to use Table Views and navigation controllers. This is the code I would use if I was using NIB files. This piece of code would work fine if I was using NIB files, but I decided to try storyboard -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (_webViewController == nil) {
self.webViewController = [[[WebViewController alloc] initWithNibName:#"WebViewController" bundle:[NSBundle mainBundle]] autorelease];
}
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
_webViewController.entry = entry;
[self.navigationController pushViewController:_webViewController animated:YES];
}
I have created another scene in storyboard and have everything set up out there. When a user clicks a cell in this Table View they will be taken to that scene. This is what I wrote for the code but it is not right and I'm hopelessly stuck :( -
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"articleView"]){
UINavigationController *navigationController = segue.destinationViewController;
WebViewController *controller = (WebViewController *)navigationController.topViewController;
controller.webView = self;
}
Answer - Answers 2 and 3 are both right
I had a very similar problem 2 days ago, with the user selecting the disclosure accessory on a row and wanting to invoke a segue. What worked for me:
In storyboard, create segue "articleView" from the entire tableView controller (not the cell) to the next viewController.
Add the following method to your tableViewController:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"articleView" sender:[self.tableView cellForRowAtIndexPath:indexPath]];
}
Just for you I also tested with tableView:didSelectRowAtIndexPath: and it works.
Enjoy,
Damien
If your table view is already embedded in a navigation controller (looks that way, from your first code snippet) then the destination view controller of your segue will be a WebViewController, not a navigation controller.
You can therefore have your prepareForSegue method pass the item directly across :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"articleView"])
{
WebViewController *controller = (WebViewController *)segue.destinationViewController;
RSSEntry *entry = [_allEntries objectAtIndex:[self.tableView indexPathForSelectedRow].row];
controller.entry = entry;
}
}
You may find this excellent two-part tutorial helpful
http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1