In my code I created this
#import <UIKit/UIKit.h>
#interface ViewControllerTable : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *myTableView;
#property (strong, nonatomic) NSString *plcVar;
#end
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
listaPlacas *placas = [[listaPlacas alloc]init];
[self.navigationController pushViewController:placas animated:YES];
}
But in the other view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
for (UITableViewCell *cell in [tableView visibleCells]) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSString *placaVar = cell.textLabel.text;
ViewControllerTable *VCT = [[ViewControllerTable alloc]init];
VCT.plcVar = [[NSString alloc]init];
VCT.plcVar = placaVar;
}
When I click in back button the value in variable plcVar is empty.
I assume that "other view" is the viewController you push to from your first ViewController, and you are trying to set a property in the firstViewController from your otherViewController.
Then here is where you go wrong:
ViewControllerTable *VCT = [[ViewControllerTable alloc]init];
You are creating a new instance of VCT, when in fact you want a reference to the VCT that you pushed from.
You can get this via your navigation controller...
NSArray* viewControllers = [self.navigationController viewControllers];
ViewControllerTable* VCT = [viewControllers objectAtIndex:
[ viewControllers count]-2];
Related
Sunny Thank you for your reply. I made the following changes and now by clicking on a tablecell or search result, I am pushed to DetailViewController but, for any cell I click on I get the same DetailView. I need the detail view to show different information depending on the cell I click on.
I added this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
[self performSegueWithIdentifier: #"ShowDetails" sender: self];
}
}
New segue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ShowDetails"]) {
DetailViewController *detailviewcontroller = [segue destinationViewController];
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
int row = [myIndexPath row];
detailviewcontroller.DetailModal = #[_Title[row],_Attribute[row]];
}
}
update:
The reason I am having difficulty in implementing your code is because my information to be passed to the detailview is in DetailViewController.h but as an array not a string:
#property (nonatomic,strong) NSArray *Title;
#property (nonatomic,strong) NSArray *Attribute;
in the viewdidload I get these from my plist:
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
_Title = [dict objectForKey:#"caseName"];
_Attribute= [dict objectForKey:#"attribute"];
the table view is populated with said data with the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableViewCell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row];
}
else
{
cell.TitleLabel.text = self.Title[indexPath.row];
cell.AttributeLabel.text = self.Attribute[indexPath.row];
}
return cell;
}
Then in the DetailViewController.h:
#property (strong, nonatomic) IBOutlet UILabel *TitleLabel;
#property (nonatomic,strong) IBOutlet UILabel *AttributeLabel;
#property (nonatomic,strong) NSArray *DetailModal;
DetailViewController.m:
_TitleLabel.text = _DetailModal[0];
_AttributeLabel.text = _DetailModal[1];
My segue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ShowDetails"]) {
DetailViewController *detailviewcontroller = [segue destinationViewController];
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
int row = [myIndexPath row];
detailviewcontroller.DetailModal = #[_Title[row],_Attribute[row]];
}
}
I thought it would help to put more of my code so you're able to get clearer picture. In my non expert opinion, I think the reason your code doesn't work for me is because my data is being stored in an array (DetailModal) and not in a string.
Thank you for taking the time to help me with this
You should implement TableView delegate;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (tableView == self.searchDisplayController.searchResultsTableView) {
[self performSegueWithIdentifier:kSHOW_DETAILS sender:cell];
}
else {
[self performSegueWithIdentifier:kSHOW_DETAILS sender:cell];
}
}
Update:
You have to set the value to be displayed in the detailed view. You might declare a property in the header file for your detailView which you might set in prepareForSegue (or in the didSelectRowAtIndexPath above). For example, in DetailViewController you might do this:
#interface DetailViewController: UIViewController
#property (weak, nonatomic) NSString *myDetailValue;
#end
Then in tableView: didSelectRowAtIndexPath: above, set the correct value before you do the segue:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (tableView == self.searchDisplayController.searchResultsTableView) {
// TODO select value from filteredList to set the detailViewController, e.g.:
self.detailedViewController.myDetailValue = valueFromFilteredListForIndexPath;
[self performSegueWithIdentifier:kSHOW_DETAILS sender:cell];
}
else {
// TODO select value from regular list to set the detailViewController, e.g.:
self.detailedViewController.myDetailValue = valueFromRegularListForIndexPath;
[self performSegueWithIdentifier:kSHOW_DETAILS sender:cell];
}
}
Similarly, you can do it the prepare for segue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ShowDetails"]) {
DetailViewController *detailviewcontroller = [segue destinationViewController];
NSIndexPath *myIndexPath;
if (self.inSearch) {
myIndexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
detailedViewController.myDetailValue = valueFromFilteredListForIndexPath;
}
else {
myIndexPath = [self.tableView indexPathForSelectedRow];
detailedViewController.myDetailValue = valueFromRegularListForIndexPath;
}
}
}
I have a NSMutableString defined in my AppDelegate.h file.
//AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>{
#public
NSMutableString *nidForDetailDisplay;
}
Then I set this string in my ViewController.m file. I am storing a string (nidForDetailDisplay) to pass from a tableview to detailview.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
rowNumber = indexPath.row;
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
// this is where I set the string to a string of text
appDelegate->theNidForDetailDisplay = [arrayOfNids objectAtIndex:rowNumber];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIViewController *detailViewController;
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"detail"];
[self.navigationController pushViewController:detail animated:YES];
}
In ViewController, when I log theNidForDetailDisplay, I get the correct string.
Next, I try to access this updated NSMutableString (nidForDetailDisplay) in the subsequent DetailViewController
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSMutableString *storyid;
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate->theNidOnDetailDisplay = storyid;
}
However, when I print the string (storyid) or (theNidOnDetailDisplay) they both return null.
Am I missing something here? Should I be using instance variables?
Thanks in advance.
Don't use your AppDelegate to pass data. Add a property to the detail view controller and assign the value to the property.
on DetailViewController:
#property (nonatomic) NSMutableString * nid;
The assign it before you present:
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"detail"];
detail.nid = [arrayOfNids objectAtIndex:rowNumber];
[self.navigationController pushViewController:detail animated:YES];
I want to set up a textLabel and a detailTextLabel for my table. The textLabel is working properly. However, I couldnt get the detailTextLabel to display the text that I have set. Below is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"SomeString";
cell.detailTextLabel.text = #"hello";
return cell;
}
I have faced similar issue as like #user1829700.
My initial settings are
TableView --> Prototype Cell --> (Attribute Inspector) Style - set to
Custom (by design)
Changed Style to - Subtitle, it does the trick
Try this
Use appropriate UITableViewCellStyle with this (anything but UITableViewCellStyleDefault should thus work). The cell's style is specified when you initialize it.
In your custom table cell implementation try adding #synthesize detailTextLabel;
Header (.h):
#property (nonatomic, weak) IBOutlet UILabel *textLabel;
#property (nonatomic, weak) IBOutlet UILabel *detailTextLabel;
Implementation (.m):
#synthesize textLabel;
#synthesize detailTextLabel;
I do not really have much idea how to get this done but the idea is that in the first view controller, it will have an array of 6 items. After any of the table view cell is clicked, that particular cell will be removed and the next controller will display the remaining 5 items.
in the FirstController .m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
SecondController *sc = [sb instantiateViewControllerWithIdentifier:#"SecondController"];
filteredFive = [[NSMutableArray alloc] init];
// I only display two of it here as it will be very lengthy
if ([[totalItems objectAtIndex:indexPath.row] isEqual:#"ONE"]) {
[filteredFive addObject:#"TWO"];
[filteredFive addObject:#"THREE"];
[filteredFive addObject:#"FOUR"];
[filteredFive addObject:#"FIVE"];
[filteredFive addObject:#"SIX"];
}
if ([[totalItems objectAtIndex:indexPath.row] isEqual:#"TWO"]) {
[filteredFive addObject:#"ONE"];
[filteredFive addObject:#"THREE"];
[filteredFive addObject:#"FOUR"];
[filteredFive addObject:#"FIVE"];
[filteredFive addObject:#"SIX"];
}
[self.navigationController pushViewController:sc animated:NO];
// this is the part I am trying to transfer the array filteredFive into SecondController's array
sc.fiveItems = filteredFive;
Then at SecondController.h
#interface SecondController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *fiveItems;
}
#property (weak, nonatomic) IBOutlet UITableView *secondTable;
#property (nonatomic, retain) NSMutableArray *fiveItems;
#end
SecondController.m
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [fiveItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.textLabel.text = [fiveCourses objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
I am wondering if there is any errors in my codes that I have that disallow the data to be shown. Is it that I failed to transfer the array from one to another? Or am I missing some code thus the code is not showing in the SecondController?
If there is another way to display the 5 remaining items, I will be willing to try too.
I have TableViewController & DetailViewController.
I'd like to modify one label named 'label' in DetailViewController.
TableViewController.h :
#interface CodexTableView : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *listOfItems;
DetailViewController *vc;
}
#property (nonatomic, retain) DetailViewController *vc;
#property (strong, nonatomic) IBOutlet UITableView *myTable;
#end
And an extract of my TableViewController.m :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
vc.label.text = #"bonjour";
//UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//NSLog(#"%#",cell.textLabel.text);
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
dvController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
But it doesn't works.
Someone know the error ?
Please help me.
Thanks :)
You mix up two variables for your detail view controller. Remove the dvController one and only work with your vc property, like so:
vc = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
[[vc label] setText:#"bonjour"];
Make label a property of your detail view controller and have it point at your onscreen label.