Cant pass value to another class - iphone

i have navigation controller and i want to change label text of next controller before view is loaded. There is my code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
ProductViewController *product = [[ProductViewController alloc]init];
product.myDescription.text = [[self.listOfItems objectAtIndex:indexPath.row] objectForKey:#"description"];
[self.navigationController pushViewController:product animated:YES];
}
I did try product.myDescription.text = #"123"; But it still not working.. i have no idea why, maybe i missing something? myDescription is connected UILabel of my second controller.

myDescription doesn't exist when you try to use it because the view hasn't been loaded yet. You can ensure the view is loaded by requesting it (product.view) though doing this is generally a little strange (because you aren't going to use the view). A better (but more code) approach is to pass the values required and have the controller set them when the view is loaded.

Related

Blank detail view pushed from a TableView that was created programmatically

My setup is using a storyboard in which I create a login, then a home screen in which the user can press a button to display their messages.
These are displayed in a table which is instantiated programmatically.
From this table I then want to press a row to go into detail about that row, but when i do this the view displayed is blank but all methods associated with the class are being fired (view did load, and so on).
I have literally tried 10 different ways from different solutions others had had suggested on their questions but nothing works.
Code to make new view and push it:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
messageDetail *messageDetailView = [[messageDetail alloc]initWithNibName:nil bundle:nil];
messageDetailView.message = [entityObjects objectAtIndex:indexPath.row];
[self.navigationController pushViewController:messageDetailView animated:YES];
}
ViewDidLoad of DetailView:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"Message Detail:\n%#",message);
messageTitle.text = message.message_title;
messageBody.text = message.message_xml;
}
I have tried using a segueIdentifier route and then intercepting the prepareSegue: to add data to the new view, but as the tableView is created programmatically there is no segue.
I have tried instantiating the view from storyboard via:
messageDetailView = [self.storyboard instantiateViewControllerWithIdentifier:#"MDV"];
But the app crashes as there is no view with identifier, even though i set the identifiers.
You send nil as nib name, if you do not have a nib file do not allocate the message detail view with initWithNibName function.
Use raw init or initWithFrame. Whichever works for you.
Good luck
I basically did the same thing with this code here. Hope this helps
Detail_View *next=[[Detail_View alloc] initWithNibName:#"Detail_View" bundle:nil];
//These are just values that I needed to set before going to the next page.
//You can use this if you want to pass over the value of your table row
//otherwise ignore this
next.htmlContents= [[storage_array objectAtIndex:indexPath.row]objectForKey:#"title"];
next.titleText=[[storage_array objectAtIndex:indexPath.row] objectForKey:#"title"];
[self.navigationController pushViewController:next animated:YES];

Error from NSString to UILabel xcode

I have a error in my script, it doesn't works. but I don't know how to change it.
I'm using 2 views, I'd like to use if/else for changing the text on the favoriteColorLabel on the second view from the first.
If someone know the problem, please help me.
Th
My code :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row==1) {
DetailVC.favoriteColorLabel=#"Bonjour";
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
dvController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:dvController animated:YES];
}
}
Thanks.
I think the error is on this line:
DetailVC.favoriteColorLabel=#"Bonjour";
It is most likely you actually want to set the text attribute of your label like this:
DetailVC.favoriteColorLabel.text=#"Bonjour";
This is because the text attribute is the text displayed by the label onscreen. You were setting the actual UILabel object to an NSString literal, which is probably not what you wanted to do.
You are setting Text to a label Thats why you are getting error in your code.
You need to do it as like this:
labelname.text = #"String";
Then your issue will resolve.
Hope it works.
I would consider setting a label in the second view from a method in the first bad design and error prone.
What I would do, is give your detail view controller a property that is an NSString *. After you create the detail view controller from your first controller, you pass the text #"Bonjour" to that property (by using its setter method) and then you can present the detail view controller. This second view controller can look at the value of its string and set the label accordingly.
The detail view controller is there to manage its own views, it should not need you first view controller to manage what's onscreen.

Passing a string to a modal view

I am trying to grab the string off an object in a table row when it is clicked. I have seen tutorials about getting the string and passing it to synthesized label however I am unable to pass the same string to a synthesized string in my modal view. Is this possible? My code in the parent view is as follows.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Modules *selectedModule;
selectedModule = [fixedArray objectAtIndex:indexPath.row];
NSString *moduleComponent = [selectedModule valueForKey:#"name"];
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"Detail"];
[self.navigationController pushViewController:detail animated: YES];
detail.number.text = moduleComponent;
detail.moduleLabel.text = moduleComponent;
detail.module = moduleComponent;
}
The detail.moduleLabel.text displays correctly however when I try to alert or draw a label using the detail.module I receive null. Any help is greatly appreciated.
Move [self.navigationController pushViewController:detail animated: YES]; after assigning the value to the detail controller.
EDIT:
Another point to note is when exactly do you try to alert it - viewDidLoad, viewWillAppear, viewDidAppear. Try in the different methods to see what the result is. I apologize but I am unable to test it now and give you the result itself, but have experienced similar behavior (not assigned value) depending on the specific event (viewDidLoad, viewWillAppear, viewDidAppear) where I try to access the value.

How do I correctly set a UIViewController's navigationController title?

I am trying the following code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Thing *sub = [[subscriptions objectAtIndex:indexPath.row] retain];
StoriesViewController *thing = [[StoriesViewController alloc] initWithThing:sub];
thing.navigationController.title = sub.title;
[self.navigationController pushViewController:thing animated:YES];
[thing release];
[sub release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
I thought this is how you correctly set the title for pushing the controller. I tried thing.title however that was setting the TabBarItem's title instead.
thing.navigationItem.title = sub.title;
or in StoriesViewController.m file in viewDidLoad method:
self.navigationItem.title = sub.title
It's possible you're pushing a UITabBarController instead of your regular view controller. In this case, the navigationItem will display the title of the current controller, the tab bar controller, even though you see another view. You would have to change the tab bar controller's title to change the text displayed above.
self.tabBarController.title = #"Your title";
The title needs to be set in (or after) viewDidLoad, so if you want to set from another class that is creating StoriesViewController.
Make another property and have that class set it.
In StoriesViewController's viewDidLoad, set the title from that property.
This is because outlets aren't initialized to their view counterparts until viewDidLoad.
It looks like StoriesViewController is holding on to sub (does the initWithThing set a property to it?) If so, just set the title in viewDidLoad to the Thing property's title.
[thing setTitle: sub.title];
The solution was thing.navigationItem.title = #"title"; however it turns out that subclassing a UINavigationController broke it somehow and my solution to that was to use a category rather than a subclass
If your UIViewController is part of a UITabController, then you can do:
self.tabBarController.title = sub.title;

Need help getting parent reference to child view controller

I've got the following code in one of my view controllers:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case 0: // "days" section tapped
{ DayPicker *dayPicker = [[DayPicker alloc] initWithStyle:UITableViewStylePlain];
dayPicker.rowLabel = self.activeDaysLabel;
dayPicker.selectedDays = self.newRule.activeDays;
[self.navigationController pushViewController:dayPicker animated:YES];
[dayPicker release];
break;
...
Then, in the DayPicker controller, I do some stuff to the dayPicker.rowLabel property. Now, when the dayPicker is dismissed, I want the value in dayPicker.rowLabel to be used as the cell.textLabel.text property in the cell that called the controller in the first place (i.e., the cell label becomes the option that was selected within the DayPicker controller).
I thought that by using the assignment operator to set dayPicker.rowLabel = self.activeDaysLabel, the two pointed to the same object in memory, and that upon dismissing the DayPicker, my first view controller, which uses self.activeDaysLabel as the cell.textLabel.text property for the cell in question, would automagically pick up the new value of the object. But no such luck.
Have I missed something basic here, or am I going about this the wrong way? I originally passed a reference to the calling view controller to the child view controller, but several here told me that was likely to cause problems, being a circular reference. That setup worked, though; now I'm not sure how to accomplish the same thing "the right way."
As usual, thanks in advance for your help.
Turns out I needed to add a call to
[self.tableView reloadData];
in the viewWillAppear method to get the table to read the new value.