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;
}
}
Related
I'm a beginner in Objective C and I have an issue between my Table View Controller and ViewController. When I click on the cell nothing happens, I don't go in my view controller to have more details about my cell.
I use a push segue and I don't forget to give it an identifier and to do this code part in my TableView:
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showDetails"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
ViewController *destViewController = segue.destinationViewController;
destViewController.details = data [indexPath.row];
}
}
My link between my two views is between the cell prototype and the ViewController window.
in didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"showDetails" sender:self];
}
in prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showDetails"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
ViewController *destViewController = segue.destinationViewController;
destViewController.details = data [indexPath.row];
}
}
I have a CollectionView in a viewcontroller. I named this viewController as TabViewController. Now in that collectionView I have some images. Now when I am tapping on the cells it is pushing to another viewController named as DetailViewController. In DetailViewController I have an imageView. I want to show the selected image in TabViewController in the imageView of DetailViewController.
But no images are coming. Can any one show me where I am wrong.
This is the code that I have done.
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
if (collectionView.tag == 6) {
[self performSegueWithIdentifier:#"detailController" sender:self];
_lastSelectedImage = [UIImage imageNamed:_myPatternString];
}
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
DetailViewController *detailObject = (DetailViewController *) segue.destinationViewController;
detailObject.selectedImageInPreviousVC = _lastSelectedImage;
}
and in DetailViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
_myImageView.image = _selectedImageInPreviousVC;
}
You could directly sent the image in performSegueWithIdentifier.
Use something like this:
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
if (collectionView.tag == 6) {
[self performSegueWithIdentifier:#"detailController" sender:[UIImage imageNamed:_myPatternString]];
}
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
DetailViewController *detailObject = (DetailViewController *) segue.destinationViewController;
detailObject.selectedImageInPreviousVC = sender;
}
Also make sure that detailObject.selectedImageInPreviousVC is not a weak property.
Try this:
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
if (collectionView.tag == 6) {
// Perform segue need to be called after _lastSelectedImage has been set
_lastSelectedImage = [UIImage imageNamed:_myPatternString];
[self performSegueWithIdentifier:#"detailController" sender:self];
}
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
DetailViewController *detailObject = [segue destinationViewController];
detailObject.selectedImageInPreviousVC = _lastSelectedImage;
}
I have two VCs and i want to pass resourceName from HomeViewController to SingleWebViewController. But the resourceName is getting null.
HomeViewController.m
#import "HomeViewController.h"
#import "SingleWebViewController.h"
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
singleWebViewController = segue.destinationViewController;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedRow = indexPath.row;
switch (selectedRow)
{
case 0:
{
singleWebViewController.resourceName=#"intro";
NSLog(#"HtmlFileName:%#" , singleWebViewController.resourceName);
[self performSegueWithIdentifier:#"toSingleWebView" sender:self];
break;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
SingleWebViewController.h has the following line
#property (nonatomic,strong)NSString *resourceName;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:self.resourceName ofType:#"html" inDirectory:nil] ;
NSLog(#"%#se:" , self.resourceName);
NSURL *url = [NSURL fileURLWithPath:htmlFile ];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
_webView.delegate=(id)self;
}
i did notice that didSelectRowAtIndexPath is getting called before prepareForSegue. what's the cause for this. Please suggest.
In prepareForSegue, you're assigning something new to singleWebViewController. If this controller is different from the controller that's already assigned in tableView: didSelectRowAtIndexPath:, which I assume it is, then your property will be reset.
Instead, do this:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
singleWebViewController = segue.destinationViewController;
singleWebViewController.resourceName = #"intro";
}
In your prepareForSegue you can set the property resourceName
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
SingleWebViewController *singleWebViewController = segue.destinationViewController;
[singleWebViewController setResourceName:#"Resource name"];
}
try this..pass value in prepareforsegue
Edit: if you want for different case to pass the different value then do this..i m assuming you want to segueway to same ViewController ie SingleWebViewController here.
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"toSingleWebView"]) {
SingleWebViewController *singleWebViewController = segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathOfSelectedRow];
if(indexpath.row == 0){
singleWebViewController.resourceName=#"intro";
}
else if(indexPath.row == 1){
singleWebViewController.resourceName=#"some other value";
}
else{
singleWebViewController.resourceName=#"something else";
}
NSLog(#"HtmlFileName:%#" , singleWebViewController.resourceName);
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedRow = indexPath.row;
switch (selectedRow)
{
case 0:
{
[self performSegueWithIdentifier:#"toSingleWebView" sender:self];
break;
}
case 1:
{
[self performSegueWithIdentifier:#"toSingleWebView" sender:self];
break;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
You're setting resourceName after viewDidLoad has been called. Add a custom initialiser, initWithResourceName:(NSString *)resourceName, and set it there. Or move the code from viewDidLoad to viewDidAppear:.
Try with:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
HomeViewController *obj = segue.destinationViewController;
if([segue.identifier isEqualToString:#"toSingleWebView"]){
obj.singleWebViewController = singleWebViewController;
}
}
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.
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;
}
}