How to present specific view controller when a cell is selected on iOS 6? - xcode

I am using Xcode 4.5.2, and iOS6.
I have 3 View Controllers, FirstViewController contains dynamic table view.
What I want is, when the first row is selected it takes the user to SecondViewController, and when the second row is selected to the ThirdViewController.
Below is the didSelectRowAtIndexPath method of my code
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *path = indexPath;
NSInteger theInteger = path.row;
switch (theInteger)
{
case 0:
//here i wanna present FirstViewController
break;
case 1:
//here i wanna present SecondViewController
break;
default:
break;
}
}
I am not sure if this is the best practice but this is all what I know. I hope I made it clear.

Related

IOS/Xcode/Storyboard: How can I segue from table row to modal view controller

This is a storyboard question, I think.
How do you create a segue from a table row to a new view controller?
Here is a picture of what it is supposed to look like.
I tried control dragging from the table to the view controller on the bottom but it does not take. If I embed the view controller in a navigator I can create a push segue but it does not work when you click on it.
This is the code I am using for that but first I would like to be able to replicate the storyboard in the picture. In any case I want the new view controller to pop up when you click on the table row.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Store Selection
[self setSelection:indexPath];
}
...
else if ([segue.identifier isEqualToString:#"updateToDoViewController"]) {
NSLog(#"segue to update");
// Obtain Reference to View Controller
UpdateTodoVC *vc = (UpdateTodoVC *)[segue destinationViewController];
// Configure View Controller
[vc setManagedObjectContext:self.managedObjectContext];
if (self.selection) {
// Fetch Record
NSManagedObject *record = [self.fetchedResultsController objectAtIndexPath:self.selection];
if (record) {
[vc setRecord:record];
}
// Reset Selection
[self setSelection:nil];
}
}
}
Thanks for any suggestions.
You can't ctrl+drag from a "Prototype" cell! You can only do that with static cells. So you have to ctrl+drag from the UITableViewController to the next view controller, give it an identifier of updateToDoViewController, and perform the segue programatically:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Store Selection
[self setSelection:indexPath];
// Perform the segue with identifier: updateToDoViewController
[self performSegueWithIdentifier:#"updateToDoViewController" sender:self];
}

Dynamic sections with dynamic cells with Core Data?

I am really close to the solution, but i cannot figure out the rest, so i am asking for help now...
I use core data, so I'd like to show, whats inside of it.
Here is my code so far:
at .m
#property NSMutableArray *users, *sharedWishes;
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *delegate=[UIApplication sharedApplication].delegate;
self.context=delegate.managedObjectContext;
self.users=delegate.collectUserNames; //collect every users from database
self.sharedWishes=[delegate collectSharedWishes]; //collect every wish from DB - each user has different number of records
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// works great so far
return self.users.count;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
User *user=[self.users objectAtIndex:section]; //shows the username for each section
if(self.users.count<1)
return #"no friends";
else
return user.userName; //this is just fine, too
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SharedWishes *wish=[self.sharedWishes objectAtIndex:indexPath.row]; //this is where the problem is...the tableview shows the records until the other user records starts
static NSString *identifier=#"Cell";
Cell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
...
}
So, any suggestion for the cellForRowAtIndexPath:, what should I add?
Every section should have a title (which has) for the current user name, and every section should contain that particular person's records (which fails). Instead, when new section begins (with new user) the existing records starting to repeat. How should I avoid it?
Thanks in advance!
EDIT: added this few line to cellForRowAtIndexPath:
User *user=[self.users objectAtIndex:indexPath.section];
AppDelegate *delegate=[UIApplication sharedApplication].delegate;
self.currentUserWishes=[delegate collectCurrentUserSharedWishes:user];
Now it works!

tableView cell - select from another tableView

I am wondering if and how it could be possible to get the following working
From a tableView cell I want to get to another tableView with i.e. 10 different cells where the user has to choose on, and this one is set up in the previous table cell.
So let's say i.e
The table cell is "choose" - touching this cell a tableView with 10 different cells opens up and the user can choose one of them. By choosing one of them it gets back to the inital tableView and the cell is filled with the users choice. How exactly to achieve this?
Hello you can check with below code :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(tableView == displayTableView)
{
//Implementation for choiceTableview
choiceTableview.hidden = FALSE;
displayTableView.hidden = TRUE;
[choiceTableview reloadData];
}
else
{
//Implementation for displayTableView
choiceTableview.hidden = TRUE;
displayTableView.hidden = FALSE;
[displayTableView reloadData];
}
}
You have to check below condition in tableview method and display data according to ti:
if(tableView == displayTableView)
{
//Implementation for choiceTableview
}
else
{
//Implementation for displayTableView
}
This will help you to handle as many table view as you want. Cheers...

Using a different view in editing mode in a view based NSTableView

I have a NSTableView with a single NSTableCellView column that let's say, has an icon, name and an optional date.
When you edit a row, I want to replace the whole view with a simple NSTextField, and I will do some parsing to that text and extract that optional date, if present.
My question is, how would you implement this editing mechanism?
I tried returning a different view in the tableView:viewForTableColumn:row, something like:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
BOOL isSelected = [tableView isRowSelected:row];
if (isSelected)
{
NSView *view = [tableView makeViewWithIdentifier:#"editor" owner:self];
....snip....
return view;
}
else
{
TaskView *view = [tableView makeViewWithIdentifier:#"view" owner:self];
....snip....
return view;
}
}
and then whenever the selected row changes, trying to request a refresh on that row.
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
NSTableView *table = [aNotification object];
NSUInteger rowIndex = [table selectedRow];
[table reloadDataForRowIndexes:[NSIndexSet indexSetWithIndex:rowIndex]
columnIndexes:[NSIndexSet indexSetWithIndex:0]];
}
It doesn't quite work, and the code feels a bit dirty.
It must be a better way of doing this, and I can't seem to find in the docs or online.

XCode 4.2 + iOS 5 Storyboard : Can't distinguish between segmented control sections as segue initiators?

Using Xcode 4.2 Storyboard, I've just created a segmented control in a view with two segments. I then ctrl-drag from each segment to a separate view to create two segues. It seems the developers forgot to distinguish between the segments though, since only one segue can be created; attempting to create a second to the 'other' control segment cause the first segue to be replaced by the second. Does anyone have a Storyboard workaround for this, or must I write the code manually?
Thank you.
4.2 has been publicly release now.
The problem can be solved with Storyboard using a custom segue. In the custom segue the segmented control can be tested to determine which controller to call. I have tested the following custom segue:
#import "FlipTopPop.h"
#interface UIViewController (Extension)
#property (strong, nonatomic) IBOutlet UISegmentedControl *tabControl;
#end
#implementation FlipTopPop
- (void) perform {
UIViewController *src = (UIViewController *) self.sourceViewController;
switch (src.tabControl.selectedSegmentIndex) {
case 0:
// go to settings
src.tabControl.selectedSegmentIndex = 1; //not yet implemented - for now don't segway - reset to index 1 for now
break;
case 1:
// this controller is called with index 1 - do nothing - should not get here
break;
case 2: {
[UIView transitionWithView:src.navigationController.view duration:0.5
options:UIViewAnimationOptionTransitionFlipFromTop
animations:^{
[src.navigationController popViewControllerAnimated:NO];
}
completion:NULL];
}
break;
default:
break;
}
}
#end
Note that I have not yet implemented the transition in case 0 (segment 0) which will transition to another controller with code similar to what is implemented in case 2.
I solved this situation in the following way. I added the segmented control to my toolbar in storyboard. This must be done first by adding a bar button and then adding the segmented control to that. My segment control has 3 segments each of which takes you to a different view. The current view represents on of those leaving a requirement for two segues to the two other views. I then created two button out of the view to the right on the toolbar. I control clicked from those to the two destination controllers to create the two required segues. I also attached the "value changed" sent action to the IBAction in the code below. The rest is implemented in code as follows:
- (IBAction)segmentChanged:(id)sender {
switch (self.segmentedControl.selectedSegmentIndex) {
case 0:
[self performSegueWithIdentifier: #"goToSettings" sender: self];
break;
case 1:
// aready here - do nothing
break;
case 2:
[self performSegueWithIdentifier: #"returnToNotes"
sender: self];
break;
default:
break;
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"goToSettings"]) {
[[segue destinationViewController] setManagedObjectContext:self.managedObjectContext];
// do nothing special
}
if ([[segue identifier] isEqualToString:#"returnToNotes"]) {
// do nothing special
}
}
It is also necessary to keep the segment index selected to show what view cone is currently in. This is done with the following statement in the viewWillAppear method:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.segmentedControl.selectedSegmentIndex = 1; // the index for the current view
}
Similar code is implemented in each of the three view controllers selected by the segmented control. This solution is not much different than doing it all in code, but has the advantage the the storyboard reflects the views and view transitions (segues).

Resources