Creating many ViewControllers with different content and moving between them - iphone

Im having a ViewController in a storyboard that presents images, i want to have "next" and "prev" buttons to move through instances(10 viewControllers) of the same class that contains different images.
The "first" instance is part of a storyboard, i programmed manually all the images that are subview of a scrollbar. Now the question is how to add properly manual ViewControllers
How do i approach it? Ive been thinking and since only the images change should i make a instance method like "initWithImageArray" ?
Should i create a MutableArray of the class instances ?
If so , is there a way to know which instance of the array i am ? so i would know when to "stop" and not display "next" button or "prev" button
VCDecorations *page1 = [self.storyboard instantiateViewControllerWithIdentifier:#"VCDecorations"];
VCDecorations *page2 = [self.storyboard instantiateViewControllerWithIdentifier:#"VCDecorations"];
VCDecorations *page3 = [self.storyboard instantiateViewControllerWithIdentifier:#"VCDecorations"];
DecoViewControllers = [[NSArray alloc]initWithObjects:page1,page2,page3, nil];
Is there away to know if i"m currently in "page1" or "page2" ?
Thank you.

Instead of using different viewControllers use single viewController and add all images in a an array and when next or Previous button is tapped change the image in imageView according to your array.

Try this Example:
self.imageArray = [NSArray arrayWithObjects:#"image1.png",#"image2.png"],#"image3.png"],#"image4.png"],#"image5.png"],#"image6.png"],nil];
self.currentIndex = 0;
-(IBAction)nextImageClicked:(id)sender{
if (self.currentIndex < self.imageArray.count-1) {
self.currentIndex++;
NSString *imageName = [NSString stringWithFormat:#"%#",[self.imageArray objectAtIndex:self.currentIndex]];
UIImage * image = [UIImage imageNamed:imageName];
self.imageView.image = image;
}
}
-(IBAction)prevImageClicked:(id)sender{
if (self.currentIndex > 0) {
self.currentIndex--;
NSString *imageName = [NSString stringWithFormat:#"%#",[self.imageArray objectAtIndex:self.currentIndex]];
UIImage * image = [UIImage imageNamed:imageName];
self.imageView.image = image;
}
}

Related

Change the animation images of a UIImageView

In my viewDidLoad method of my iPhone app I have the following code:
zombie[i].animationImages = zombieImages;
zombie[i].animationDuration = 0.8/zombieSpeed[i];
zombie[i].animationRepeatCount = -1;
[zombie[i] startAnimating];
Later on in the app the following code is called:
[zombie[i] stopAnimating];
zombie[i] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"zh.png"]];
zombie[i].animationImages = flyingZombieImages;
zombie[i].animationDuration = 0.8/zombieSpeed[i];
zombie[i].animationRepeatCount = -1;
[zombie[i] startAnimating];
This causes the app to crash, with EXC_BAD_ACCESS on the line zombie[i].animationImages = flyingZombieImages;
flyingZombieImages is initialized with the following code: (zombieImages is initialized the same way)
NSMutableArray *flyingZombieImages = [NSMutableArray array];
for (NSUInteger i=1; i <= 29; i++) {
NSString *imageName = [NSString stringWithFormat:#"flzom%d.png", i];
[flyingZombieImages addObject:[UIImage imageNamed:imageName]];
}
Why is this happening? Is there a workaround?
As Dima mentioned flyingZombieImages is likely not initialized properly, which is causing the crash. However, there is another problem as well when you create the new instances of UIImageView:
zombie[i] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"zh.png"]];
At this point you already have a reference to the old UIImageView stored in this variable. You are losing the reference to it and most likely will leak its memory. You also would want to remove the old UIImageView from the view hierarchy and add the new one.
A better way is instead to use the original UIImageView and change its image by replacing this line with:
zombie[i].image = [UIImage imageNamed:#"zh.png"];

how to load few images from URL individually to scroll view ? objective-c

i have this scroll view, that I'm loading into him few images from URL.
the problem is that the scroll view don't show any of them until that all loaded.
i want to show every image the moment i finished loading her.
my code looks like this:
-(void)loadPhotosToLeftscroll{
for (int count = 0 ; count < [leftPhotoArray count]; count++) {
NSLog(#"nextPhotoHight: %f",nextLeftPhotoHight);
NSMutableDictionary *photoDict;
photoDict = [leftPhotoArray objectAtIndex:count];
float photoHight = [[photoDict objectForKey:#"photos_height"]floatValue];
float photoWide= [[photoDict objectForKey:#"photos_width"]floatValue];
NSString *photoPath = [photoDict objectForKey:#"photos_path"];
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:photoPath]];
UIImage *image = [[UIImage alloc] initWithData:imageData];
UIImageView *photoView = [[UIImageView alloc]initWithFrame:CGRectMake(10 , nextLeftPhotoHight, photoWide, photoHight )];
[photoView setImage:image];
[photoView.layer setMasksToBounds:YES];
[photoView.layer setCornerRadius:6];
nextLeftPhotoHight = photoView.frame.size.height + photoView.frame.origin.y + 10;
[leftBlockScroll addSubview:photoView];
}
}
You better use a asynchronous way to do that.
Relative topic is NSURLConnection, UIImageView.
I have done something similar before.
1. Create a new model inherit to UIView
2. This model will have a UIImageView, NSData
3. When u init the model, pass in a URL
4. Use NSURLConnection to send out AsynchroizedRequest
5. By using NSURLConnection delegate, you will finally get the Data of the image
6. Init a UIImage with these data
7. Init The UIImageView with this Image
8. Add this imageview to this model or directly pointing this model to the imageview
Feel free to ask for more detail :)

SSCollectionView SSCollectionViewItem - no Items displayed

I ran into difficulties with SSCollectionView and SSCollectionViewItem.
First of all I'd like to get it initialized from IB. But that won't work for me.
I have a SelectFooViewController which is:
#interface SelectFooViewController : SSCollectionViewController { ... }
and am using it as filesOwner of the corresponding XIB.
SelectFooViewController* selectFooVC = [[SelectFooViewController alloc]
initWithNibName:#"SelectFooViewController" bundle:nil];
But since it wont work I had to initialize its properties inside viewDidLoad() myself.
Furthermore I am not able to display anything except the backgroundColor of my SSCollectionViewItems. What I want is a textLabel and an image .
- (SSCollectionViewItem *)collectionView:(SSCollectionView *)aCollectionView itemForIndexPath:(NSIndexPath *)indexPath {
SSCollectionViewItem *item = [[[SSCollectionViewItem alloc] initWithStyle:SSCollectionViewItemStyleImage reuseIdentifier:itemIdentifier] autorelease];
SSLabel* label = [[SSLabel alloc] init];
[label setText:#"foo"];
item.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"foo.png"]];
item.textLabel = label;
[label autorelease];
return item;
}
I can confirm that the delegate methods (for determining the number Of rows, sections and such) are implemented and working as expected. But my items are all empty - but react onclick with the expected popup.
Does anyone see an error in what I did? - Thanks...
EDIT: I was also not able to display a local image by changing SSCatalog project
I just figured out, that I have to set the frame of each property (textLabel, detailTextLabel and imageView) myself. That fixed it.
When you create instance SelectFooViewController just insert this line
selectFooVC.view;
or
selectFooVC.view.hidden = NO;
And then add it to the view.
This is because the view is not initalised until you explicitly access it. Hence your items are loaded only when you click it and not immediately. You can call it a hack but i don't call it one. :-)

Updating imageView on UIButton within UITableViewCell

I have custom UITableViewCells (created in interface builder) which are used for a static UITableView which for the sake of argument I will call the AddUserTableViewController (i.e. I do not use autoreleased cells as I only have 5 rows).
In the top cell (used for the user name) I have embedded a UIButton which I use to open an UIActionSheet which in turn allows an UIImagePicker to set an image on the UIButton (i.e. [button.imageView.image = imageFromImagePicker]). The button has a default image (added in IB) which says "add image".
The image on the button is set without issue and remains in place even when I navigate to the detail page of any of the table cells with the exception of the user name cell which contains the button. When I select this cell the image dissapears and does not reappear once I navigate back to the AddUserTableViewController from the "add name" detail view. The "add image" default image, referred to above, is displayed.
I have tried many strategies amongst which have been:
1) using [self.tableView reloadData], [button.imageView setNeedsDisplay], [self.view setNeedsDisplay] in the viewWillAppear method;
2) using the above methods in the cellForRowAtIndexPath method;
3) using the above methods in the overriden willDisplayCell:forRowAtIndexPath: method;
When I place debugging NSLog statements in the program I can see that the button.imageView.image property is still set to the image selected using the UIImagePicker but it is not displayed (the default image from the nib is displayed).
As I mentioned above, this only happens if I navigate away from the AddUserTableViewController by selecting the UITableViewCell within which the UIButton is embedded.
I am currently at a loss as to what to do and would be extremely grateful for any assistance that anyone could offer. I just need to find a way to update the image on the embedded UIButton in the above-mentioned circumstances.
I've added the a section of the code from the cellForRowAtIndexPath: method just for illustrative purposes -
//populates the personal info section
if (section == kPersonalInfoAddSection) {
//Covers the add image button and the name adding field
if (row == kNameRow) {
UILabel *nameLabel = (UILabel *)[nameCell viewWithTag:kMainTextTag];
UILabel *reqLabel = (UILabel *) [nameCell viewWithTag:kSubTextTag];
//UIButton *imageButton = (UIButton *) [nameCell viewWithTag:kImageTag];
if (mainUser.imagePath != nil) {
UIImage *img = [UIImage imageWithContentsOfFile:mainUserImagePath];
imageButton.imageView.image = img;
[imageButton.imageView setNeedsDisplay];
//[imageButton setNeedsDisplay];
//[nameCell setNeedsDisplay];
NSLog(#"************************** Added the BESPOKE image (%#)to the image button", img);
}
else {
NSLog(#"************************** Added the DEFAULT image (%#)to the image button", addUserImage);
imageButton.imageView.image = addUserImage;
}
UIColor *blackText = [[UIColor alloc] initWithWhite:0 alpha:1];
NSString *firstName;
if (mainUser.firstName){
nameLabel.textColor = blackText;
firstName = mainUser.firstName;
}
else {
nameLabel.textColor = reqLabel.textColor;
firstName = NAME_STRING;
}
NSString *lastName = (mainUser.lastName)? mainUser.lastName : EMPTY_STRING;
NSString *name = [[NSString alloc] initWithFormat:#"%# %#", firstName, lastName];
nameLabel.text = name;
reqLabel.text = REQUIRED_STRING;
[blackText release];
return nameCell;
}
In the cellForRowAtIndexPath are you doing something that would 'unset' the image?

Using a variable in a statement to reference the image stored in a UIImageView

UPDATED Scroll down to see the question re-asked more clearly....
If I had the name of a particular UIImageView (IBOutlet) stored in a variable, how can I use it to change the image that is displayed. I tried this, but it does not work.
I'm still new to iphone programming, so any help would be appreciated.
NSString *TmpImage = #"0.png";
NSString *Tst = #"si1_1_2";
TmpImage = #"1.png";
UIImage *sampleimage = [[UIImage imageNamed:TmpImage] retain];
((UIImageView *) (Tst)).image = sampleimage; // This is the line in question
[sampleimage release];
RESTATED:
I have a bunch of images on the screen.... UIImageView *s1, *s2 ,*s3 etc up to *s10
Now suppose I want to update the image each displays to the same image.
Rather than doing
s1.image = sampleimage;
s2.image = sampleimage;
:
s10.image = sampleimage;
How could i write a for loop to go from 1 to 10 and then use
the loop var as part of the line that updates the image.
Something like this.
for ( i = 1; i <- 10; ++i )
s(i).image = sample; // I know that does not work
Basic question is how do I incorporate the variable as part of the statement to access the image? Don't get hung up on my example. The main question is how to use a variable as part of the access to some element/object.
Bottom Line... If I can build the name of a UIImageView into a NSString object, How can I then use that NSString object to manipulate the UIImageView.
Thanks!
Ugh! Your line in question:
((UIImageView *) (Tst)).image = sampleimage;
is casting a string pointer as a UIImageView pointer - you're basically saying that your pointer to a string is actually a pointer to a UIImageView! It will compile (because the compiler will accept your assertion happily) but will of course crash on running.
You need to declare a variable of type UIImageView. This can then hold whichever view you want to set the image of. So your code could look like the following:
NSString *TmpImage = #"0.png";
UIImageView *myImageView;
If (someCondition == YES) {
myImageView = si1_1_2; //Assuming this is the name of your UIImageView
} else {
myImageView = si1_1_3; //etc
}
UIImage *sampleimage = [UIImage imageNamed:TmpImage]; //no need to retain it
myImageView.image = sampleImage;
Hopefully this makes sense!
Edit: I should add, why are you trying to have multiple UIImageViews? Because a UIImageView's image can be changed at any time (and in fact can hold many), would it not be better to have merely one UIImageView and just change the image in it?