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])];
Related
I want to create a global array, I was looking in to NSMutableArray because you can index it. I need to be able to call specific lines of the array and display via a label the 1st part of the array and compare users input with the second part.
Example is: User sees, press "x" (this is the first part of the array) and if the click one button it will compare that button id to the second part of the array.
Im not familiar with arrays in objective C, links or code snips will help!
It sound like a bit like you have a question answer format...
What you could do is have an NSArray of Question the objects that may look something like
#interface PSQuestion : NSObject
#property (nonatomic, copy) NSString *title;
#property (nonatomic, assign) NSInteger answer;
#end
#implementation PSQuestion
#synthesize title = _title;
#synthesize answer = _answer;
#end
Nw you can have an indexed array of objects that contain both components you require
// Configure questions
PSQuestion *question1 = [[PSQuestion alloc] init];
question1.title = #"Click X";
question1.answer = 2;
I need to be able to call specific lines of the array and display via
a label the 1st part of the array and compare users input with the
second part.
No problem! NSArray or it's mutable counterpart can store strings just as easily as any other object. It's as simple as [mymutablearray addObject:#"mystring"];.
User sees, press "x" (this is the first part of the array) and if the
click one button it will compare that button id to the second part of
the array.
To compare two arrays use [_array1 isEqualToArray:array2];, to compare strings within an array to a button label, use
[[array1 objectAtIndex:0]isEqualToString:myButton.titleLabel.text];
Looks like you only need a one dimensional array of objects that have two elements. Define a class that has the two parts of which you speak, and then make an NSMutableArray full of instances of that class.
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,
;)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a view that displays in a UIPopoverController. Before, it just had properties that were associated with it's view for a single object. So it would be something like
TargetDetailView : NSObject
#property Target *target
- (id)initWithTarget:(Target *)target
Now however, in some situations depending on what level zoom the user is looking at our view, there will be multiple targets in the popover. So I added an NSMutableArray *targets property to the class in order to show multiple targets in the popover.
Right now, the actual target objects that get drawn on our view are getting drawn on top of each other which is a waste.
The initWithTarget method only got called when you actually clicked on a target and the popover got presented.
What I'm trying to do now is calculate ahead of time where I am goign to have multiple targets in the same location, draw it only once, but when pressed have the popover show and this time, use the NSArray *targets to search through to show the multiple targets in that location.
The problem I have is I do not know where to initialize the memory for the NSMutableArray *targets. The TargetDetailView only gets initialized when you press the target, but I want to calculate ahead of time from my other class when the target views are on top of each other to only draw it once.
I sort my targets in order in terms of location, then I look to see if the first two are similar enough in location to bin them:
for (int i = 0; i < [sortedArray count] - 1; i++) {
TargetDetailView *firstTargetDetailView = (TargetDetailView *)[sortedArray objectAtIndex:i];
TargetDetailView *secondTargetDetailView = (TargetDetailView *)[sortedArray objectAtIndex:i + 1];
if (secondTargetDetailView.target.location - firstTargetDetailView.target.location < scale) {
[firstTargetDetailView.targets addObject:secondTargetDetailView.target]; // crash
[newTargetDetailViewArray addObject:(TargetDetailView *)[sortedArray objectAtIndex:i]];
}
}
I try to have the first TargetDetailView.target property point to the next TargetDetailView if it's close enough in location. However, the .targets property never gets initialized since my TargetDetailView only gets initialized on press.
How should I either
a) allocate memory for this object
b) refactor and/or allocate memory for this object
Thanks.
If I may make a suggestion, you could use the same initWithTarget method but always keep your targets in an array. You could then initialize your array there and add your Target parameter from the start, like this:
#interface TargetDetailView :NSObject
#property (nonatomic, retain) NSMutableArray *targets;
- (id)initWithTarget:(Target *)target;
#end
#implementation TargetDetailView
#synthesize targets;
- (id)initWithTarget:(Target *)target {
self = [super init];
if (self) {
targets = [[NSMutableArray alloc] initWithObject:target];
}
return self;
}
You can then use the targets property to add more targets (if you'd like) and use a simple loop through targets without having to care about the number of items in there.
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);
}
I have 5 different views and when I tap the button, I want to push one of 5 views randomly. However, I do not want to make 5 different controllers for each views. Do I have a chance to put them in one controller? If so, how?
You can have as many views as you want in a single UIViewController subclass. You can create them all in Interface Builder as well, in the one .xib file for your UIViewController class (it might get a bit hard to see, though, better to lay out each UIView in its own .xib). You can present them in any combination you want. Assuming you want to just show one view at a time, you can do this:
In your viewDidLoad method, start out showing the initial view, and in your class keep track of which is the current view:
...
#property(nonatomic, retain) UIView *currentView;
...
- (void)viewDidLoad
{
[self.view addSubview:self.defaultView];
self.currentView = self.defaultView;
}
Then to switch to a particular other view do this:
- (void) switchToView:(UIView *)newView
{
[self.currentView removeFromSuperView];
[self.view addSubview:newView];
self.currentView = newView;
}
}
Or you can show them all at one time: just [self.view addSubview:theView];
You have to set the tags of all the views, and then use
int r = arc4random() % 5;
method to find the random number. Use this newly generated number to check the tag, in your selector
Just change the view outlet of the controller on the action of the button. Use random numbers to select which view. Ouh and - (void)setNeedsDisplay :)