else if (setIndexPath == 4)
{
viewWithButtons.frame = CGRectMake(5, 40, 310, 390);
int ch = 6;
int cv = 6;
for ( int i = 0 ; i < cv ; ++i )
{
for ( j = 0 ; j < ch ; ++j )
{
btnMatrix = [[[UIButton alloc] initWithFrame:CGRectMake(10+pw*j, 51+ph*i, width, height)] autorelease];
btnMatrix.tag = i*ch+j;
btnMatrix.userInteractionEnabled = TRUE;
[btnMatrix addTarget:self action:#selector(changeImage:) forControlEvents:UIControlEventTouchDown];
[btnMatrix setBackgroundImage:imgDefaultBG forState:UIControlStateNormal];
[viewWithButtons addSubview:btnMatrix];
[arrButton addObject:btnMatrix];
}
}
}
[self.view addSubview:viewWithButtons];
I am generating buttons dynamically according to the selected matrix, like 4 * 4, 4 * 5, 4 * 6, 5 * 6, 6 * 6 etc....
Buttons are completely set as per the selected matrix from above (the code is for 6 * 6 matrix).
Now i have to set random background images of these buttons.
I am not able to set this kind of dynamic buttons' background.
is there any effective way to set backgrounds dynamically in random order for each separate buttons?
for (intimg = 1; intimg <= 15; intimg++)
{
arr_FirstSet = [[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"%d_f_2.png",intimg], nil];
}
for (intimgSET2 = 1; intimgSET2 <= 15; intimgSET2++) {
arr_SecondSet = [[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"%d_s_2.png",intimgSET2], nil];
}
I am adding images in this format to the NSMutableArray.
How to pick random image from the above used arrays. I have to match both arrays images & give the result on buttons. If both matches, suppose i press on one button it will display 1_f_2.png & on another button press it will display 1_s_2.png (as background)then both of these buttons will be removed from the view.
I had tried many logics to get this.
I am not getting exactly what i am interested to do on touchdown of button.
Please suggest any code snippet or link or code also.
Thanks in advance.
- (UIImage*)randomImage {
NSInteger imageCount = [[self imageArray] count]
return [[self imageArray] objectAtIndex:arc4random() % imageCount];
}
If you want matching pairs. Use dictionary with two images, or simple class holding two images.
Related
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 //
});
}
}
});
Hi everybody I making an Xcode App but have now been stuck for 2 days
in my app I have to create some images (which balls) in the amount stated by the user .
e,g if the user says make 10 10 balls should appear on the same screen at random locations I know to make location random but can't figure how to do it ... how do i connect to the interface builder ???
tried this
1.h
UIImageView *c[100];
1.m
for (int i = 0; i < "users value" ; i++) {
float x = (float)(67 + ((95 + 22) * i));
UIImageView *c[i] = [[UIImageView alloc]
initWithFrame:CGRectMake(x, 20.0f, 95.0f, 149.0f)];
[ballsView[i]setImage:[UIImage imageNamed:#"1.png"]];
ballsView[i].opaque = YES;
ballsView[i].hidden = NO; /* change to YES for distribution */
}
use this. hope helps
for (int i = 0; i < "users value" ; i++)
{
float x = (float)(67 + ((95 + 22) * i));
UIImageView *c[i] = [[UIImageView alloc]
initWithFrame:CGRectMake(x, 20.0f, 95.0f, 149.0f)];
c[i].image=[UIImage imageNamed:#"1.png"]];
[ballsView[i] addSubView:c[i]];
ballsView[i].opaque = YES;
ballsView[i].hidden = NO; /* change to YES for distribution */
}
I have many no. of buttons on my view and I want to shuffle the buttons(change in position) then how can I shuffle the button in my view.
use arc4random()
and change position of your buttons
In your .h file : UIButton *buttonsarray[10];
In your .m file :
// Make array of button using following way.I used this method to creates button and you can use yours.
- (void)viewDidLoad {
[super viewDidLoad];
float y = 5;
int x = 0;
int count = 0;
for (int i = 0 ; i < 10; i++)
{
count ++;
buttonsarray[i] = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonsarray[i].frame = CGRectMake(x, y, 100, 100);
[buttonsarray[i] setTitle:[NSString stringWithFormat:#"%d",i+1] forState:UIControlStateNormal];
x = x + 105;
[self.view addSubview:b[i]];
if(count == 3)
{
count = 0;
x = 0;
y = y+ 105;
}
}
}
// This function will soufflé your buttons
- (IBAction)btnClicked:(id)sender
{
int n = 10;
int swaper;
for (int i = 0 ; i < 10 ; i++)
{
int r = arc4random()%n;
if(r != swaper){
swaper = r;
CGRect r1 = buttonsarray[i].frame;
buttonsarray[i].frame = buttonsarray[swaper].frame;
buttonsarray[swaper].frame = r1;
n--;
}
}
}
Hope,this will help you..
You can do this
Make an Array with a group of CGPoints you will need to store the points as strings.
In the layoutSubViews method set something like this:
-(void)layoutSubViews{
[self.subviews enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
CGPoint newPoint = CGPointFromString([positionsArray objectAtIndex:idx]);
object.frame = CGRectMake(newPoint.x,newPoint.y,object.frame.size.width,object.frame.size.height);
}];
}
Then you will need to shuffle the positions in the positions array, you can see an example method here :How to Shuffle an Array
Now every time you need to Shuffle the positions you can call -(void)setNeedsLayout on your view.
There are more options but that was the first I thought of.
Good Luck
I have two for loops that add subviews to the view. The first one adds all the subviews, but the second for loop is not even executed! Whats Up?
- (void)createBoxes
{
for (int i; i<5; i++) {
GameBox *box = [[GameBox alloc] initWithFrame:CGRectMake((i * 64) + 7, 50, 50, 50)];
[self.view addSubview:box];
}
for (int e; e<5; e++) {
GameBox *box1 = [[GameBox alloc] initWithFrame:CGRectMake((e * 64) + 7, 107, 50, 50)];
[self.view addSubview:box1];
}
}
Proper way would be to initialize variables :
int i = 0;
int e = 0;
Otherwise you never know which value you'll get.
Why are you not initialising your loop variables? These are not standard for loops at all.
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.