UILabel in different subviews - iphone

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.

Related

Display JSON as List in ViewController (Not in TableView)

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!

XCode - Dynamically created labels, when i change the text it changes it for the last one only

So i have a bunch of dynamically loaded labels..
Each of them has the same name because there is no telling how many there will be..
I have another method (not the one that created the labels) changing the text for one of the labels, but when i run it only the last label that was created will change..
I need it to change the one that has a certain tag or something..
Help is much appreciated, this website is yet to let me down.
self.myLabel cannot be connected to multiple labels, so it will contain the reference of last created label, you will have to create new label every time, and you can't track them by class properties, you have to access label by their tag.
you can set tag for each label, below is sample code,
for(int i=0; i< numberOfLabels; i++)
{
UILabel *label = [[UILabel alloc] init];
label.tag = i; // do not use tag 0 here.. u can use i+1, or i+100.. something like this.
[self.view addSubview:label];
}
to access labels,
UILabel *label = (UILabel*)[self.view viewWithTag: labelTag];
Okay since you dont have any code to show i guess i have to speculate.
What i understood is that you are creating Dynamic UILabels in ur code and you want to access them. Since you have same name for all the UILabels you might me loosing the previous UILabel when every time you create a new UILabel. So in order to keep track of how many UILabel you created you must add them in an Array. Declare an NSMutableArray in your viewController.h file and make sure in the viewDidLoad u allocate it like
arrForLabels = [[NSMutableArray alloc]init];
Since it is an NSMutableArray you can add object to it.
So when u create a UILabel make sure you add the same UILabel in the Array as well
for Instance
[arrForLabels addObject:yourLabel];
you can try to NSLog your Array to see its content.
Now all youu got to do is to Create a weak link like that
UILabel *tempLabel = [arrForLabels objectAtIndex:1];
now tempLabel will be the UILabel to change text
tempLabel.text = #"My New Text";
It will work fine.
Feel free to ask for any issues in it.

Using Texture as label in WhirlyGlobe

WhirlyGlobe is a very useful framework and I would like to display Country Flags instead of names for each countries.
When looking at LabelLayer.h it seems possible to overwrite the icontexture generated from Text label by a picture:
#interface SingleLabel : NSObject {
NSString *text;
WhirlyGlobe::GeoCoord loc;
NSDictionary *desc; // If set, this overrides the top level description
WhirlyGlobe::SimpleIdentity iconTexture; // If non-zero, this is the texture to use as an icon }
But I didn't find any way to use label for picture display.
Can someone please help me understand how to replace Text label by Picture label?
Following #Mousebird first answer here is what I"ve implemented:
// This describes how our labels will look
NSDictionary *labelDesc =
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],#"enable",
[UIColor clearColor],#"backgroundColor",
[UIColor whiteColor],#"textColor",
[UIFont boldSystemFontOfSize:16.0],#"font",
[NSNumber numberWithInt:4],#"drawOffset",
[NSNumber numberWithFloat:0.08],#"height",
[NSNumber numberWithFloat:0.08],#"width",
nil];
// Build up an individual label
NSMutableArray *labels = [[[NSMutableArray alloc] init] autorelease];
SingleLabel *texLabel = [[[SingleLabel alloc] init] autorelease];
Texture *theTex = new Texture(#"icon", #"png");
theTex->setUsesMipmaps(true);
texLabel.text = #"";
texLabel.iconTexture = theTex->getId();
theScene->addChangeRequest(new AddTextureReq(theTex));
[texLabel setLoc:GeoCoord::CoordFromDegrees(5, -3)];
[labels addObject:texLabel];
[self.labelLayer addLabels:labels desc:labelDesc];
Still a problem, the texture is loaded in memory but didn't appear. Putting a string as a text create a Blank square on the first character of the label
Sorry for the delay. I need to set up some sort of feed for stackoverflow.
Anyway, the short answer is there isn't a great way to do it. I'm adding Markers for a client, but that won't be out until 1.2. For now, you should be able to make a label display a texture. That's the iconTexture you're showing there.
To create a texture, do the following:
Texture *theTex = new Texture(#"nameoftexture", #"png");
theTex->setUsesMipmaps(true);
theTexId = theTex->getId();
scene->addChangeRequest(new AddTextureReq(theTex));
And then to use it:
singleLabel.text = #"";
singleLabel.iconTexture = theTexId;

JSON - Php/SQL iPhone APP Questions/Help

Hey group, first time posting here
I am somewhat new to the JSON/PHP/mySQL world, but been around iPhone designing for the past few years, though this topic of conversation is a while new area I am entering.
I have done JSON iPhone examples that allow me to create a UITableView and display the JSON data into the TableViewCells (CustomCells) and displays the data (NSDictionary)into UILabels
The problem I am having now, is that I want an APP that displays this information from the JSON into just a couple of UILabel's on a regular UIViewController and not a UITableView
any help would greatly be appreciated,
the example I used and learned for JSON and UITABLE was from http://tempered.mobi/%20
I used that example from my app, but incorporated a few other things like CUSTOM cells
however now when the USER selects the specific CELL I want it to load specific data from another JSON file, and cannot get it to load in a UILabel or UITextView in a UIViewController
HELP :-)
Not sure if I completely understand your query, when the user clicks on a table cell you want the app to do a request for data in JSON format and display it in labels on a view? correct?
If so, then I'd modify the didSelectRowAtIndexPath method. Use the index to determine which JSON request to fire, load the results into object/array/dictionary and then set the labels text from this data source.
Apologizes if I mis-understood your question.
Here is my coding to it, this is the XIB that is loaded once the TABLEViewCell is selected, i apologize in advanced for the formatting, I dont know why it is not coming out properly as I am simply copying and pasting from Xcode
.h File, all I added was
IBOutlet UILabel *homeTeam;
IBOutlet UITextView *homeTeam2;
IBOutlet UILabel *awayTeam;
NSArray *rows;
}
#property (retain, nonatomic) NSArray *rows;
(added note: I obviously hooked up the UILabels and UIText View to my labels and text view in that specific XIB) and have tested using the homeTeam.text = #""; (in the Implementation file)to make sure they are functioning properly
.m file
import "CJSONDeserializer.h"
import "JSON.h"
#synthesize homeTeam, awayTeam, awayTeam2;
(void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://yourserver.com/yourdata.json"]; // Modify this to match your url.
NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url]; // Pulls the URL
//NSLog(jsonreturn);
// Look at the console and you can see what the restults are
NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;
// In "real" code you should surround this with try and catch
NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
if (dict)
{
rows = [dict objectForKey:#"users"];
[rows retain];
}
NSLog(#"Array: %#",rows); //making NOTE this NSLOG does print my JSON Data grabbed from the SQL Database
awayTeam2.text =[dict objectForKey:#"awayteam"];
awayTeam.text = [dict objectForKey:#"awayteam"];
[jsonreturn release];
CONSOLE: as you can see it grabs my JSON data just fine....
Array: (
{
awayicon = "Brampton.png";
awayteam = Mississauga;
homeicon = "Mississauga.jpg";
hometeam = Brampton;
hscfinal = 10;
id = 1;
}
)

Hide UIToolbar UIBarButtonItems

I have a UIToolbar that I set up using IB with three buttons, left, middle and right. In some situations I would like to not display the middle button. Does anybody know of a way to hide a specific button on inside a UIToolBar? There is no hide property, all I can find is setEnable but this still leaves the button causing users to wonder what its purpose is. I would like to only display it in situations that it actually has a use.
Thanks in advance!
Reset the items:
-(void)setItems:(NSArray *)items animated:(BOOL)animated
You can get the current items using the items property, then just remove the one you don't want to show and pass in the new NSArray.
As you can see, you can also animate it to make it clear to the user.
Rather than guessing at the index, I added an IBOutlet for the UIBarButtonItem and then removed it by name:
NSMutableArray *toolBarButtons = [self._toolbar.items mutableCopy];
[toolBarButtons removeObject:self._selectButton]; // right button
[self._toolbar setItems:toolBarButtons];
And of course it helps to connect the outlets in the designer :)
This is how i did it.. too much headache but its the best i could come up with :
NSArray *toolBarArray = toolBar.items;
NSMutableArray *newToolBarArray = [NSMutableArray arrayWithArray:toolBarArray];
[newToolBarArray removeObjectAtIndex:2];
[newToolBarArray removeObjectAtIndex:1];
//remove whatever buttons you want to.
NSArray *finalTabBarArray =[[NSArray alloc] initWithObjects:newToolBarArray, nil];
[toolBar setItems:[finalTabBarArray objectAtIndex:0] animated:NO];
I know it is quite old thread for but those who look this page for solution, here you go :
With iOS7, you can use this approach to show/hide your toolbar button :
if(// your code Condition)
{ self.toolbarBtn1.enabled = YES;
self.toolbarBtn1.tintColor = nil; }
else
{ self.toolbarBtn1.enabled = NO;
self.toolbarBtn1.tintColor = [UIColor clearColor]; }
This does not work here because the array you are sending with setItem is not what the function expects.
I had to replace the line:
NSArray *finalTabBarArray = [[NSArray alloc] initWithObjects:newToolBarArray, nil];
with this one:
NSArray *finalTabBarArray = [newToolBarArray copy];
Then it works perfectly.
Mohit's answer is one that I have used, but you dont need to specifically make it a NSArray that the toolbar sets. You can just set the array of items as a NSMutableArray. No real advantage that I am aware off but its a few lines less code. And that way you can take the array and move about UIButton objects as you would any other array with objects and then just reset the toolbar with that mutable array.
[newToolBarArray removeObjectAtIndex:2];
[newToolBarArray removeObjectAtIndex:1];
[toolBar setItems:newToolBarArray];