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
Related
I've a table view with some rows and each row has its Detail Disclosure button.
I want that when the user taps on that button, another table view (customized by storyboard) appears and shows some relative data.
From TableViewController.m
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailViewController = [[detailViewController alloc] init];
//creating a parse object
PFObject *checkin = [_dataSourceArray objectAtIndex:indexPath.row];
//getting data i want
NSString *trainNumb = [checkin objectForKey:#"trainNumber"];
//passing the data
detailViewController.trainNumber = trainNumb;
}
From the DetailTableViewController.m
- (void)viewDidLoad {
[_trainNumberLabel setText:_trainNumber];
}
The problem is that in the DetailTableView the NSString results to be null.
What I'm missing here? Thank you!
If this code actually compiles, this may be your problem:
DetailViewController *detailViewController = [[detailViewController alloc] init];
It should be:
DetailViewController *detailViewController = [[DetailViewController alloc] init];
But you shouldn't be alloc/initing your view controller in the first place. When you customize a view controller in Interface Builder, you've got to instantiate it like this if you want those customizations:
[self.storyboard instantiateViewControllerWithIdentifier:#"MyStoryboardIdentifier"]`
And you've got to set the controller's storyboard ID to "MyStoryboardIdentifier" or whatever identifier you want to use.
Also, as you've indicated in the comments, you've got a timing issue: your detail controller's viewDidLoad runs before you set the train number. A better approach would be to ensure that it works regardless of the sequence:
- (void)updateTrainNumberLabel
{
self.trainNumberLabel.text = self.trainNumber;
}
- (void)setTrainNumber:(NSString *)trainNumber
{
_trainNumber = trainNumber;
[self updateTrainNumberLabel];
}
- (void)viewDidLoad
{
...
[self updateTrainNumberLabel];
}
In other words, you configure your label in a separate method, in this case updateTrainNumberLabel, and when anything that could affect the label happens, e.g. the view loading or the number being changed, you call the method.
I'm working on master detail app and i would like to view different detailView of selected row. and to be my question clear i give you example here:
Master Detail
detail 1 >
detail 2 >
the target is when i press [detail 1 >] give me self detailview of detail 1 row. and press [detail 2] give self detailview of detail 2 row. i think it's clear. also add customize buttons, images inside every detail view if possible!!
any help will be appreciated.
DetailViewController *detailView = [[DetailViewController alloc]init];
detailView.tagValue = 1;
[self.navigationController pushViewController:detailView animated:YES];
You can send a tag value to detailView and based on this value you can show different views.
Like make a property like #property(nonatomic)NSInteger tagValue; in .h class of detailView and synthesize it in .m class. Now you can send the tagValue from master Class like this
Now you can check value of tagValue in DetailViewController and based on this you can create your View.
I think it is clear now. But if you still have any issue, you can write your code.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailView = [[DetailViewController alloc]init];
detailView.tagValue = indexPath.row;
[self.navigationController pushViewController:detailView animated:YES];
}
we need to give detailView.tagValue = indexPath.row;
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.
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 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;