I am new to iphone dev. I have a Label and a scroll view. I am trying to get data from url and bind it to label and a scroll view. I am able to bind the data to label by using...
UrlValues *asana=[[data yoga]objectAtIndex:i];
self.AsanaName.text = [asana asanatitle];
Similarly i want to bind some other data in the url to a scroll view. The data i want to bind is 4 or 5 sentences. So, can anyone help me how to do it... Any sample code will be really helpful... Thanks
if you want to show text from the URL fetch text from URL -
NSString *data_from_URL = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"urlgoes here"] encoding:NSUTF8StringEncoding error:nil];
Then you can set this text in the UITextView -
UITextView *sv = [[UITextView alloc] init];
[sv setText:data_from_URL];
Related
I am having a trouble on exporting text data. I have a view contains over 3 different uitextview that allow user to type in data. Then i want to export these texts from the 3 different uitextview (e.g. textview1, textview2 and textview3) into a new uiviewcontroller that contains only one uitextview. I also want the textview 1-3 are placed in order. So that the new uitextview will have three different sections in the order of 1-3. I am using PrepareFroSegue method. How should i code this? Many thanks!
What you want to do is concatenate (combine) the strings into one NSString that will be sent into the next view. You can do this in your PrepareForSegue. Put this in your prepareforsegue but edit the code so it works with your code.
NSString *concatenatedString = [NSString stringWithFormat:#"%# %# %#", textview1.text, textview2.text, textview3.text];
YourViewController *dV = [segue destinationViewController];
dV.stringToStoreValue = concatenatedString;
The first line I concatenate the string
The second line I get the destination view controller from the segue
The third line I set the text to a NSString #property in the destination view controller. You must then in the view did load of the destination view controller set
yourcombinedtextfield.text = stringToStoreValue;
I do not directly set the value of the text field in the prepareForSegue because if the view has not loaded yet then the I would be setting nothing and not see the string on the next page in the textfield!
You're trying to communicate between ViewControllers this answer looks like a good starting point for understanding how this is done..
UPDATE :
newController.textView.text = [NSString stringWithFormat:#"%# %# %#", textView1.text, textView2.text, textView3.text];
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 have an app where the users enters information in a few textfields which are then added to a uitextview by a nsstring does anybody know how i can set one of the textfields to italics and keep the others normal
regards
Edit:
Heres my code i want to only change once textfield (e.g textfbookpublisher):
NSString* combinedString = [NSString stringWithFormat:
#"%#,%#.'%#.'%#.%#,%#.",
textfbookpublisher.text,
textfbookauthor.text,
textfbooktitle.text,
textfbookplace.text,
nameofuni.text,
textfbookyear.text];
DissertationGen*bookg = [[DissertationGen alloc] init];
bookg.message = combinedString;
bookg.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:bookg animated:YES]
A quick search would have given you an easy answer. For example, this post.
Simply set the font of the UITextfield you want to a [UIFont italicSystemFontOfSize:].
try this link see accepted answer and also check TTTAttributedLabel library
Bold one Word
now you want only textfbookpublisher in italic then pass it individually to next DissertationGen view
in DissertationGen controller's viewdidload method use following code
UILabel *lblpubl = [[UILabel alloc] init];
lblpubl.font = [UIFont italicSystemFontOfSize:18.0];
lblpubl.text = your variable which contains textfbookpublisher's text;
now use lblpubl as you wish
I have a list of images retrieve from .xml file and these images are images link from the server e.g. " www.seeimage.com/rice.png"
I am parsing the images everytime I went to that page
-(void)viewdidload{
for (int i = 0; i<[appDelegate.foodItems count];i++) {
NSURL *ZensaiimageSmallURL = [NSURL URLWithString:ZensaiPLUitems.ZensaiimageSmallURL];
NSString *string = [[NSString alloc] initWithFormat:#"%#", ZensaiimageSmallURL];
NSData *simageData = [NSData dataWithContentsOfURL:ZensaiimageSmallURL];
UIImage *itemSmallimage = [UIImage imageWithData:simageData];
[zenbutton2 setImage:itemSmallimage forState:UIControlStateNormal];
[scrollView addSubview:zenbutton2];
}
}
i have been trying out on this tutorial : http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/13315-image-caching-tutorial.html
but i have no idea on how to implement this in my work.
any idea on how to cache them on the first run and whenever i return to that view ?
i don't want to rerun this method to retrive the images from the website everytime i come to this view.
it takes quite some time to init the images from the website to my UIButton before populating them to the scrollview.
if you do not want to retrieve the images from the website every time then you can download it once and store them e.g. into NSMutableDictionary . And access it whenever you want.
OR
Parse those images in separate thread so that your table view or scroll view will not get paused.
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;
}
)