How can i write logic for this App - iphone

I am trying to do an app i just stuck with some logic in that app. It contains 10 fields in which user need to enter text in all the fields.After this screen i designed 5 screens which contains labels in all the screens in which the text entered by the user will display in all the screens.
What exactly i need is after user enters the text he chooses one field from 10 fields.
After choosing a field he advances to labels screens in which he need to decide weather he chosen field is there in that screen or not ,the same thing will happen in the rest labels screens also.If the chosen field is there in the particular screen then the user will press a button named YES. In which screen he presses YES button i need to update that buttons in that particular screens and at last i need to get update of that YES buttons . In what ever screen i am getting the update of the button depending on that my result will be displayed.
I Just need how can i update that YES buttons if user presses in the different views so that i can get count of which screen the button is pressed and depending on that i can display result.
Help me in logic of this app. Thanks!

you can try using core data for this,
another approach would be to have an array, composed of dictionaries [to pass around in all your views]
like this:
arrayFields, make it a mutable array
composed of dictionaries with this objects : field and pressed
so you can go checking and setting each object and specify if it has a yes or a no associated with it and act accordingly
good luck!
edit,
you need an array as a property
so in your
Starting1classViewController.h
#interface **** {
NSMutableArray *_fields;
UITextField *_textfield1;
}
#property (nonatomic,retain) NSMutableArray *fields;
#property (nonatomic,retain) UITextField *textfield1;
Starting1classViewController.m
#synthesize fields = _fields;
- (void)dealloc {
[_fields release];
[_textfield1 release];
[super dealloc];
}
- (void)viewDidLoad {
self.fields = [NSMutableArray array];
self.textfield1 = [[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 20)]autorelease];
[self.view addSubview:textfield1];
}
create your textFields, and when filled, have a button that assigns the values of the textfields to a dictionary for each textField so, this dictionary goes into the array,
- (void)textFieldToArrayOfDictos {
NSMutableDictionary *fieldsDicto = [NSMutableDictionary dictionary];
[fieldsDicto setObject:self.textfield1.text forKey:#"text"];
[fieldsDicto setObject:#"no" forKey:#"selected"];
[self.fields addObject:fieldsDicto];
}
so you pass the array around your views, so you can populate your labels with the data from the array,
modifing the index needed with selected or not,
you just need to know what index is what,
or if you wanted to have the name of each field asociated to the values, you could make a dictionary of dictionaries,
;)

Related

Showing something different in a label every time the view is loaded

What I want to do, is when the app is loaded, the user see's a tip. I have about twenty of them and I want just a label to show them. I can show them in order, I just don't know how to show a different one every time. So is their a method to just go through an order every time the view is loaded?
so far i have done this
Made a tip lebel to be set
#property (weak, nonatomic) IBOutlet UILabel *tipLabel;
and im gunna set it in the view did load
#synthesize tipLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
tipLabel.text = // What Do I put here to pick from my list of 20 strings?
(in order or random)
You don't say where or how you are storing your list (or array) of 20 strings, but assuming it is a "NSArray" object with 20 strings, you could do something like this:
tipLabel.text = [arrayOfTips objectAtIndex: (arc4random() % [arrayOfTips count])];

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!

How to generate UITextFields dynamically with different name and accessing it from another place?

I have an iPhone application in which there are dynamically generated textfields to capture a value of product quantity. The default quantity is 1. I have generated textfields like this
for(int i=0;i<[array count];i++)
{
UITextField *i=[[UITextField alloc]init];
i.frame=CGRectMake(90, Yposqtytextfield, 60, 30);
i.borderStyle=UITextBorderStyleRoundedRect;
[self.scrollview addSubview:i];
i.delegate=self;
i.text=#"1";
i.tag=i;
[appDelegate.qtyArray addObject:i.text];
}
But I want values of quantities in next page. For that I have taken this qtyarray. Now the user is allowed to change quantity. So how can I change the value of quantity in the array. as declaration of textfield is local to that loop. So at next page navigation how can i get the values of all these textfields?
UITextField *aField = (UITextField*)[appDelegate.qtyArray objectAtIndex:0]
it should be
NSString *aValue = [appDelegate.qtyArray objectAtIndex:0];
because he is adding value of textfield not the textfield object itself.
you can access your qtyArray anywhere in the application. Since application delegate is singleton class. You can access value like this
NSString *aValue = [appDelegate.qtyArray objectAtIndex:0];

creating a Mutable array that can be added to in later clicks of the same button?

General noob questions:
(1) How can I create an NSMutable array in a buttonClicked action that I can add more entries to during subsequent clicks of the same button? I always seem to start over with a new array at every click (the array prints with only 1 entry which is the most recent button's tag in an NSLog statement).
I have about 100 buttons (one for each character in my string called "list") generated by a for-loop earlier in my code, and each has been assigned a tag. They are in a scrollview within the view of my ViewController.
I wish to keep track of how many (and which ones) of the buttons have been clicked with the option of removing those entries if they are clicked a second time.
This is what I have so far:
-(void) buttonClicked:(UIButton *)sender
NSMutableArray * theseButtonsHaveBeenClicked = [[NSMutableArray alloc] initWithCapacity: list.length];
NSNumber *sendNum = [NSNumber numberWithInt:sender.tag];
[theseButtonsHaveBeenClicked addObject:sendNum at index:sender.tag];
NSLog(#"%#",theseButtonsHaveBeenClicked);
}
(2) I have read that I may be able to use a plist dictionary but I don't really understand how I would accomplish that in code since I cant type out the items in the dictionary manually (since I don't know which buttons the user will click). Would this be easier if I somehow loaded and replaced the dictionary in a plist file? And how would I do that?
(3) I also have no idea how I should memory manage this since I need to keep updating the array. autorelease?
Thanks for any help you can provide!
Okay, firstly you are creating a locally scoped array that is being re-initialised on every call to buttonClicked:. The variable should be part of the class init cycle.
You will also be better off with an NSMutableDictionary instead of an NSMutableArray. With a dictionary we don't have to specify capacity and we can use the button's tags as dictionary keys.
Here's what you need to do, these three steps always go together: property/synthesize/release. A good one to remember.
//Add property declaration to .h file
#property (nonatomic, retain) NSMutableDictionary * theseButtonsHaveBeenClicked;
//Add the synthesize directive to the top of .m file
#synthesize theseButtonsHaveBeenClicked;
// Add release call to the dealloc method at the bottom of .m file
- (void) dealloc {
self.theseButtonsHaveBeenClicked = nil; // syntactically equiv to [theseButtonsHaveBeenClicked release] but also nulls the pointer
[super dealloc];
}
Next we create a storage object when the class instance is initialised. Add this to your class's init or viewDidLoad method.
self.theseButtonsHaveBeenClicked = [[NSMutableDictionary alloc] dictionary]; // convenience method for creating a dictionary
And your updated buttonClicked: method should look more like this.
-(void) buttonClicked:(UIButton *)sender {
NSNumber *senderTagAsNum = [NSNumber numberWithInt:sender.tag];
NSString *senderTagAsString = [[NSString alloc] initWithFormat:#"%#",senderTagAsNum];
// this block adds to dict on first click, removes if already in dict
if(![self.theseButtonsHaveBeenClicked objectForKey:senderTagAsString]) {
[self.theseButtonsHaveBeenClicked setValue:senderTagAsNum forKey:senderTagAsString];
} else {
[self.theseButtonsHaveBeenClicked removeObjectForKey:senderTagAsString]; }
[senderTagAsString release];
NSLog(#"%#", self.theseButtonsHaveBeenClicked);
}

tagging a cell for identification in a selected tableview

how is it possible to tag an iphone cell in order to be able to use in a selected favorite table view. i want to be able to tagg the cell and its remaining content in order to be able to display it properly inside a new table view under a different view controller.
You set a tag with a number and by looking at UIView class ref, you set it with an NSInteger,
so a number to you and me.
tableViewCell.tag = 1
Or you extend UITableView and add your own method like
- (void) setFavorite:(BOOL)ans
{
favorited = ans;
}
- (BOOL) favorite
{
return favorited;
}
And then when you access the table view cell, cast it.
Probably better to make it a property though
#property (nonatmoic, assign, getter=isFavorited) BOOL favorited;