Program crashes when adding NSMutableArray of strings to a UILabel - ios

When I add this array of strings to the summaryText UILabel it crashes. Please let me know how to fix this.
NSMutableArray *arr = [[NSMutableArray alloc]init];
arr = [Singleton getArray];
NSString *str = [arr componentsJoinedByString:#"\n"];
summaryText.text = str;
This is what was brought up when i command clicked summaryText
#implementation TotalViewController
#synthesize tax,taxLabel,total,totalLabel,final,finalLabel,fiveLabel,threeLabel,twoLabel,five,three,two, points, pointsLabel,summaryText;

I had suggested initWithArray, but not clear why you don't replace entire above snippet with:
summaryText.text = [[Singleton getArray] componentsJoinedByString:#"\n"];
But as others pointed out, your crash isn't here, this just simplifies your code. The problem has to rest elsewhere, probably related to the definition/creation of summaryText. Hard to say without seeing crash log or more code.
Update:
You said that you created this control in Interface Builder. You might want to double check your "connections inspector" for that control and make sure your outlet is set up correctly. This sounds a lot like a control that hasn't been set up correctly in Interface Builder. Or you can look at you .h file in Xcode and it will tell you if it's successfully linked to a control in Interface Builder. You'll see a little "circle" to the left of the source code, a solid dot in the circle means that your outlet is hooked up correctly, and empty circle means that it's not (e.g. in this example below, contactName, contactAddress, and contactPhone are all linked up correctly, but myLabel is not):

Related

Changing overlaying labels text on a ScrollView

This is sort of hard to explain so I'm including a picture.
So I have a scrollview (the yellow part) and then an overlaying box with score and how many upgrades the player purchased, etc. The green box with the labels stays in the same position while the yellow part scrolls. That works fine, except I am trying to get the labels to change when the player purchases an item. So lets say the player purchases a pickaxe. The upgrades owned should change from "0/20" to "1/20".
This is my setup. I am using SpriteBuilder by the way. I have a class that handles the scrollview, like the buy buttons and descriptions, etc... Then I have another class just for the overlay on the side, and this is basically the only method in it:
- (void)didLoadFromCCB {
if (_doge < 1000000000000) {
balanceLabel.string = [NSString stringWithFormat:#"%.2Lf", _doge];
} else {
balanceLabel.string = #"A lot!";
}
upOwnedLabel.string = [NSString stringWithFormat:#"%d", _upOwned];
upMaxedLabel.string = [NSString stringWithFormat:#"%d", _upMaxed];
NSLog([NSString stringWithFormat:#"%d", _upOwned]);
}
I've tried a few things and this is the one that I think is "most correct". In the didLoadFromCCB on the scroll class, I have this:
UpgradesScene *upgradesScene = [[UpgradesScene alloc] init];
[upgradesScene didLoadFromCCB];
It's in the correct place (under buy button) but for some reason it doesn't update the label. I do get an NSLog message telling me the level, but for some reason the label doesn't work.
I'm very new to this language so please go easy on me :) Thank you
The problem is that you're creating a new upgradesScene with alloc init rather than getting a reference to the one that's in your scroll view. You should have a property (or IBOutlet if you're making this in IB) that points to that view.

how can i differentiate and refer to various UITextViews that are created programmatically?

A UITextView is created each time i click on ADD button. Y-axis value is altered(say, y+=100) every time i click ADD and so a set of UITextViews are created one below the other. I cant figure out how to differentiate and access a particular UITextView. Thanks for any help!
EDIT:
-(IBAction)access:(id)sender
{
int tg=[sender superview].tag;
UIView *view=(UIView *)[textView viewWithTag:tg-1];
}
tg-1 because im trying to access the previous UITextView and when i do this it returns NULL.
Store them on a NSMutableArray:
NSMutableArray * views = [[NSMutableArray alloc] init]
Your IBAction
-(IBAction)access:(id)sender{
int tg=[sender superview].tag;
UIView *view=(UIView *)[textView viewWithTag:tg-1];
[views addObject: views];
}
Then you can get all the references with a integer index with:
UIView * storedView = [views objectAtIndex: 1];
Use a view tag to differentiate the views and access them.
You don't say how you're creating the new views, but something like this should work:
UIView* new_view = [UITextView initWithFrame(...)];
new_view.tag = generate_tag()
Where the generate_tag() function generates whatever naming scheme makes sense for your application.

Loop in a variable of type IBOutlet

I have twelve text fields as you can see below:
IBOutlet UITextField *ce_1;
IBOutlet UITextField *ce_2;
IBOutlet UITextField *ce_3;
....
IBOutlet UITextField *ce_12;
All I have to do is to set an existing object in an array in each of the variables that are responsible for the text fields, I'm currently doing as follows:
ce_1.text = myArray[1];
ce_2.text = myArray[2];
ce_3.text = myArray[3];
....
ce_12.text = myArray[12];
Not to be writing a lot, I thought I'd put this in an automated way within a loop as follows:
for(i=1;i<13;i++){
ce_[i].text = myArray[i];
}
But this command does not work the way I expected, so I would like your help to try to solve my idea and put it into practice, is there any way of doing this?
Research and start using IBOutletCollection. It will give you an array of text fields that you can build in your storyboard XIB.
Note that you may need to consider the order of the array, and that you might want to sort it (possibly based on the tag of each view).
Technically, you could use string formats and KVC to do what you're currently trying to but it is far from ideal.
You can't just replace ce_1 ce_2 ce_3 with ce_[i] it doesn't work that way. You can only use [number] with an nsarray variable (or decendents).
for example:
NSArray* myArray = #[#1];
NSLog(#"%#", myArray[0]);
You might want to look into IBOutletCollection in order to achieve something similar to what you're looking for.
However, contrary to other answers here IBOutletCollection are ordered by how you link them in the interface builder.
Refer to this for IBOutletCollections: How can I use IBOutletCollection to connect multiple UIImageViews to the same outlet?
You can use IBOutletCollection. You can also use key-value coding:
for(NSUInteger i = 0; i < 13; i++)
{
[[self valueForKey:[NSString stringWithFormat:#"ce_%u", i]] setText: myArray[i]];
}
This will give you what you want.
The way I like to handle these situations is creating a temporary array containing all the text fields:
NSArray *allFields = #[_ce_1, _ce_2, _ce_3, ...];
NSInteger i = 0;
for (UITextField *tf in allFields)
{
tf.text = myArray[i];
i++
}
IBOutletCollection also work but sometimes it gets hard to figure out when you come back to your project which label is #3 or #5 and such... I find this works better for me usually :)

Showing a correct answer when someone selects a wrong one

Okay so I am new to objective-c and have used a couple different tutorials to build my first app that is a quiz. I had it completely done and I was somewhat satisfied.. then I realized that if someone selects the wrong answer it doesn't show them what the correct one is. So I have been trying to figure it out for a little bit. So I created a new label That stays hidden until an answer is chosen.
Is there an if statement I can use that can utilize the text I set buttons to? For example:
switch (QuestionSelected) {
case 0:
QuestionText.text = [NSString stringWithFormat:#"Question One"];
[Answer1 setTitle:#"Answer number 1" forState:UIControlStateNormal];
[Answer2 setTitle:#"Answer number 2" forState:UIControlStateNormal];
[Answer3 setTitle:#"Answer number 3" forState:UIControlStateNormal];
[Answer4 setTitle:#"Answer number 4" forState:UIControlStateNormal];
Answer1Correct = YES;
break;
This is what a question looks like within the category. My Answer1-4's are the buttons. And the Answer1Correct (depending on the answer) is a BOOL statement.
Is there a line that I can use in an if statement saying something along
if (Answer1Correct == YES) {
CorrectAnswer.text = [NSstring stringWithFormat:"#?", Answer1]; (The question mark would be for text the button was set to. I know that #i, can be used for integers but I don't know if I can use the text a button was set to)
I know that won't work but I am trying to wrap my head around how a lot of the valuables work. For instance, the problem I see is that within my questions I have BOOLs, and the only way it activates the BOOL is dependent on what they choose. For instance if they chose Answer2 in that previous statement it just sets runs my wrong answer one because this is what I have for the buttons
-(IBAction)Answer1:(id)sender{
if (Answer1Correct == YES) {
[self RightAnswer];
}
else{
[self WrongAnswer];
}
And then the same thing for the following three buttons.
I know I am probably all over the place but the reason I started this was to try and learn how to understand it. So how can I establish what is Correct when they choose the wrong one and how could I make the label show accordingly. I am not asking to be spoon fed, this is my first fully functional app that doesn't really have a full purpose other than myself trying to put things together and understand how things work together. So a little explanation or where I can read up on what is going on would be more appreciated then just an answer. I probably need to give a little more information so if I what I am asking doesn't make sense please ask. Thank you guys for the help.
Without seeing all of the code it's difficult, however one approach would be to have an array with the correct values in for each of the questions you would need to set a strong reference to this to ensure that it's not dealloc'd by arc.
in your .h
#property (nonatomic, strong)NSArray *correctAnswersArray;
#property (nonatomic, strong) IBOutlet UILabel *myLabelForIncorrectAnswer;
in your .m
-(void)viewDidLoad{
self.correctAnswersArray = [[NSArray alloc]initWithObjects:#"answer one", #"answer two", #"answer three", #"answer four",nil];
}
this can be reference using
self.correctAnswersArray[i];
where 'i' is the int value you require from the array - note that arrays are indexed starting at 0
With regards to your current conditional statement, you could set the label text using the array when you confirm if the answer is correct or incorrect so
if (Answer1Correct == YES) {
[self RightAnswer];
}
else{
[self WrongAnswer];
self.myLabelForIncorrectAnswer.text = self.correctAnswersArray[0];
}
and so on for each of the conditionals amending the array index number for each 'else' condition

UISegmentedControl and localization

I have a .xib file, with accompanying .strings files for different languages. The .xib file contains a label, and a UISegmentedControl.
When asking IB to localize the .xib file, I get the following .strings file:
"6.segmentTitles[0]" = "title1";
// ...More strings related to the segmented control...
"11.text" = "bla";
The 'bla' string belongs to the label.
Changing the 'bla` string is reflected in runtime, while changing the 'title1' string does not. Anyone knows why?
This question is not new, but as it is still without any answers I will add my solution as it may help others.
Generally, as it's mentioned above, it is an open bug that UISegmentedControl segment titles do not pick up localization strings.
- (void)viewDidLoad
{
...
// Locale will be picked automatically by NSBundle.
NSString *resourcePath =[[NSBundle mainBundle] pathForResource:#"MainStoryboard" ofType:#"strings"];
NSDictionary *resourceDict = [NSDictionary dictionaryWithContentsOfFile:resourcePath];
[self.segmentedControl setTitle:[resourceDict objectForKey:#"COo-BO-Ryl.segmentTitles[0]"] forSegmentAtIndex:0];
[self.segmentedControl setTitle:[resourceDict objectForKey:#"COo-BO-Ryl.segmentTitles[1]"] forSegmentAtIndex:1];
}
Where COo-BO-Ryl is the Object ID of segmentedControl.
Not very pretty, but does the job.

Resources