In my IB I already have 5 UIImageViews with an image in each of them. UIImageViews are called blue1, blue2,.... blue5.
In my code I'm trying to add a pan gesture recognizer and then add them to an array.
How could i programmatically change the name of which UIImageView?
I tried making a string first and then casting it to UIImageView in the addObject step but that didn't work.
I tihnk i have to make it UIImageView in the first line below but then how could i do something like what i did below with NSString stringWithFormat:#"blue%d", numberOfSetUpTurns+1]
NSString * currentPlayer = [NSString stringWithFormat:#"blue%d", numberOfSetUpTurns+1];
[playerOneArr addObject: (UIImageView *)currentPlayer];
With the code that you have now, you are trying to cast a string into a UIImageView, which will not work.
You can perhaps create an NSMutableDictionary, to the dictionary you can do something like:
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict addObject: someUIImageView forKey:currentPlayer];
This way when you want to retrieve the imageView you can just call
[playerOneArr addObject:[dict objectForKey:currentPlayer]];
I hope I understood your question and I hope this helps!
Related
I am producing a JSON string that I need to parse out and display onto the page. My JSON string outputs information about the contents of a CD like this:
[{"song_name":"Song 1","artist":"John","price":"$1"},
{"song_name":"Song 2","artist":"Anna","price":"$2"},
{"song_name":"Song 3","artist":"Ryan","price":"$3"}]
I would like to display the contents in my viewController in a list format displaying the song_name, artist, and price. I do not want to use a tableView to display this information, but would rather just have a list displayed. How might I go about doing this? I would assume that I need to use NSJSONSerialization, but have only used that for a tableView in the past. Thank you!
In addition to other answers, you can use only one label, just create NSMutableString (for dynamicly adding tracks info) with #"\n" between tracks info, pass it to label.text and set UILabel's property numberOfLines to 0
Follow these steps:
Parse the JSON and store the key-value pair(NSDictionary of CDs) in an NSArray (say infoArray)
Add a UIScrollview as a subview on your viewController's view.
Allocate UILabels dynamically, depending on infoArray count. Looking at your data I believe you can initialize labels with static frames i.e your y can remain static.
Add the text from the infoArray on this label.
Still, I would suggest use UITableView only. It is much simpler and a better approach
You make an array of dictionaries using NSJSONSerialization indeed, then you parse this array one by one and create a view of every dictionary. You're probably best of using a method for this:
-(UIView *) listView: (NSString *)songName andArtist:(NSString *)artist andPrice:(NSString *)price andIndex:(int)viewIndex {
//create your view here and do anything you want
UIView *subView = [[UIView alloc] init] autoRelease];
subView.frame = CGRectMake(0, viewIndex * 70, self.view.frame.width, 70);
//add labels and other stuff
// return the view
return subView;
}
The you add it to the current view by setting different Y values so they appear underneath each other by using the viewIndex of the former method... So if you have an array it goes something like this:
for (int i = 0; i < [array count]; i++) {
NSDictionary *dict = [array objectAtIndex:i];
NSString *songName = [dict valueForKey:#"song_name"];
NSString *artist = [dict valueForKey:#"artist"];
NSString *price = [dict valueForKey:#"price"];
UIView *tempView = [self listView:songName andArtist:artist andPrice:price andIndex:i];
[self.view addSubView:tempView];
}
You have to add it all to a scrollview otherwise you will run into the problem of to many rows on the page. Google for UIScrollView if you don't know how.
But I would recommend against this approach.. Tableviews are there with a reason. They are made for this stuff. Because the also provide for scrolling, drawing and refreshing. If you can, use them!
I'm creating my first app. It will be a catalog of products, that you can scroll through.
I created a UIScrollView with a width of 960 (320*3) and added a UIPageControl. Inside it I added 3 different view, each of them represents one of my products - with all the information I need - name, image, description, price, etc..
I can see the views move, and so I've set the first product with UILabel classes and UIImageView. I was wondering if it is possible to use NSArray and set the UILabel's text property and imageNamed in the next view as the user switches to it.
My problem is that each view has a different UILabel element.
Thanks for your help, it is much appreciated!
Yes you could use a NSArray to store the data for each of your views. I would suggest first creating a simple object with the properties labelText and imageName;
Then you can create a NSArray of your custom object like this:
MyObject obj1 = [MyObject new];
obj1.labelText = #"My Text 1";
obj1.imageName = #"My Image 1";
//Other objects..
then
NSArray *myArray = [[NSArray alloc] initWithObjects: obj1, obj2, obj3, nil];
Then When you switch pages simply do this:
MyObject *myPageInfo = [myArray objectAtIndex:pageNumber];
myLabel.text = myPageInfo.labelText;
mylabel.imageName = myPageInfo.imageName;
Hope that helps.
i would like to know how i can access the name of an image when the syntax is something like that:
NSArray* imgArray =[[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"test_01.png"],
[UIImage imageNamed:#"test_02.png"],
[UIImage imageNamed:#"test_02.png"], nil];
If i print out a specific index, i get the address.
NSLog(#"TEST: %#", [imgArray objectAtIndex:1]);
Output:
TEST: <UIImage: 0x4b12cd0>
How do i get the stringvalue?
Any ideas?
Thanks for your time.
UIImage does not expose the name of file it was initialized with. There is no way to access it from the image object.
You should keep track of the string values yourself. The UIImage object is somewhat closed with respect to the data backing the image.
You could of course subclass UIImage and implement
+(UIImage*)imageNamed:(NSString*)name
{
self.keepName = name;
return [super imageNamed:name];
}
adding a keepName string property, but I wouldn't see why you would.
If you want to store & retrive image from NSMutableArray than you have to take your image in UIimageView and than store it in NSMutableArray.. and from particular index you can retrieve it back.
K.D
I am trying to load an NSMutableArray with UIImageViews. Everything is going fine with that.
Unfortunately, I have no idea how to use the objects when they are in the mutable array.
Here is some code:
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
NSMutableArray *array = [NSMutableArray new];
[array loadWithObject:(UIImageView *)imageView];
[imageView release];
That kind of sets up what I've done. Here's what I want to do:
[array objectAtIndex:5].center = GCRectMake(0, 0);
but that doesn't work. How can I do this??
OK, I'll explain the problems you're having. The way to do what you are trying to do is the following:
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
NSArray *array = [NSArray arrayWithObject:imageView];
[imageView release];
[[array objectAtIndex:0] setCenter:CGPointMake(0.0,0.0)];
First, there is no method -[NSMutableArray loadWithObject:]. Likewise, for your example, you don't really even need a mutable array. Mutable objects have their place, but I usually try to use immutable ones when it makes sense to; as such, I've used NSArray.
Next, you never need to typecast objects when you're adding them to an array. There are a few reasons wherefore your example didn't work:
You were accessing the sixth (starting at one) object in the array. Was there an instance of UIImageView at that index?
For some reason, dot-notation for getters and setters only works when the compiler knows the type of the object you're sending a message to. Since the type of an object that is coming out of an array is not clear at compile-time, you can't use dot-notation. Instead, just use old-fashioned Objective-C method-sending syntax ("brackets and colons").
Finally, it's Core Graphics, not Gore Craphics: hence the prefix is CG, not GC. Also, -[UIImageView setCenter:] takes a CGPoint, not CGRect. So the function you wanted was CGPointMake.
Best of luck to you! Let me know if this helps clear some things up.
I think you should refer NSMutableArray.
However, I am just giving an overview of NSMutableArray.
NSMutableArray = Next Step (NS) Mutable Array
Mutable means array can be modified as and when required.
Now, This mutable array can hold any kind of objects.
Assume that I want to store strings in an array. I would write following statements.
NSMutableArray *anArray=[[NSMutableArray alloc] init];
[anArray addObject:#"Sagar"];
[anArray addObject:#"pureman"];
[anArray addObject:#"Samir"];
Here, I found that you need to store imageViews in your requirements.
NSMutableArray *anArray=[[NSMutableArray alloc] init];
UIImageView *imgV1=[[UIImageView alloc] initWithFrame:CGRectMake(10,50,60,70)];
UIImageView *imgV2=[[UIImageView alloc] initWithFrame:CGRectMake(10,110,60,70)];
UIImageView *imgV3=[[UIImageView alloc] initWithFrame:CGRectMake(10,170,60,70)];
UIImageView *imgV4=[[UIImageView alloc] initWithFrame:CGRectMake(10,210,60,70)];
[anArray addObject:imgV1];
[anArray addObject:imgV2];
[anArray addObject:imgV3];
[anArray addObject:imgV4];
Now, once ImageViews are added to array, release imageviews as array has it's retain count.
[imgV1 release];
[imgV2 release];
[imgV3 release];
[imgV4 release];
Above code will add images to NSMutableArray
When you use one of the image from an array, just write down this thing
UIImageView *x=[anArray objectAtIndex:0];
Hope Above descriptions work for you.
Add comment, if you don't understood.
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?