How to pass data from one ViewController to another after using Segue? - ios

I went from one ViewController to another using Segue in my second viewController has an UITableView. now, I want to push from second ViewController's didSelected method to another viewController.
code use to Segue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
self.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
SearchVC *svc = segue.destinationViewController;
self.providesPresentationContextTransitionStyle = YES;
self.definesPresentationContext = YES;
}
under button action:
-(void) searchButtonClicked:(id)sender {
[self performSegueWithIdentifier:#"search" sender:nil];
}
And the main problem arise here, can't navigate using below code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
PackageListVC *pdvc = (PackageListVC *)[self.storyboard instantiateViewControllerWithIdentifier:#"PackageListVC"];
pdvc.strServiceProviderId = [[[arrSearch objectAtIndex:indexPath.row] objectForKey:#"ServiceProviderId"] stringValue];
pdvc.strCategoryId = [[[arrSearch objectAtIndex:indexPath.row] objectForKey:#"CategoryId"] stringValue];
pdvc.strSubCategoryId = [[[arrSearch objectAtIndex:indexPath.row] objectForKey:#"SubCategoryId"] stringValue];
pdvc.strTitle = [[arrSearch objectAtIndex:indexPath.row] objectForKey:#"CompanyName"];
[self.navigationController pushViewController:pdvc animated:YES];
}

You do it in the prepareForSegue method.
where you declare svc (SearchVC *svc = segue.destinationViewController;) you can say something like:
svc.variable = 1;

Related

iPad - DetailViewController programmatically

I'm working on an iPad app where I use UISplitViewController. I also use DetailViewController in which to display some information from XML, but the information is not displayed in DetailViewController but in MaterViewController. I really do not know how to fix it. Can you please help. Thank you
There is code:
-(void) showDetailsForIndexPath:(NSIndexPath*)indexPath
{
[self.searchBar resignFirstResponder];
DetailViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailsViewController"];
Slova* slova;
if(isFiltered)
{
slova = [filteredTableData objectAtIndex:indexPath.row];
}
else
{
slova = [self.slovoArray objectAtIndex:indexPath.row];
}
vc.slovoItem = slova;
[self.navigationController pushViewController:vc animated:true];
}
The reason is that you're pushing the new UIViewController onto the stack for self, which is presumably the MasterViewController. Instead, create a new project using Apple's Master-Detail Application and you'll see how they do it.
Caveat: they use Storyboards, but it is very possible to do this all in code.
In Apple's example, both the MasterViewController and the DetailViewController are visible from the start (in landscape).
Look how Apple's example sets detailItem (slovoItem?) for DetailViewController:
#pragma mark - Segues
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = self.objects[indexPath.row];
DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
[controller setDetailItem:object];
controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
controller.navigationItem.leftItemsSupplementBackButton = YES;
}
}
So, you'll have DetailViewController already visible and you'll just update its detailItem property.
Therefore, the following code exists in DetailViewController:
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem {
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView {
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
}
}

How to transition from UITableViewCell to another view controller

I am trying to go from a UITableView with prototype cells to a detailviewcontroller of the item I selected on.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"historyToDetail"])
{
BYFHistoryDetailViewController *controller = (BYFHistoryDetailViewController *)segue.destinationViewController;
controller.workOut = [[BYFWorkOut alloc] init];
controller.workOut=_selectRow;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BYFHistoryTableViewController *detailViewController =[[BYFHistoryTableViewController alloc] init];
NSArray *items = [[BYFworkOutStore sharedStore] allItems];
BYFWorkOut *selectedItem = items[indexPath.row];
_selectRow = selectedItem;
}
What is not happening is the transition from the table to detail I have a push segue from the prototype cell to the details.
What am I missing?
You are doing quite a lot wrong here. When using segue's you don't create an instance of the class. You simply call:
[self performSegueWithIdentifier:#"MySegue" sender:self];
This will use the segue you have defined in the storyboard. Where MySegue is the segue ID you created.
When you want to pass in data you use the callback
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
BYFHistoryDetailViewController *vc = (BYFHistoryDetailViewController *)[segue destinationViewController];
vc.workOut = selectedItem;
}
But using this callback will mean you will need to store selectedItem somewhere after you click the row so you can access it here.
EDIT
Your code seems a bit odd here also.
You set workout to a new object.
detailViewController.workOut = [[BYFWorkOut alloc]init];
Create another object from data.
NSArray *items = [[BYFworkOutStore sharedStore] allItems];
BYFWorkOut *selectedItem = items[indexPath.row];
And then assign the new object, overwriting the previous one.
//give detail view controller a pointer to the item object in row
detailViewController.workOut = selectedItem;
There is no need to have the first line of code at all
EDIT 2
If you only going to be using the one selected item at a time. you can do this in your UITableViewController class.
#implementation MyTableViewControllerClass
{
BYFWorkOut *_selectedItem;
}
inside didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *items = [[BYFworkOutStore sharedStore] allItems];
_selectedItem = items[indexPath.row];
}
EDIT 3
I've modified the code you posted here. You didn't add the first line of code i posted. Please look at this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"historyToDetail"])
{
BYFHistoryDetailViewController *controller = (BYFHistoryDetailViewController *)segue.destinationViewController;
controller.workOut = _selectRow;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *items = [[BYFworkOutStore sharedStore] allItems];
_selectRow = items[indexPath.row];
[self performSegueWithIdentifier:#"historyToDetail" sender:self];
}
You need to name your segue and call the method:
[self performSegueWithIdentifier:#"MySegue" sender:self];

Implementing tableView didSelectRowAtIndexPath in storyboard

How would you implement tableView didSelectRowAtIndexPath in storyboard. I have already created a view controller that creates the table. I don't know what to do in the storyboard to get the view to transition when clicking each cell in the table.
This is what I originally had programmatically inside that method:
Show *show = [self.shows objectAtIndex:indexPath.section];
ShowContentItem *showItem = show.showContentItems[indexPath.row];
if ([showItem.contentType isEqualToString:#"Headline_Section"]) {
HeadlinesViewController *headlinesVC = [[HeadlinesViewController alloc] init];
headlinesVC.shows = [[NSMutableArray alloc] initWithObjects:show, nil];
[self.navigationController pushViewController:headlinesVC animated:YES];
} else {
NSInteger contentIndex = [self.storyPostContent indexOfObject:showItem];
NSRange rangeForView = NSMakeRange(contentIndex, [self.storyPostContent count] - contentIndex );
ShowContentViewController *showContentVC = [[ShowContentViewController alloc] init];
showContentVC.contentList = [self.storyPostContent subarrayWithRange: rangeForView];
[self.navigationController pushViewController: showContentVC animated:YES];
}
How would I specifically alter the code above to work with storyboard?
You control drag from your prototype cell to your destination controller. Then in prepareForSegue you do something like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"yourSegue"]) {
yourViewController *destinationController = segue.destinationViewController;
destinationController.someVariable = yourTable.indexPathForSelectedRow;
}
}

Using didSelectRowAtIndexPath

I have a table with 5 cells and if the user clicks my second cell for example, I want to show another table or another page.
Is using
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath`
the way ?
Here is an example of navigating to another view controller when the user clicks on the second cell (assuming you have a navigation controller):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row == 1){
MyViewController * vc = [[MyViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}
}
sample Link :http://www.tutorialspoint.com/ios/ios_ui_elements_tableview.htm
if (indexPath.row ==1)
{
//Second cell index path its move another class xib
classname *yourcontroller = [[classname alloc] initWithNibName:#"xib file name" bundle:nil];
yourcontroller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:helpcontroller animated:YES completion:nil];
}
If you are using a Storyboard, you may skip tableView:didSelectRowAtIndexPath: and use prepareForSegue:sender: instead:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"__SEGUE_IDENTIFIER__"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
SomeOtherViewController *vc = (SomeOtherViewController *)segue.destinationViewController;
// assign something to vc
}
}

NSDictionary is null which is populated in previous View Controller

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog((#"This is didSelectRowAtIndexPAth"));
DetailViewController *detailViewController = [[DetailViewController alloc] init];
detailViewController.myDictionary = [self.placeArray objectAtIndex:[indexPath row]]
// This MyDictionary NSdictionary is declared in other viewController which is populated here ..
NSLog(#"My Dictionary: This is a MasterView one %#",detailViewController.myDictionary);
UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
detailViewController = [mystoryboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
//The below NSlog is showing the content , its not null still in next viewcontroller same variable showing null content
NSLog(#"My Dictionary: This is a MasterView two %#",detailViewController.myDictionary);
[self performSegueWithIdentifier:#"showDetail" sender:self];
}
Use PrepareForSegue for this purpose
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
DetailViewController * detailViewController = (DetailViewController*)segue.destinationViewController;
detailViewController.myDictionary = [self.placeArray objectAtIndex:index];
}
From did select just use the prepareForSegue method(Hope you have already used.) Set the presenting view controller option via xib or programmatically and just stop there.prepareForSegue will call automatically while presenting the view while using storyboard.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
DetailViewController *detailViewController = [[DetailViewController alloc] init];
detailViewController.myDictionary = [self.placeArray objectAtIndex:[indexPath row]];
}
edit you did_select_method and try.

Resources