I am solving this issue about 2 weeks and only be able to pass string, not image.
I store the image from camera or from library in this method.
-(IBAction)saveAction:(id)sender
{
Tricks *trick = [[Tricks alloc]init];
trick.trickName = self.trickLabel.text;
trick.trickPhoto = [[UIImageView alloc] initWithFrame:CGRectMake(0, 356, 320, 305)];
trick.trickPhoto.image = self.ImagePhoto.image;
[[Tricks trickList]addObject:trick];
}
In tableViewClass I store the values into properties of detailView
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"detailTrick"]){
NSIndexPath *indexPath = nil;
indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController *detailViewController = [segue destinationViewController];
Tricks *trick = [[Tricks trickList] objectAtIndex:indexPath.row];
detailViewController.trickPhoto = [[UIImageView alloc]initWithFrame:CGRectMake(0, 358, 200, 200)];
detailViewController.fileText = trick.trickName;
detailViewController.trickPhoto = trick.trickPhoto;
//object = [Tricks trickList][indexPath.row]
}
}
Text showed up every time without problems, but there is no image in detailViewController.Thanks for help.
viewDidLoad of detailViewController
[super viewDidLoad];
[self.detailButton setTitle:[NSString stringWithFormat:#"%#",_fileText] forState:UIControlStateNormal];
[self.trickPhoto setImage:_trickPhoto.image];
First off, trickPhoto within the Trick class should be a UIImage, not a UIImageView. Models shouldn't know anything about views such as frames, etc so right now you're violating the MVC design pattern.
Then it'd be:
- (IBAction)saveAction:(id)sender
{
Tricks *trick = [[Tricks alloc] init];
trick.trickName = self.trickLabel.text;
trick.trickPhoto = self.ImagePhoto.image;
[[Tricks trickList] addObject:trick];
}
The better way of doing this would be to create a Trick property within DetailViewController and just pass the whole trick into the view controller.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"detailTrick"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController *detailViewController = [segue destinationViewController];
Tricks *trick = [[Tricks trickList] objectAtIndex:indexPath.row];
NSLog(#"Make sure trick isn't nil: %#", trick);
detailViewController.trick = trick;
}
}
Then you'd just populate it with:
[super viewDidLoad];
[self.detailButton setTitle:[NSString stringWithFormat:#"%#", self.trick.trickName] forState:UIControlStateNormal];
[self.trickPhoto setImage:self.trick.trickPhoto];
Related
I know this question is asked many time , i had searched a lot and tried many solution but not worked. I have made a customize table view in which data is load from a service. The data load is quite limited , i have to show the detail of data into new view controller when user click on a cell. Its should pass data of the respective cell which carries data. I have tried segue technique to pass data to new vc but fails , its shows null in value which i'm passing. I have created some labels in new vc in which i'm calling the values from table view cell. My code is,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"ShowDetail"]) {
//Do something
ShowViewController *destViewController = segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
destViewController.tites = [_Title objectAtIndex:indexPath.row];
destViewController.prce = [_Price objectAtIndex:indexPath.row];
NSLog(#"PPPP is %#",destViewController.phon);
destViewController.ara = [_LandArea objectAtIndex:indexPath.row];
destViewController.phon = [_Phone objectAtIndex:indexPath.row];
destViewController.citi = [_City objectAtIndex:indexPath.row];
destViewController.loc = [_location objectAtIndex:indexPath.row];
destViewController.tye = [_type objectAtIndex:indexPath.row];
destViewController.Idss = [_Id objectAtIndex:indexPath.row];
destViewController.nam = [_name objectAtIndex:indexPath.row];
destViewController.emal = [_email objectAtIndex:indexPath.row];
destViewController.roomss = [_rooms objectAtIndex:indexPath.row];
destViewController.wash = [_washroom objectAtIndex:indexPath.row];
destViewController.flloors = [_floor objectAtIndex:indexPath.row];
destViewController.stat = [_status objectAtIndex:indexPath.row];
destViewController.descrp = [_descrip objectAtIndex:indexPath.row];
destViewController.countryy = [_country objectAtIndex:indexPath.row];
}
}
Issue in this question is that you are not populating the _Price and other arrays properly, so where you are populating _Title array , fill other arrays as well like _Price, _City
An outlet doesn't instantiate because an outlet is a variable (or property).
The objects in a nib are instantiated when that nib is loaded, and they are assigned to each outlet as immediately as possible afterward, after the objects are created but before awakeFromNib is sent to all relevant objects.
In your case you can pass data in ShowViewController and update the label in ShowViewController's viewDidLoad or viewDidAppear.
Define string in ShowViewController interface as
#property (strong, nonatomic) NSString * titesStr;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"ShowDetail"]) {
//Do something
ShowViewController *destViewController = segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
destViewController.titesStr = [_Title objectAtIndex:indexPath.row];
....
....
....
}
}
In ShowViewController viewDidAppear update your label as
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
tites.text = titesStr;
}
In place of passing data one by one you can use array also. Best implementation would be using model class.
EDIT
[self performSegueWithIdentifier:#"ShowDetail" sender:tableView];
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"ShowDetail"]) {
//Do something
UITableView *tv = (UITableView*)sender;
ShowViewController *destViewController = segue.destinationViewController;
NSIndexPath *indexPath = [tv indexPathForSelectedRow];
destViewController.tites = [_Title objectAtIndex:indexPath.row];
....
....
....
}
}
Make following changes in your code :
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"ShowDetail" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"ShowDetail"])
{
ShowViewController *destViewController =(ShowViewController *) segue.destinationViewController;
NSIndexPath *indexPath = (NSIndexPath *)sender;
destViewController.tites = [_Title objectAtIndex:indexPath.row];
destViewController.prce = [_Price objectAtIndex:indexPath.row];
// use this indexpath for segue
}
}
I want to pass PFObject from VC1 to VC2. I know thats easy if I pass PFObject from Tableview to VC. Anyway, I have A VC with Tableview with the segue to detailVC (1) with ImageView. This ImageView would be shown in detailiamgeviewcontroller after tap image.(2) And I want to send pfobject to this Controller. But have no idea how to.
Any idea?
(1)
if ([segue.identifier isEqualToString:#"showDetaill"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
PFObject *object = [self.objects objectAtIndex:indexPath.row];
DetailViewController2 *detailViewController = [segue destinationViewController];
detailViewController.objecteditem = object;
}
(2)
- (void)viewDidLoad
{
[super viewDidLoad];
[self.imageviewsecond setImageWithURL:[NSURL URLWithString:[self.objecteditem objectForKey:#"VlajkaURL"]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
UITapGestureRecognizer *singleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
singleTap.numberOfTapsRequired = 1;
[imageviewsecond addGestureRecognizer:singleTap];
}
- (void)handleSingleTap:(UIGestureRecognizer *)sender
{
[self performSegueWithIdentifier:#"sendi" sender:self];
NSLog(#"image tapped!!!");
}
Do the same as you are already doing. In view 1 you are sending the object to view 2 (DetailViewController2). In DetailViewController2, in the prepareForSegue method, you do the same:
if ([segue.identifier isEqualToString:#"sendi"])
{
DetailImageViewController *detailImageViewController = [segue destinationViewController];
detailImageViewController.somePropertyName = self.objecteditem;
}
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".
I am creating a program where if I click on thumbnail in collectionview, larger image should be opened in scroll view which is in another view collector. For that purpose i am using segue. but I am doing something wrong there, how can I solve this problem,
my code of segue is,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"])
{
NSIndexPath *selectedIndexPath = [[self.gallerycollection indexPathsForSelectedItems] objectAtIndex:0];
// load the image, to prevent it from being cached we use 'initWithContentsOfFile'
NSString *imageNameToLoad = [NSString stringWithFormat:#"interior_%d", selectedIndexPath.row];
NSString *pathToImage = [[NSBundle mainBundle] pathForResource:imageNameToLoad ofType:#"jpg"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToImage];
GalleryImageScrollViewController *gallerydetailViewController = [segue destinationViewController];
gallerydetailViewController.FullScreenImageScroller=image ;
}
}
detailViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *mutablearray = [NSMutableArray array];
data=[MyDatabase new];
slideImages=[data OpenMyDatabase:#"SELECT pic_name_big FROM interior":#"pic_name_big"];
[mutablearray addObjectsFromArray:slideImages];
temparr = [[NSMutableArray alloc]init];
temparr=[NSMutableArray arrayWithArray:mutablearray];
[self putImageViewsInScrollView:[temparr count]];
self.FullScreenImageScroller.delegate=self;
}
-(void) putImageViewsInScrollView:(int)numberOfImageViews
{
for(int i=0 ;i<numberOfImageViews; i++)
{
fullScreenImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[temparr objectAtIndex:i]]];
fullScreenImageView.frame = CGRectMake((WIDTH_OF_IMAGE * i) , 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[self.FullScreenImageScroller addSubview:fullScreenImageView];
}
[self.FullScreenImageScroller setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE * ([temparr count]), HEIGHT_OF_IMAGE)];
[self.FullScreenImageScroller setContentOffset:CGPointMake(0, 0)];
[self.FullScreenImageScroller scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
}
then what should I pass in scrollview controller (GalleryImageScrollViewController) to open image of specific thumbnail ?
Pass the image name to the detail view controller like this
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSArray *indexpaths = [self.collectionView indexPathsForSelectedItems];
DetailViewController *dest = [segue destinationViewController];
dest.imageName = [recipeImages objectAtIndex:[[indexpaths objectAtIndex:0] row]];
// imageName is a property of detail view controller
}
Then in the viewDidLoad of detailViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageView.image = [UIImage imageNamed:self.imageName];
}
Im not using scroll view, only an imageView added as a sub view to the view
Its pretty simple. Just pass the index of selected thumbnail image to GalleryImageScrollViewController.(from answers given above..)
Then, In the viewDidLoad method of detailViewController populate image in imageView using the fetched index.
In the viewDidLoad of detailViewController
-(void)viewDidLoad
{
[super viewDidLoad];
self.imageView.image = [UIImage imageNamed:[[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"interior_%d", indexOfThatImage] ofType:#"png"]]];
}
Please note that file extension is correct in that of your bundle.
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;
}
}