Is this piece of code suppose to set the title on the ViewController I am connecting to?
The 2 UIViewControllers are connected via a push segue - the first one is embedded in a NavigationController.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"settingsSegue"])
{
self.navigationItem.title = [[NSString alloc] initWithFormat:#"Custom Title"];
}
}
It does not work for me, but the syntax is right.
Advance Thanks:-)
The above answer works for me with one exception...
Change
self.title = myTitle;
to
self.navigationItem.title = myTitle;
Set the title in the viewDidLoad of the destination viewcontroller using a property in the destination VC:
if ([[segue identifier] isEqualToString:#"settingsSegue"]) {
MyDestinationViewController *mdvc = segue.destinationViewController;
mdvc.myTitle = [[NSString alloc] initWithFormat:#"Custom Title"];
}
Then in the viewDidLoad event in MyDestinationViewController.h:
#property (nonatomic,strong) NSString *myTitle;
In MyDestinationViewController.m:
#synthesize myTitle;
And finally in viewDidLoad:
self.title = myTitle;
You can set the title directly in the segue as well, there is no need to go through a property:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"settingsSegue"]) {
segue.destinationViewController.navigationItem.title = #"Custom Title";
}
}
Or, what I needed, to set the title of the pushed view controller to the title of the table cell that was clicked:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"settingsSegue"]) {
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
UITableViewCell *cell = [self tableView:self.tableView cellForRowAtIndexPath:myIndexPath];
segue.destinationViewController.navigationItem.title = cell.textLabel.text;
}
}
Related
when I come the part of sending image from UICollectionViewController to another ViewController here is View A:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Detail Segue"])
{
PhotoDetailViewController *PhotoDetailVC = segue.destinationViewController;
NSIndexPath *path = [[self.collectionView indexPathsForSelectedItems] lastObject];
Photo *selectedPhoto = self.photos[path.row];
PhotoDetailVC.photo = selectedPhoto;
}
}
and I receive it in the ViewController (View B) using:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.imageView.image= self.photo.image;
}
I think the problem is that you didn't call performSegueWithIdentifier:sender: in any place in code which actually responsible for triggering navigation not prepareForSegue:sender: which is called when preparing for the navigation
so I suggest to implement collectionView:didSelectItemAtIndexPath: as the following
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
Photo *selectedPhoto = self.photos[path.row];
// this line is the one that is responsible for navigation
[self performSegueWithIdentifier:#"Detail Segue" sender:selectedPhoto];
}
and modify prepareForSegue:sender: as the following
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"Detail Segue"]) {
PhotoDetailViewController *PhotoDetailVC = segue.destinationViewController;
PhotoDetailVC.photo = sender;
}
}
i am new to ios programming. i have a static tableViewController in which i have 3 cells. i have another view controller where there is a label and description. what i am trying to do is detect the cell which user clicks and then change the label which is in my second view controller according to that but the problem is whenever i click the cell the program crashes and added the breakpoint
here is my code
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSString *name;
NSString *description;
if([[segue identifier] isEqualToString:#"PushAppDetailsFromCell1"] )
{
name = #"Label 1 ";
description = #"Long description of Label 1...";
}
else if([[segue identifier] isEqualToString:#"PushAppDetailsFromCell2"] )
{
name = #"Label 2";
description = #"Long description of Label 2...";
}
else if([[segue identifier] isEqualToString:#"PushAppDetailsFromCell3"] )
{
name = #"Label 3";
description = #"Long description of Label 3...";
}
else {
return;
}
AppDetailsViewController *apDetailsViewController =
segue.destinationViewController; //here i am getting the breakpoint
apDetailsViewController.appDetails =
[[AppDetails alloc] initWithName:name description:description];
}
AppDetails.m
-(id)initWithName:(NSString *)name description:(NSString *)descr{
self = [super init];
if(self){
self.name = name;
self.description = descr;
}
return self;
}
AppDetailsViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.nameLabel.text = self.appDetails.name;
self.descriptionLabel.text = self.appDetails.description;
}
Steps to figure out the problem
1) Put a break point in prepareForSegue
2) Try to see it displays correct segue id as it should be(there might be making spelling mistake).
3) see where it's crashing in
- In prepareForSegue?
- Is this calling initname()?
- has it started it viewDidLoad().
If you do this mostly you will figure out what the problem is. If you can not then let me know.
Bind segue from UIViewController to UIViewController rather then cell to UIViewController. Implement following code for navigation.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(#"%ld",aIntSelected);
NSLog(#"%#",segue.destinationViewController);
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
aIntSelected=indexPath.row;
NSLog(#"didSelectRowAtIndexPath called");
[self performSegueWithIdentifier:#"pushSecond" sender:self];
}
NSArray *names = #[#"Label 1", #"Label2", #"Label 3"];
NSArray *descs = #[#"Description 1", #"Description", #"Description 3"];
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"PushAppDetailsSegue"]) {
NSIndexPath *indexPath = [self.yourTableView indexPathForSelectedRow];
AppDetailsViewController *controller = segue.destinationViewController;
controller.appDetails = [[AppDetails alloc] initWithName:names[indexPath.row] description:descs[indexPath.row]];
}
}
Drag a segue from your ViewController to SecondViewController and name it "PushAppDetailsSegue".
Can someone tell me why my values are not being sent from a UIViewController to a UITableViewController?
In my UIViewController I have a number of buttons created programatically and each button has its own tag number set as below:
bySeaButton = [UIButton buttonWithType:UIButtonTypeCustom];
bySeaButton.tag = 4;
bySeaButton.frame = CGRectMake(20, 294, 280.f, 40.f);
UIImage *seaButton = [UIImage imageNamed:#"gettingHereBySeaButton.png"];
[bySeaButton setBackgroundImage:seaButton forState:UIControlStateNormal];
[self.view addSubview:bySeaButton];
[bySeaButton addTarget:self action:#selector(bySeaButtonClicked) forControlEvents:UIControlEventTouchUpInside];
Then in my prepareForSegue I set the tagVale in my UITableViewController to the buttons tag number as follows:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
MyTableViewController *vc = [[MyTableViewController alloc] init];
vc.tagNumber = bySeaButton.tag;
}
A NSLog here shows that the vc.tagNumber is set to the buttons tag number. However, when I access the value for the tagNumber in the UITableViewController, it is always set to 0. Below is how I use it (tagNumber is declared in .m of UITableViewController as #property (nonatomic, assign) int tagNumber;)
NSString *strFromInt = [NSString stringWithFormat:#"%d", self.tagNumber];
Output for this is always 0 - regardless of the buttons tag value
try this code in preparesegue
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
MyTableViewController *vc = segue.destinationViewController;
vc.tagNumber = bySeaButton.tag;
}
first declare segue id in storyboard ,after add this method in target
[self performSegueWithIdentifier:#"push segue id" sender:self];
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"push segue id"])
{
MyTableViewController *vc = [segue destinationViewControlle];
vc.tagNumber = bySeaButton.tag;
}
}
I have a UIViewController, and i have to pass a value (UIImage, NSString) to an UINavigationViewController, I am using
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
AppDetail *advc = [segue destinationViewController];
if ([[segue identifier] isEqualToString:#"showDetail"]) {
advc.appTitel = name;
advc.appIcon = icon;
advc.detailAppName = detileName;
advc.appDescription = description;
advc.appDeveloper = developer;
}
}
to pass the value to the UIViewController, it works perfectly if I pass it directly to a UIViewController, but I can't pass it to a UINavigationViewController,
Anyone know how I can do this?
I have this sample code from an Apple segue example. Perhaps it helps.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Room Create"]) {
// Prepare the settings view where the user inputs the 'serviceType' and local peer 'displayName'
UINavigationController *navController = segue.destinationViewController;
SettingsViewController *viewController = (SettingsViewController *)navController.topViewController;
viewController.delegate = self;
// Pass the existing properties (if any) so the user can edit them.
viewController.displayName = self.displayName;
viewController.serviceType = self.serviceType;
}
}
To pass value using Segue in your case with a UINavigationController try the code below :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"])
{
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
AppDetail *advc = (AppDetail *)navController.topViewController;
advc.appTitel = name;
advc.appIcon = icon;
advc.detailAppName = detileName;
advc.appDescription = description;
advc.appDeveloper = developer;
}
}
If you don't use a UINavigationController, you need to use this following code :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
// Get reference to the destination view controller
AppDetail *advcc = segue.destinationViewController;
advc.appTitel = name;
advc.appIcon = icon;
advc.detailAppName = detileName;
advc.appDescription = description;
}
}
I am just trying to transfer a simple string from a UILabel in a prototype cell into a label in the next View Controller. Value of label.text in the viewDidLoad of the View Controller is returning (null).
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
mainCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (mainCell == nil) {
mainCell = [[dictionaryTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString* date = [dateArray objectAtIndex:indexPath.row];
mainCell.viewLabel.text = date;
return mainCell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"View Segue"]) {
NSLog(#"View Load Segue Success");
ViewController *one = segue.destinationViewController;
one.label.text = mainCell.viewLabel.text;
}
}
What am I doing wrong here?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"View Segue"]) {
NSLog(#"View Load Segue Success");
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
ViewController *one = segue.destinationViewController;
one.label.text = [dateArray objectAtIndex:indexPath.row];
}
}
And actually, assigning text to text label you should do in your viewController one's method(viewDidLoad or viewWillAppear). So, you need to make a property in viewController one for transferring NSString.
You can use indexPathForSelectedRow:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"View Segue"]) {
ViewController *one = segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
one.textProperty = [dateArray objectAtIndex:indexPath.row];
}
}
Or you can also use sender if your segue is from the cell to the next scene, e.g.:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"View Segue"])
{
ViewController *one = segue.destinationViewController;
NSAssert([sender isKindOfClass:[UITableViewCell class]], #"Not cell");
UITableViewCell *cell = sender;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
one.textProperty = [dateArray objectAtIndex:indexPath.row];
}
}
Two things to note:
As a matter of good programming style, I am not retrieving the text value from the cell. I'm retrieving the text value from the model. You should not be relying upon the view for information to be passed along. Go back to the model, the original source of the information.
Do not set the text property of the label in the destination controller directly. The controls of the destinationController have not been created yet. You should defer setting controls until the destinationController's viewDidLoad. So, instead, create a NSString property in the destination controller:
#property (nonatomic, strong) NSString *textProperty;
Clearly, you should use a more descriptive name than textProperty, but hopefully you get the idea. Anyway, prepareForSegue can set this new property and the viewDidLoad of the destination controller should then use that NSString property to populate the text property of the UILabel, e.g.:
- (void)viewDidLoad
{
[super viewDidLoad];
self.label.text = self.textProperty;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
YourViewController *controller =[[YourViewController alloc] init];
[self presentModalViewController:controller animated:YES];
one.label.text = [dateArray objectAtIndex:indexPath.row];
}
Change the label.text after presentModalViewController. Now what happens?
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion
I understand you are already using Segue. You should follow the other answer.