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.
Related
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.
How would I be able to pass the name of a table cell that is clicked in a Master View Controller to a Detailed View Controller. I would like it to be used later in my detailed view right now though I would like to use this data for the detailed view's navigation item title by means of self.navigationItem.title =. Can anyone please provide basic code to how to make this happen?
Your going to want to use the didselectrowatindexpath method. Something like this, initialize the new view and set the title:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *myDetViewCont = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
NSInteger row = indexPath.row;
NSString *title = [ListOfItems objectAtIndex:row];
myDetViewCont.navigationItem.title = title;
[self.navigationController pushViewController:myDetViewCont animated:YES];
}
If you only want to set the navigation title, you can just set it before you push the detailViewController in navigation controllers' stack.
detailViewController.title = #"whatever you want";
If you do need the name of the cell for other places, then make a NSString property in DetailViewController, and set it before pushing.
The easy way is to create a property in the detailed view controller and set it to the title. Then you can use in the ViewDidLoad to set the title.
psuedo code:
create detailed view controller
set property
push it on the navigation bar
grab property in ViewDidLoad
I have a UIPopoverController that has a UITableView that is populated. I am trying to pass the information from the popovercontroller to the superview. The tableview that is placed in the popovercontroller is receiving the information with the didSelectRow method but the information is not transferring to the textView placed in the superView.
Below is the code I use to try and place the selected user in the textview and underneath that is the Superview code where I try to take the string and place it into the actual field when the index is clicked. I know my issue is I am not telling my superView that the row was touched and to send that information into the field but I have searched and con not find a good explanation on how to attempt this.
If anyone has any suggestions that would be great!
thanks
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath {
if (indexPath){
NSString *selectedUser = [(Tweet*)[Friend objectAtIndex:indexPath.row]
followingScreenName];
TwitterFollowersTimline *textBox = [[TwitterFollowersTimline alloc]
initWithNibName:#"TwitterFollowersTimeline" bundle:[NSBundle mainBundle]];
textBox.selectedFriend = selectedUser;
NSLog(#"%#", selectedUser);
[textBox release];
textBox = nil;
}
}
In my SuperView
selectedFriend = [[NSString alloc] init];
creatTweetText.text = [NSString stringWithFormat:#"#%#", selectedFriend];
In order to pass information from the View Controller of the popover you're going to want to set up a delegate. Here's a link to Apple's documentation and also a good guide example.
https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html
http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html
Essentially when you create the View Controller that houses the methods for your table view, you'll want to give it a delegate. Then once a cell is selected in your table view, you'll be able to send a call to your delegate with any information you need.
i have an application where a there is a root view controller which pushes another a view controller into view. this view controller shows a table. when a cell is selected, it pushes to another view controller.
The first push works, but the second doesn't.
Here is my code for the selection of the cell in the table:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
TwoViewController *two = [[TwoViewController alloc] initWithNibName:#"TwoViewController": bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:two animated:YES];
[two release];
Any idea what i've done wrong? the codes I've used for both pushes are nearly identical...
I've tried this with putting this code in the touchUpInside event for a UIButton as well, but it does not work.
My guess is that there is no navigationController to push the view, but I don't know.
Please help!
Did you change the -initWithNibName:bundle method on TwoViewController? My guess is it doesn't initialize correctly (i.e.: the method doesn't initialize the super class correctly).
Edit: I think I found the error, it's kind of obvious, actually.
Please check this code you posted:
initWithNibName:#"TwoViewController": bundle:[NSBundle mainBundle]];
The error is near the #"TwoViewController": string, an extra semicolon that should not be there, change it to:
initWithNibName:#"TwoViewController" bundle:[NSBundle mainBundle]];
Log both 'two' and self.navigationController,
NSLog(#"two = %#", two);
NSLog(#"self.navigationController = %#", self.navigationController);
First you'll get to know, if one of them is nil (or both) and whether tableView:didSelectRowAtIndexPath: gets called at all.
Its navigation controller problem.Check it properly
If the new view controller is part of a view, it probably doesn't have a navigation controller. Check if navigation controller is nil before pushing.
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;