How to update an object's image in NSMutableArray? - iphone

I am trying to set the image of the face down card to the image that the value of the card is. The method in KCCard, image:, returns the image of the card.
- (UIImage *)image:(BOOL)yesOrNo
{
if (!yesOrNo) {
return [UIImage imageNamed:#"back-blue-150-3.png"];
} else {
return [UIImage imageNamed:[NSString stringWithFormat:#"%#-%#-150", [self suitAsString], [self valueAsString]]];
}
}
The code I am using in the deal method is as follows.
int lastDealerX = 437;
//int lastDealerY = 49;
int lastDealerTag = 0;
for (KCCard *aCard in dealerHand) {
if (lastDealerTag == 0) {
KCCardView *cardView = [[KCCardView alloc] initWithFrame:CGRectMake(lastDealerX, 49, 150, 215)];
cardView.backgroundColor = [UIColor blackColor];
cardView.image = [aCard image:NO];
cardView.tag = lastDealerTag;
[self.view addSubview:cardView];
lastDealerTag = lastDealerTag + 1;
lastDealerX = lastDealerX + 42;
} else {
KCCardView *cardView = [[KCCardView alloc] initWithFrame:CGRectMake(lastDealerX, 49, 150, 215)];
cardView.backgroundColor = [UIColor blackColor];
cardView.image = [aCard image:YES];
cardView.tag = lastDealerTag;
[self.view addSubview:cardView];
lastDealerTag = lastDealerTag + 1;
lastDealerX = lastDealerX + 42;
}
}
The KCCardView with tag 0 shows the card face down and the other card is face up. The problem is that when I want the face down card to show, it won't. Here is the show code.
- (IBAction)showCard:(id)sender {
for (UIView *view in self.view.subviews) {
for (KCCard *aCard in dealerHand) {
KCCardView *cardView = (KCCardView *)view;
if (cardView.tag == 0) {
cardView.image = [[dealerHand objectAtIndex:0] image:YES];
}
}
}
}
KCCard is an NSObject, KCCardView is a UIImageView, and dealerHand is an NSMutableArray.
Here is a video showing the build and run: http://aleckazarian.com/misc/Blackjack.mov
Here is the XCode project: http://aleckazarian.com/misc/Blackjack.zip

If you look at the connection in the nib you'll notice that it is connected to
showCard
this is a completely different method to
showCard:
In your class you implement - (IBAction)showCard:(id)sender; therefore you need to break the connection in Interface builder and reconnect it.
Update
The second time I ran your program I got
-[UIRoundedRectButton setImage:]: unrecognized selector sent to instance 0x68612e0
This looks like it's because you are iterating over the view's subviews and checking if 0 == tag. 0 is the default value for tag so essentially mostly every view will respond true unless you have explicitly set the tags to something else. The problem code it
for (UIView *view in self.view.subviews) {
for (KCCard *aCard in dealerHand) {
KCCardView *cardView = (KCCardView *)view;
if (cardView.tag == 0) { // <------- This is the bad check
cardView.image = [((KCCard *)[dealerHand objectAtIndex:0]) image:YES];
}
}
}
To fix this either do one of these (they are in order of my preference - I wouldn't go near 3 or 4 in this case):
Keep a reference to the cardView's in an array
Give the cardView's a non zero tag when they are created
Use respondsToSelector:
Test for the class `[cardView isKindOf:[UIButton class]];

The compiler does not know, what kind of object [dealerHand objectAtIndex:0] is, thus it cannot respond to image:. Try this:
cardView.image = [((KCCard *)[dealerHand objectAtIndex:0]) image:YES];

Related

Set Uibutton Random Position on UIView

I want to set 5 buttons on uiview at random position. Buttons need maintain some spacing to each other. I mean buttons should not overlap to each other.
All buttons set on UIView come from corners with rotation animation.
btn1.transform = CGAffineTransformMakeRotation(40);
btn2.transform = CGAffineTransformMakeRotation(60);
btn3.transform = CGAffineTransformMakeRotation(90);
btn4.transform = CGAffineTransformMakeRotation(30);
btn5.transform = CGAffineTransformMakeRotation(20);
I Can rotate buttons using above code but can you pls. help me for set buttons on random position with out overlapping by each other.
If points are fix than I can set buttons with animation by this code but I want random position of buttons.
[AnimationView moveBubble:CGPointMake(18, 142) duration:1 : btn1];
[AnimationView moveBubble:CGPointMake(118, 142) duration:1 : btn2];
[AnimationView moveBubble:CGPointMake(193, 142) duration:1 : btn3];
[AnimationView moveBubble:CGPointMake(18, 216) duration:1 : btn4];
Thanks in advance.
1st, add buttons to an NSArray, only to make things easier:
NSArray *buttonArray = #[btn1,btn2,btn3,btn4,btn5];
Now, this code tries to Arrange them at random positions.
int xTemp, yTemp;
for (int i = 0; i < 5; i++) {
while (YES) {
xTemp = arc4random_uniform(view.frame.size.width - [buttonArray[i] frame].size.width);
yTemp = arc4random_uniform(view.frame.size.height - [buttonArray[i] frame].size.height);
if (![self positionx:xTemp y:yTemp intersectsAnyButtonTillIndex:i inButtonArray:buttonArray]) {
[AnimationView moveBubble:CGPointMake(xTemp, yTemp) duration:1 : buttonArray[i]];
break;
}
}
}
Implement this function somewhere too:
- (BOOL) positionx:(int)xTemp y:(int)yTemp intersectsAnyButtonTillIndex:(int)index inButtonArray:(NSArray *)buttonArray {
//Again please change the < to <= , I'm sorry, doing too many things at once.
for (int i = 0; i <= index; i++) {
CGRect frame = [buttonArray[i] frame];
//EDIT : In the logic earlier, I had wrongly done a minus where I should have done a plus.
if ((xTemp > frame.origin.x && xTemp < (frame.size.width + frame.origin.x)) && (yTemp > frame.origin.y && yTemp < (frame.size.height + frame.origin.y))) return YES;
}
return NO;
OK this is a workign soln., I hope, just added something to WolfLink's answer. Check This.
for (UIButton *button in buttonArray) {
button.frame = CGRectMake(arc4random_uniform(view.frame.size.width - button.frame.size.width), arc4random_uniform(view.frame.size.height - button.frame.size.height), button.frame.size.width, button.frame.size.height);
while ([self button:button intersectsButtonInArray:buttonArray]) {
button.frame = CGRectMake(arc4random_uniform(view.frame.size.width - button.frame.size.width), arc4random_uniform(view.frame.size.height - button.frame.size.height), button.frame.size.width, button.frame.size.height);
}
//another function
-(BOOL)button:(UIButton *)button intersectsButtonInArray:(NSArray *)array {
for (UIButton *testButton in array) {
if (CGRectIntersectsRect(button.frame, testButton.frame) && ![button isEqual:testButton]) {
return YES;
}
}
return NO;
}
Based on spiritofmysoul.wordpress's code:
//in the function where you randomize the buttons
NSArray *buttonArray = #[btn1,btn2,btn3,btn4,btn5];
for (UIButton *button in buttonArray) {
float widthOffset = self.frame.size.width-button.frame.size.width;
float heightOffset = self.frame.size.height-button.frame.size.height;
button.frame = CGRectMake(arc4random()%widthOffset, arc4random()%heightOffset, button.frame.size.width, button.frame.size.height);
while ([self button:button intersectsButtonInArray:buttonArray]) {
button.frame = CGRectMake(arc4random(), arc4random(), button.frame.size.width, button.frame.size.height);
}
//another function
-(BOOL)button:(UIButton *)button intersectsButtonInArray:(NSArray *)array {
for (UIButton *testButton in array) {
if (CGRectIntersectsRect(button.frame, testButton.frame) && ![button isEqual:testButton]) {
return YES;
}
}
return NO;
}
Beware: This will work well for small amounts of buttons on a large space but as you add buttons and you run out of space, this method will take much longer to run. If there is not enough space for all the buttons, it will become an infinite loop.

iOS: Find all controls with the same tag

I have created a storyboard with 12 buttons, 12 smaller buttons and 12 labels.
It is like that:
btnBig1.tag = 1
btnSmall1.tag = 1
lbl1.tag = 1
btnBig2.tag = 2
btnSmall2.tag = 2
lbl2.tag = 2
etc...
Now when a procedure is called
- (IBAction)processButtonUpInside:(id)sender
{
UIButton *nButton = (UIButton*)sender;
int nInt = nButton.tag;
}
... I would like to do something with all 3 controls (big button, small button and label).
It should look like this (pseudo-code):
- (IBAction)processButtonUpInside:(id)sender
{
UIButton *nButton = (UIButton*)sender;
int nInt = nButton.tag;
UIButton *nButtonBig (UIButton*)CastFromTagID(nInt)
//do something with the big button
UIButton *nButtonSmall (UIButton*)CastFromTagID(nInt)
//do something with the small button
UILabel *nLabel (UILabel*)CastFromTagID(nInt)
//do something with the label
}
As you can see, the CastFromTagID is my "own invention". I don't know how I should actually do this.
Can somebody help?
Thank you very much.
You can use 3 a different starting point for each button family:
enum {
kTagFirstBigButton = 1000,
kTagFirstSmallButton = 2000,
kTagFirstLabel = 3000,
}
Assign the tags using them:
btnBig1.tag = kTagFirstBigButton + 1;
btnSmall1.tag = kTagFirstSmallButton + 1;
lbl1.tag = kTagFirstLabel + 1;
btnBig2.tag = kTagFirstBigButton + 2;
btnSmall2.tag = kTagFirstSmallButton + 2;
lbl2.tag = kTagFirstLabel + 2;
...
Now it's easy to find anything:
- (IBAction)processButtonUpInside:(id)sender
{
UIButton *nButton = (UIButton*)sender;
/* I'm not sure what button is `sender` here
If it's a big or small one you can guess
comparing its tag with the first tag
*/
int offset = nButton.tag;
UIButton *nButtonBig = (UIButton*)[view viewWithTag:kTagFirstBigButton + offset];
//do something with the big button
UIButton *nButtonSmall = (UIButton*)[view viewWithTag:kTagFirstSmallButton + offset];
//do something with the small button
UILabel *nLabel = (UILabel*)[view viewWithTag:kTagFirstLabel + offset];
//do something with the label
}
You shouldn't assign the same tag id to different views.
Indeed, do something like this:
btnBig1.tag = 11 btnSmall1.tag = 12 lbl1.tag = 13;
btnBig2.tag = 21 btnSmall2.tag = 22 lbl2.tag = 23;
Then consider the last digit of the tag id:
UIView *nView = (UIView *)sender;
if (lastDigit(sender.tag) == 3)
// this is a label
{
UIButton *nButtonBig = [nView.superview viewWithTag:nInt-2];
UIButton *nButtonSmall = [nView.superview viewWithTag:nInt-1];
UILabel *nLabel = (UILabel *)sender;
}
else if (lastDigit(sender.tag) == 2)
.....
where lastDigit(sender.tag) is a function that returns the last digit of a given integer.
In my case where I don't have a reference to the subviews that I want to edit under the same tag I just grab all of the subviews for a given view then loop through all of them and check the tag, if the tag match I execute some code. For example..
#define kSameViewTag 9500001
for (int i = 0; i < 10; i++) // add a bunch of same tag views //
{
UIView *someview = [UIView new];
someview.tag = kSameViewTag;
[YourParent addSubview:someview];
}
Then later on or whenever you need to loop through your views you can do.
NSArray *subviews = [[YourParent subviews] copy];
for (UIView *v in subviews)
{
if (v.tag == kSameViewTag)
{
// Do some code //
}
}
Now this may become an performance issue if you have a lot of subviews so you can always run it on the background thread then jump into the main thread to do UI updates. For Example:
NSArray *subviews = [[YourParent subviews] copy];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
for (UIView *v in subviews)
{
if (v.tag == kSameViewTag)
{
dispatch_async(dispatch_get_main_queue(), ^{
// Do Some UI Stuff Here //
});
}
}
});

Dynamically Add Textfield with tag On button Part-2

I want to add a textfield dynamically with tag so that it can give unique value every time. And than add those values and show on label. When I click button one textfield add "n" give the value, and that value adds to the previous value.
Value adding Successfully. But when I edit anything, change or give another value such as (10 instead of 12) the loop will run again because of this line
[Txt_New_Estimated addTarget:self action:#selector(C6Loop) forControlEvents:UIControlEventEditingDidEnd];
2nd problem is that when I add a new textfield then the previous textfield did not modify and do not add in rest of textfields... before adding a new textfield it works properly but when edit anything loop will run again.... i want to overCome this problem, so please check this code and give some possible solution. I am sending my code here Please check this code.
-(void)CreateTextFeildOnRun
{
if (tag ==0)
{
textPosY = 420;
}
for ( i =tag; i<= tag; i++)
{
Txt_New_Estimated = [[UITextField alloc]initWithFrame:CGRectMake(360, textPosY , 130, 65)];
Txt_New_Estimated.delegate = self;
Txt_New_Estimated.text=#"";
//[Txt_New_Estimated setTag:1234];
Txt_New_Estimated.tag = i;
Txt_New_Estimated.clearButtonMode = UITextFieldViewModeWhileEditing;
[Txt_New_Estimated addTarget:self action:#selector(C6Loop) forControlEvents:UIControlEventEditingDidEnd];
Txt_New_Estimated.placeholder = #"Estimated";
Txt_New_Estimated.font = [UIFont fontWithName:#"Arial" size:23];
Txt_New_Estimated.backgroundColor = [UIColor whiteColor];
Txt_New_Estimated.textAlignment = UITextAlignmentCenter;
[scrollview addSubview:Txt_New_Estimated];
}
}
-(void)C6Loop{
Txt_New_ONU.text=Txt_New_Estimated.text;
[self Calculate2];
}
-(void)Calculate2{
int y14=([Txt_New_Estimated.text intValue]);
y14=n;
n=d;
c14= y14+([Txt_New_Estimated.text floatValue]);
n = c14;
[self addest];
}
-(void)addest{
float c1= ([Txt_Engring_Est.text floatValue]) + ([Txt_Weddring_Est.text floatValue]) + ([Txt_Bridal_Est.text floatValue])+ ([Txt_Veil_Est.text floatValue])+ ([Txt_Shoe_Est.text floatValue])+n;
Txt_Total_Est.text = [[NSString alloc] initWithFormat:#"%.2f",c1];
}
Thank You.
Why don't you add those TextField when created to to an NSMutableArray. something like this:
-(void)createTextFieldOnRun
{
if (tag ==0)
{
textPosY = 420;
}
for ( i =tag; i<= tag; i++)
{
UITextField *Txt_New_Estimated = [[UITextField alloc]initWithFrame:CGRectMake(360,textPosY ,130,65)];
Txt_New_Estimated.delegate = self;
//....other code..
[scrollview addSubview:Txt_New_Estimated];
[textFieldArray addObject:Txt_New_Estimated];
}
}
and when calculating do something like this:
int result = 0;
for(UITextField *field in textFieldArray) // will iterate all UITextField which was added
{
result += [field.text intValue]; //or floatValue
}
NSLog(#"the result is %d", result); //here you have sum of all the text fields' text
It doesn't matter whether you change the value or not, it will recalculate all the values.

How to make animation when changing UIImageView

I wrote this code and made 2 buttons, next and back, to display a picture in an NSMutubleArray using an UIImageView.
-(IBAction) pictureNext:(id)sender;
{
if (pictureIndice==[promMUAraay count])
{
pictureIndice=0;
}
NSLog(#"indice : %d ",pictureIndice);
Promotion *prom = [[Promotion alloc]init];
prom =[promMUAraay objectAtIndex:pictureIndice];
promotionPicture.image = [UIImage imageWithData:prom.pPicture];
//promLabel.text=prom.pName;
NSLog(#"label : %#",prom.pName);
pictureIndice++;
}
-(IBAction) pictureBack:(id)sender;
{
if (pictureIndice==[promMUAraay count])
{
pictureIndice=0;
}
NSLog(#"indice : %d ",pictureIndice);
Promotion *prom = [[Promotion alloc]init];
prom =[promMUAraay objectAtIndex:pictureIndice];
promotionPicture.image = [UIImage imageWithData:prom.pPicture];
//promLabel.text=prom.pName;
if (pictureIndice==0)
{
pictureIndice=[promMUAraay count];
}
pictureIndice--;
}
This works, but is it possible to make a little animation when changing image?
There are a lot of animation options to choose from. Since you have a single UIImageView object, here is a "fade out/in" example (warning: not tested):
-(void)fadeView:(UIView *)thisView fadeOut:(BOOL)fadeOut {
// self.ReviewViewController.view.alpha = 1;
[UIView beginAnimations:#"fadeOut" context:nil];
[UIView setAnimationDuration:0.3];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
if (fadeOut) {
thisView.alpha = 0;
} else {
thisView.alpha = 1;
}
[UIView commitAnimations];
}
Then in your pictureNext and pictureBack you can change the line:
promotionPicture.image = [UIImage imageWithData:prom.pPicture];
to these 3 lines:
[self fadeView:promotionPicture fadeOut:YES];
promotionPicture.image = [UIImage imageWithData:prom.pPicture];
[self fadeView:promotionPicture fadeOut:NO];
Side note:
You could combine your pictureNext and pictureBack by having both buttons call the same selector and determining if you sender is the back or forward button.
-(IBAction)pictureMove:(id)sender {
if (sender == yourNextButton) {
//do next index math here
} else if (sender == yourBackButton) {
//do back index math here
}
.
.
.
//you image change code goes here
.
.
.
}

MKAnnotation shows it's Custom Marker graphic in simulator but not on device

I had this working very early, but then it stopped and I have no idea why. Here is the code:
- (void)updateMarkers:(NSMutableArray *)myAudioLocationVOArray
{
[self cleanupMarkers];
NSLog(#"UPDATE ALL MARKERS");
int tArrayCount = [myAudioLocationVOArray count];
for (int i=0; i< tArrayCount; i = i + 1)
{
AudioLocationVO* tAudioLocVO = [myAudioLocationVOArray objectAtIndex:i];
AudioAnnotation *tNewAnn = [[AudioAnnotation alloc] init];
tNewAnn.coordinate = CLLocationCoordinate2DMake(tAudioLocVO.latitude, tAudioLocVO.longitude);
// add current track if available
tNewAnn.audioLocationVORef = tAudioLocVO;
[self.mapView addAnnotation:tNewAnn];
[tNewAnn release];
}
}
- (void)cleanupMarkers
{
NSLog(#"REMOVE ALL MARKERS");
NSArray *tExistingPoints = self.mapView.annotations;
if ([tExistingPoints count] > 0)
{
[self.mapView removeAnnotations:tExistingPoints];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)myMapView viewForAnnotation:(id <MKAnnotation>)myAnnotation
{
if ([myAnnotation isKindOfClass:[AudioAnnotation class]])
{
AudioAnnotation *tAnnotation = (AudioAnnotation *)myAnnotation;
MKAnnotationView *tNewMarkerView = [[[MKAnnotationView alloc] initWithAnnotation:tAnnotation reuseIdentifier:nil] autorelease];
if(tAnnotation.audioLocationVORef.state == ANNOTATION_STATE_DROPPING)
{
NSLog(#"ADD DROP MARKER");
[tNewMarkerView setImage:[UIImage imageNamed:#"greenmarker.png"]];
tNewMarkerView.draggable = YES;
}
else
{
NSLog(#"ADD NEW MARKER");
[tNewMarkerView setImage:[UIImage imageNamed:#"newMarker.png"]];
tNewMarkerView.draggable = NO;
}
tNewMarkerView.frame = CGRectMake(tNewMarkerView.frame.origin.x,tNewMarkerView.frame.origin.y,20,26);
tNewMarkerView.canShowCallout = YES;
tNewMarkerView.enabled = YES;
// callout button
UIButton *tButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
tNewMarkerView.rightCalloutAccessoryView = tButton;
// cover art and title/subtitle
UIButton *tCover = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
if(tAnnotation.audioLocationVORef.trackVO==nil)
{
tAnnotation.title = #"Drop a Track";
tAnnotation.subtitle = #"Choose a track to drop";
[tCover setImage:[UIImage imageNamed:#"preCover.png"] forState:UIControlStateNormal];
}
else
{
tAnnotation.title = tAnnotation.audioLocationVORef.trackVO.songTitle;
tAnnotation.subtitle = tAnnotation.audioLocationVORef.trackVO.artist;
NSLog(#"ADD DATA MARKER %#", tAnnotation.title);
if(tAnnotation.audioLocationVORef.state==ANNOTATION_STATE_DROPPING){
tAnnotation.subtitle = #"Touch submit to Drop";
}
[tCover setImage:[tAnnotation.audioLocationVORef.trackVO getCoverArt] forState:UIControlStateNormal];
}
// make cover enabled to see song detail?
tCover.enabled = NO;
tNewMarkerView.leftCalloutAccessoryView = tCover;
[tCover release];
return tNewMarkerView;
}
return nil;
}
I tried to delete and add again the graphics as assets. I have been playing around a bit with the frame property. So far no luck.
And why the difference between simulator and device. I am using SDK 4.2... on iPhone 4
Make sure the image filenames match exactly with the resource names including upper/lower-case.
For example, if the resource is "GreenMarker.png", then "greenmarker.png" will only work on the simulator and not on the device.
Apple QA1697 (Why doesn't my device load a file that loads fine in the Simulator?) says:
Case-sensitivity: iPhone OS uses a
case-sensitive file system, unlike the
Simulator which uses a
case-insensitive file system by
default. Make sure the
case-sensitivity of resources accessed
within code matches the filename
case-sensitivity.
True, they should, but the Mac is case-preserving but also case-insensitive. The simulator runs on the Mac, so that's what you get.