I have a detail view controller that is hooked up to a table view cell. I have a data model. It is an NSObject and has a NSMutableArray will all my properties I need in them.
I hooked up a label to an #property and named it questionLabel. For some reason, when I push over to the detail view, it does not display the question I put in the data model for the NSMutableArray.
At first I thought the segue was not working, but when I tried to change the navigation bar title, it worked when I passed my data from my NSMutableArray.
Anyone know what I am doing wrong with my label?
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"pushDetail"])
{
// Create an instance of our DetailViewController
DetailQuestionViewController *DQV = [[DetailQuestionViewController alloc] init];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
// Setting DQV to the destinationViewController of the seque
DQV = [segue destinationViewController];
Question *selectedQuestion = [self.myQuestionList questionAtIndex:path.row];
DQV.questionLabel.text = [selectedQuestion questionName];
DQV.navigationItem.title = [selectedQuestion questionRowName];
}
}
UPDATE (For: adambinsz)
Table view
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"pushDetail"])
{
// Create an instance of our DetailViewController
DetailQuestionViewController *DQV = [[DetailQuestionViewController alloc] init];
// Setting DQV to the destinationViewController of the seque
DQV = [segue destinationViewController];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Question *selectedQuestion = [self.myQuestionList questionAtIndex:path.row];
DQV.detailItem = selectedQuestion;
}
}
detailView
- (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) {
Question *theQuestion = (Question *)self.detailItem;
self.questionLabel.text = theQuestion.questionName;
NSLog(#"The Question's questionName is %#", theQuestion.questionName);
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
The reason I think it may not be working, is because the if statement for self.detailItem is not checking it correctly.
The method gets called just not the detailItem.
Your label probably hasn't been created yet when you try to set its text. I would create a selectedQuestion instance variable on DetailQuestionViewController and set it when you create the view controller. Something like this:
// Create an instance of our DetailViewController
DetailQuestionViewController *DQV = [[DetailQuestionViewController alloc] init];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
// Setting DQV to the destinationViewController of the seque
DQV = [segue destinationViewController];
DQV.selectedQuestion = [self.myQuestionList questionAtIndex:path.row];
And in DetailQuestionViewController's viewDidLoad method, set your label's text by using:
self.questionLabel.text = [selectedQuestion questionName];
self.navigationItem.title = [selectedQuestion questionRowName];
Related
In my application I have oneUITableViewController (Uctovatrida0TableViewController) and two otherUIViewContoller (DetailViewController and examples).
Now I have a problem because I do not know how to identify index (from indexPath.row) in my firstUIViewController to then correctly referred to another UIViewController.
The code in my Uctovatrida0TableViewController.m (works well):
-(void) showDetailsForIndexPath:(NSIndexPath*)indexPath
{
[self.searchBar resignFirstResponder];
DetailViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailsViewController"];
Ucty* ucet;
if(isFiltered)
{
ucet = [_filteredTableData objectAtIndex:indexPath.row];
}
else
{
ucet = [self.alldataArray objectAtIndex:indexPath.row];
}
vc.uctyItem = ucet;
[self.navigationController pushViewController:vc animated:true];
}
Code in my class DetailViewConroller where I need indexPath from Uctovatrida0TableViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"Priklad"])
{
Priklad *vc = [segue destinationViewController];
Ucty* ucet;
//of course this does not work
ucet = [self.alldataArray objectAtIndex:NSIndexPath.row];
vc.uctyItem = ucet;
[self.navigationController pushViewController:vc animated:true];
}
}
Code in my class Priklad.m:
- (void)setDetailItem:(id)newDetailItem
{
if (self.uctyItem != newDetailItem)
{
self.uctyItem = newDetailItem;
[self configureView];
}
}
- (void)configureView
{
if (self.uctyItem ) {
self.prikladLabel.text = self.uctyItem.priklad;
self.uctykprikladu.text = self.uctyItem.uctykprikladu;
}
}
From your code, it seems DetailViewConroller has a property uctyItem. So in your DetaiViewController's prepareForSegue:sender: method, you can just call vc.uctyItem = self.uctyItem;.
Add a property to DetailViewConroller that you can set before pushing to it.
#property (assign, nonatomic) int index;
If you want to get selected cell indexPath then there is a method for that is : selectedIndexPath = [tableView indexPathForSelectedRow];
I'm fairly new to Objective-C and have encountered this following issue.
I've setup the storyboard and the linked up the view controllers accordingly. Also, setup the segue. However, for some weird reason, self.tableView indexPathForSelectedRow is returning me nil.
Really appreciate for the help in advanced!
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if([[segue identifier] isEqualToString:#"infoTherapyCloseCase"])
{
infoTherapyPhaseCloseCaseViewController *info = [segue destinationViewController];
//info = [segue destinationViewController];
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
int row = [myIndexPath row];
TherapyPhases *tp = _DetailModal[row];
info.DetailModal = #[tp];
}
}
I have a static table view with 2 cells (in 2 groups).
I have a custom cell (xib) with picture and some data inside.
I've declared through storyboard the cell to be a type of my custom cell and I have a function in my view that sets the photo of the cell and it's data. The problem is that the cell stays empty. I even tried reload data to the table. Whatam I missing? (I'm new to storyboars and viewes)
the code i'm using:
#interface CAUploadDetailsViewController ()
#property (weak, nonatomic) IBOutlet CAContentCell *currentUploadedFile;
#property (strong, nonatomic) IBOutlet UITableView *table;
#end
#implementation CAUploadDetailsViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// self.tableView.delegate = self;
CAContent* content = [[CAContent new] init];
content.name = #"file.txt";
content.sizeInBytes = 4444;
self.currentUploadedFile.item = content;
// Do any additional setup after loading the view.
// self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"ExplorerBackground3"]];
[self.tableView reloadData];
}
- (void)viewWillAppear:(BOOL)animated
{
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
when i set the item, it has the following code:
- (void)setItem:(id)item
{
_item = item;
// Cast content
CAContent* content = (CAContent *)item;
self.labelTitle.text = content.name;
NSString* icon = #"MyIcon";
// Set icon image
self.imageIcon.image = [UIImage imageNamed:icon];
}
=== UPDATE (25/1/15)===
i've managed to solve it using cellForRowAtIndexPath, but it feels that this solution is a bit weird (i don't like the code) - is this is how custom cells should be handled? because i need only 1 cell to be custom (i've got 2 cells in my table)
- (void)viewDidLoad
{
[super viewDidLoad];
// self.tableView.delegate = self;
// Customize background
self.view.layer.contents = (id)[UIImage imageNamed:#"ExplorerBackground3"].CGImage;
// Register cell nib
[self.table registerNib:[UINib nibWithNibName:#"CAContentCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:#"CAContentCell"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// we should configure only the first cell.
if (indexPath.section == 0 && indexPath.row == 0)
{
CAContentCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CAContentCell"];
if (cell == nil) {
cell = [[NSBundle mainBundle] loadNibNamed:#"CAContentCell"
owner:self
options:nil][0];
}
CAContent* content = [[CAContent new] init];
content.name = #"file.txt";
content.sizeInBytes = 4444;
cell.item = content;
return cell;
}
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
I use segues for both push on cell and detail disclosure button.
I can't tap on indices, because detail disclosure button "intercepts" touch instead.
Code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue
sender:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
if ([segue.identifier isEqualToString:#"Dossier Segue"]) {
Person *person = [[self fetchedResultsController] objectAtIndexPath:indexPath];
DSRevealViewController *vc = (DSRevealViewController *)segue.destinationViewController;
vc.person = person;
vc.transitioningDelegate = self;
}
else if ([segue.identifier isEqualToString:#"Info"]) {
Person *person = [[self fetchedResultsController] objectAtIndexPath:indexPath];
NSAssert(person, #"Person is nil");
PersonInfoViewController *vc = (PersonInfoViewController *)segue.destinationViewController;
vc.person = person;
}
}
P.S. I checked my other app where I use table view delegate methods instead of segues, and it works well.
I am trying to change the color of a label in my detail view based on which cell is touched in a table view.
I just can't make this happen. Below is my code. I have included the header file from the detail view, and created an IBOutlet for the label.
Update: Ideally, when I clicked on a cell, the label color in detailview should be red. And here is a screencast about my problem. http://www.youtube.com/watch?v=i0nhbsNJWHY&feature=youtube_gdata_player
#pragma mark - TableView Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"ReferenceDetail" sender:tableView];
}
#pragma mark - Segue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ReferenceDetail"]) {
ReferenceDetailViewController *referenceDetailViewController = [segue destinationViewController];
UIViewController *referenceDetailView = [segue destinationViewController];
if(sender == self.searchDisplayController.searchResultsTableView) {
NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
NSString *destinationTitle = [[filteredReferenceArray objectAtIndex:[indexPath row]] name];
[referenceDetailView setTitle:destinationTitle];
}
else {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *destinationTitle = [[referenceArray objectAtIndex:[indexPath row]] name];
[referenceDetailView setTitle:destinationTitle];
[referenceDetailViewController.label1 setTextColor:[UIColor redColor]];
}
}
}
The problem you are having is that the outlet UILabel inside your ReferenceDetailViewController has not loaded up from the xib, yet you are trying to set it's textColor property.
As a way to make sure this is the problem, try setting a breakpoint in the line where you set the color and another breakpoint inside the ReferenceDetailViewController [viewDidLoad] method. The line where you setup the color should be getting called first.
A simple solution to your problem is to have a property on your ReferenceDetailViewController class that "holds" the UIColor instance and later set it on the viewDidLoad method:
// ...
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ReferenceDetail"]) {
ReferenceDetailViewController *referenceDetailViewController = [segue destinationViewController];
UIViewController *referenceDetailView = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *destinationTitle = [[referenceArray objectAtIndex:[indexPath row]] name];
[referenceDetailView setTitle:destinationTitle];
referenceDetailViewController.auxTextColor = [UIColor redColor];
}
// ..
And in ReferenceDetailViewController:
#interface ReferenceDetailViewController : UIViewController
#property (retain, nonatomic) UIColor *auxTextColor;
// ...
#end
#implementation ReferenceDetailViewController
- (void)viewDidLoad
{
[super viewDidLoad];
label1.textColor = self.auxTextColor;
// ...
}
#end
If you must do this for a set of labels, it'd be wise to have a NSDictionary object to hold these properties for you.
The outlets are not setup yet when you segue so you can't get to them to change them. Try sending some object or something over and in the viewdidload of the viewcontroller you segue to you read that and then use that as the way to know what color to change to and then in that same code you change the color of the label.