My code is the following,
for (int i=0; i < kNumberOfTitles; ++i) {
UILabel * aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, i*10, 100, 30)];
[_titles addObject:aLabel];
NSAssert([_titles objectAtIndex:0] != nil, #"wrong 1");
}
NSAssert([_titles objectAtIndex:0] != nil, #"wrong 2");
((UILabel *)[_titles objectAtIndex:0]).text = #"Tel";
((UILabel *)[_titles objectAtIndex:1]).text = #"Add";
for (UILabel* aLabel in _titles) {
[self.view addSubview:aLabel];
}
Before first NSAssert, I just add an new label and then retrieve it, I found it's nil.
What's the problem?
I suspect the problem is that you're not actually creating the array so it's nil if it's an instance variable (or you use ARC) or some garbage value if it's a simple local variable if you don't use ARC. You must do
_titles = [[NSMutableArray alloc] init];
before trying to add items to the array.
Related
I have dynamic UISwitches that I create using the for loop. I give the tag value for each control to have an identity. I want to reach these values later, but I get 0 each time. What I'm doing wrong?
for(int i = 0;i < self.extraArray.count; i++) {
ProductPropertiesModel *model = (ProductPropertiesModel *)[self.extraArray objectAtIndex:i];
UISwitch *switchButton = [[UISwitch alloc] initWithFrame:CGRectZero];
switchButton.translatesAutoresizingMaskIntoConstraints = false;
switchButton.tag = [model Id];
[switchButton setOn:NO];
[self.view addSubview:switchButton];
[switchButton addTarget:self action:#selector(setState:)
forControlEvents:UIControlEventValueChanged];
}
-(void)setState:(id)sender
{
UISwitch *uiswitch = (UISwitch *)sender;
NSInteger tagInteger= uiswitch.tag;
NSLog(#"%#", [NSString stringWithFormat:#"%li",(long)tagInteger]);
}
The tag value is 0, but that is wrong.
I found the problem. I just made a typo. I noticed that I was writing a typo when I was taking json data. I noticed Id writing instead of id.
NSError *jsonError;
self.extraJsonArray = nil;
self.extraJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonError];
self.extraArray = [[NSMutableArray alloc]init];
for(int j = 0;j < self.extraJsonArray.count;j++){
#try {
//NSInteger *Id = (NSInteger *)[(NSNumber *)[[self.extraJsonArray objectAtIndex:j]objectForKey:#"Id"] integerValue]; Problem is here
NSInteger *Id = (NSInteger *)[(NSNumber *)[[self.extraJsonArray objectAtIndex:j]objectForKey:#"id"] integerValue];
double price = (double)[(NSNumber *)[[self.extraJsonArray objectAtIndex:j] objectForKey:#"price"] doubleValue];
NSString *property =(NSString *)[[self.extraJsonArray objectAtIndex:j] objectForKey:#"property"];
ProductPropertiesModel *model = [[ProductPropertiesModel alloc] init];
[model setId:Id];
[model setProperty:property];
[model setPrice:price];
[model setChecked:#"No"];
[self.extraArray addObject:model];
} #catch (NSException *exception) {
NSLog(#"%#", exception.reason);
} #finally {
}
}
I would recommend instead setting [model id] as tag, you should set array index as tag.
and then later on in the switch control method, you can get the value from array index, using the code.
ProductPropertiesModel *model = (ProductPropertiesModel *)[self.extraArray objectAtIndex:i];
Hope this helps.
Try this:
for (int i =0; i < self.extraArray.count; i++) {
CGRect frame = CGRectMake(x, y, height, width);
UISwitch *switchControl = [[UISwitch alloc] initWithFrame:frame];
//add tag as index
switchControl.tag = i;
[switchControl addTarget:self action:#selector(setState:) forControlEvents: UIControlEventValueChanged];
[switchControl setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:switchControl];
y= y+50.0;
}
- (IBAction) setState: (UISwitch *) sender {
NSLog(#"no.%d %#",sender.tag, sender.on ? #"On" : #"Off");
//use sender.tag , you know which switch you selected
}
I am trying to create a custom label for coreplot using NSAttributedString. Everything works fine when my strings have 1 line.
The problem occurs when I want to have 2 line strings using code:
NSMutableAttributedString* remFinalLabel = [remAttrStr mutableCopy];
NSMutableAttributedString *newLineAttrStr = [[NSMutableAttributedString alloc]initWithString:#"\n"];
[remFinalLabel appendAttributedString:newLineAttrStr];
[remFinalLabel appendAttributedString:rem2AttrStr];
The result looks like that: not even first string is properly displayed. How can I set the number of lines in custom label?
My code to create labels:
NSMutableArray* labelsStrings = [NSMutableArray arrayWithObjects:strAttr1, strAttr2, strAttr3, nil];
NSMutableArray *ticksLocations = [NSMutableArray arrayWithObjects:#0.5, #1.5, #2.5, nil];
NSMutableArray* customLabels = [[NSMutableArray alloc] init];
NSUInteger labelLocation = 0;
#try {
for (NSNumber *tickLocation in ticksLocations) {
NSAttributedString* currentLabel = [labelsStrings objectAtIndex:labelLocation];
CPTTextLayer *txtLayer = [[CPTTextLayer alloc] initWithAttributedText:currentLabel];
CPTAxisLabel* newLabel = [[CPTAxisLabel alloc] initWithContentLayer:txtLayer];
newLabel.tickLocation = tickLocation;
newLabel.offset = 0.0f;
newLabel.alignment = CPTAlignmentLeft;
[customLabels addObject:newLabel];
labelLocation++;
}
}
#catch (NSException * e) {
DLog(#"An exception occurred while creating date labels for x-axis");
}
#finally {
y.axisLabels = [NSSet setWithArray:customLabels];
}
y.majorTickLocations = [NSSet setWithArray:ticksLocations];
You can achieve this by setting the frame of content layer of your CPTAxisLabel.
Try this:-
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:#"Your Text Here" textStyle:#"Text Style"];
[label.contentLayer setFrame:CGRectMake(0, 0, 40, 40)]; //change this frame according to your requirement
Happy Coding..!!
I am creating 5 UIButtons, using a for loop, and would like to put 5 different avatars on the UIButtons. However, only one avatar is filling all 5 buttons.
The code I have constructed looks like the following,
// create a subview for avatar buttons
UIView *avatarView = [[UIView alloc] init];
avatarView.frame = CGRectMake(20, 125, 280, 100); // don't mess with these values.
// avatarView.layer.borderColor = [UIColor redColor].CGColor;
// avatarView.layer.borderWidth = 3.0f;
[self.view addSubview:avatarView];
UIScrollView *avatarScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
avatarScroll.contentSize = CGSizeMake(500, 500);
avatarScroll.scrollEnabled = YES;
[avatarView addSubview:avatarScroll];
// fetch Data from Core Data
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Account" inManagedObjectContext:_managedObjectContext];
[fetchRequest setEntity:entity];
// fetch records and handle error
NSError *error;
NSArray *results = [_managedObjectContext executeFetchRequest:fetchRequest error:&error];
// sort results array by lastLogin
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:#"lastLogin" ascending:NO];
NSArray *sortedArray = [NSArray arrayWithObject:sort];
NSArray *sortedArray2 = [results sortedArrayUsingDescriptors:sortedArray];
// how to remove values from NSArray
NSArray *lastLoginArray = [sortedArray2 valueForKey:#"lastLogin"];
// NSLog(#"lastLoginArray = %#",lastLoginArray);
// make an array that only hold 5 values
// NSArray *last5LoginArray;
// for (int i=5; i<[lastLoginArray count]; i++) {
// [last5LoginArray addObject:[lastLoginArray objectAtIndex:i]];
// }
// NSLog(#"last5LoginArray = %#",last5LoginArray);
// NSArray *last5LoginArray = [NSArray arrayWithObjects:lastLoginArray count:4];
// NSArray *last5LoginArray = [NSArray arrayByAddingObjectsFromArray:lastLoginArray count:4];
NSMutableArray *last5LoginArray = [[NSMutableArray alloc] initWithArray:[lastLoginArray subarrayWithRange:NSMakeRange(0, 5)] ];
NSLog(#"last5LoginArray = %#",last5LoginArray);
// NSLog(#"sortedArray2 = %#",sortedArray2);
CGFloat staticX = 0;
CGFloat staticWidth = 80;
CGFloat staticHeight = 80;
CGFloat staticPadding = 5;
// need to put the avatars stored in sortedArray2 in the scrollView
for ( int i = 0; i < 5; i++) {
// do additional loading for avatars
UIButton *avatarButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// the last two values control the size of the button
// avatarButton.frame = CGRectMake(0, 0, 80, 80);
[avatarButton setFrame:CGRectMake((staticX + (i * (staticHeight + staticPadding))),5,staticWidth,staticHeight)];
// make corners round
avatarButton.layer.cornerRadius = 40; // value varies -- // 35 yields a pretty good circle.
avatarButton.clipsToBounds = YES;
// create a stock image
UIImage *btnImage = [UIImage imageNamed:#"HomeBrewPoster1.jpg"];
Account *anAccount;
for ( anAccount in results) {
if([last5LoginArray containsObject:anAccount.lastLogin]) {
NSLog(#"anAccount.lastLogin = %#",anAccount.lastLogin);
UIImage *avatarImg = [UIImage imageWithData:anAccount.avatar ];
// apply avImg to btn
[avatarButton setBackgroundImage:avatarImg forState:UIControlStateNormal];
}
}
if (btnImage == nil) {
NSLog(#"can't find HomeBrewPoster1.jpg");
// apply stock image to button(s)
[avatarButton setBackgroundImage:btnImage forState:UIControlStateNormal];
} else {
}
// this should add 5x buttons
[avatarScroll addSubview:avatarButton];
}
Your innermost for-in loop always sets the last matching avatar to the button, because the current value of i makes absolutely no difference to what the innermost loop does.
You should move the nested loop outside the first loop to prepare the five avatars upfront, and then use i to index into the array of avatars, like this:
NSMutableArray *avatars = [NSMutableArray arrayWithCapacity:5];
for ( anAccount in results) {
if([last5LoginArray containsObject:anAccount.lastLogin]) {
NSLog(#"anAccount.lastLogin = %#",anAccount.lastLogin);
UIImage *avatarImg = [UIImage imageWithData:anAccount.avatar ];
[avatars addObject:avatarImg];
}
}
NSAssert(
avatars.count == last5LoginArray.count
, #"The loop is expected to find as many avatars as there are items in last5LoginArray"
);
for ( int i = 0; i < 5; i++) {
...
// Check that we have enough logins
if (i < last5LoginArray.count) {
[avatarButton setBackgroundImage:avatars[i] forState:UIControlStateNormal];
}
}
Declare button as an array
NSArray *arrImages = [NSArray arrayWithObjects:#"1.jpg",#"2.jpg",#"3.jpg",#"4.jpg",#"5.jpg", nil];
UIButton *button[5];
button[i] = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button[i] addTarget:self action:#selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];
[button[i] setBackgroundImage:[UIImage imageNamed:[arrImages objectAtIndex:i]] forState:UIControlStateNormal];
[button[i] setTitle:#"Title" forState:UIControlStateNormal];
button[i].frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[avatarScroll addSubview:button[i]];
You can name your images like "HomeBrewPoster0", "HomeBrewPoster1", etc. And add this to choose right image in your loop:
UIImage *btnImage = [UIImage imageNamed:[NSString stringWithFormat:#"HomeBrewPoster%ld.jpg", i]];
I am trying to show no. of images in different image view. if i have 3 image then i want show in 3 diff. UIImageView in my View. Here is my code that only shows one image. What have i missed here?
NSArray * _thumbnails = _claimReport.photos.allObjects;
if (_thumbnails.count >0)
{
NSMutableArray *imageViews = [[NSMutableArray alloc] init];
for (int i =1; i<_thumbnails.count; i++)
{
UIImageView *newImageViews = [[UIImageView alloc]initWithFrame:CGRectMake(40, 1700*i+20, 670, 700)];
LAPhoto *photo = [_thumbnails objectAtIndex:i];
[newImageViews setImage:[UIImage imageWithData:photo.photo]];
NSLog(#" newImageViews %# ",newImageViews);
[imageViews addObject:newImageViews];
[self.formView addSubview:newImageViews];
}
NSLog(#"Count %d" , imageViews.count);
}
You can add the images in a scroll view:
self.scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
[self.formView addSubview:self.scrollView];
NSArray * _thumbnails = _claimReport.photos.allObjects;
if (_thumbnails.count >0)
{
NSMutableArray *imageViews = [[NSMutableArray alloc] init];
for (int i =0; i<_thumbnails.count; i++)
{
UIImageView *newImageViews = [[UIImageView alloc]initWithFrame:CGRectMake(40, 800*i+20, 670, 700)];
LAPhoto *photo = [_thumbnails objectAtIndex:i];
[newImageViews setImage:[UIImage imageWithData:photo.photo]];
NSLog(#" newImageViews %# ",newImageViews);
[imageViews addObject:newImageViews];
self.scrollView.contentSize = CGSizeMake(670, 800 *i + 800);
[self.scrollView addSubview:newImageViews];
}
NSLog(#"Count %d" , imageViews.count);
}
Now I create is as follow (my file.h):
UIImageView *pic1, *pic2, *pic3, *pic4, *pic5, *pic6, *pic7, *pic8, *pic9, *pic10;
Then in my (file.m):
UIImageView *pic1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#”picName.png”]];
UIImageView *pic2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#”picName.png”]];
……
UIImageView *pic10 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#”picName.png”]];
I need many instances of UIImageView (number triggered by other factors) of only one in this case picture.
Is there any way to create multiple instances of UIImageView automatically in my file.m, somehow as follow?:
for (int x; (x=10); x++)
{
UIImageView * pic[x] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myPic.png"]];
}
This example doesn’t work but I’d like to show what I want to program.
Of course you can - that is what the arrays are for:
NSMutableArray *pics = [NSMutableArray array];
for (int i = 0 ; i != 10 ; i++) {
[pics addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myPic.png"]]];
}
In case the name of the picture depends on the index, use NSString's stringWithFormat to produce the name of the picture - for example, you can do it like this:
NSMutableArray *pics = [NSMutableArray array];
for (int i = 0 ; i != 10 ; i++) {
NSString *imgName = [NSString stringWithFormat:#"myPic%d.png"];
[pics addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:imgName]]];
}
If the names of images follow a standard pattern you can just loop over the required amount and build the image name dynamically using the index.
If the names of the images do not follow a standard patten you can chuck them in an array and loop over them
NSArray *imageNames = #[
#"alarm.jpg",
#"bell.jpg",
#"car.jpg"
];
NSMutableArray *imageViews = [[NSMutableArray alloc] init];
for (NSString *imageName in imageNames) {
[imagesViews addObject:({
[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
})];
}
Completely optional - further reading
You could write yourself a simple category that abstracts this looping and collecting the results into a new array. This would allow you to simply write
NSArray *imageNames = #[
#"alarm.jpg",
#"bell.jpg",
#"car.jpg"
];
NSArray *imageViews = [imageNames pas_arrayByMappingWithBlock:^(id obj){
return [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
}];
The category to do that would look something like this:
#interface NSArray (PASAdditions)
- (NSArray *)pas_arrayByMappingWithBlock:(id (^)(id obj))block
#end
#implementation NSArray (PASAdditions)
- (NSArray *)pas_arrayByMappingWithBlock:(id (^)(id obj))block
{
NSMutableArray *result = [[NSMutableArray alloc] init];
for (id obj in self) {
[result addObject:block(obj)];
}
return [result copy];
}
#end