How to realize action on multiple objects - iphone

I have some values of UIButtons. Every button I have created dynamically, by this code:
-(void)AddNewTable: (NSString *) tablePic: (NSString *) addedType {
CreatedTable *ct = [[CreatedTable alloc] init];
CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
NSString * uuidString = (__bridge NSString*)CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
CFRelease(newUniqueId);
UIImage *tableImage = [UIImage imageNamed: tablePic];
CGRect frameBtn = CGRectMake(160.0f, 160.0f, tableImage.size.width, tableImage.size.height);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: tableImage forState:UIControlStateNormal];
[button setFrame:frameBtn];
[button addTarget:self action:#selector (draggedOut:withEvent::) forControlEvents:UIControlEventTouchDragInside];
[button setTitle: [NSString stringWithFormat:#"%d", tables.count] forState: UIControlStateNormal];
ct.Id = [uuidString lowercaseString];
ct.posX = 160;
ct.posY = 160;
ct.isActive = true;
ct.Index = button.titleLabel.text;
ct.picture = [NSString stringWithFormat:#"tables/%#", tablePic];
ct.type = addedType;
ct.angle = 0.0;
[tables addObject:ct];
[hallView addSubview:button];
}
CreatedTable - is NSObject with string params of created buttons.
As you can see, I'm adding selector for every created button. I can move every button by this selector. Here is it's code:
- (IBAction)draggedOut: (id)sender withEvent: (UIEvent *) event: (NSSet *)touches {
CreatedTable = [tables objectAtIndex: selected.[titleLabel.text intValue]]
UIButton *selected = (UIButton *)sender;
selected.center = [[[event allTouches] anyObject] locationInView:hallView];
ct.posX = selected.center.x;
ct.posY = selected.center.y; // Here I'm changing params in ct.
}
Now I need to realize multi-select (select some value of buttons by tapping on them, to make some king of a group) and after that I need to move this group (all selected buttons) like one single object.
Any suggestions how to realize it?

When the user select multiple buttons one by one , mark them all the selected buttons by changing their background image . Now when the user starts dragging any one of the selected button create all Bigger view (with the clear background color) and add the copies(new buttons look alike the selected buttons) of all the selected buttons and add them on to the bigger view . Now changing position of original button (button getting dragged) , change the position of bigger view . It will give a look and feel like all the selected button are getting dragged . Also as soon as the bigger view dragging is stopped remove all the original selected buttons from the main view .
Hope it will help you.

you could set a different Tag to each buttons in AddNewTable like this:
[button setTag:];
and then in draggedOut you can find it by:
[sender tag]

Related

Set UIImageView background color upon user intervention

I am developing an application in which I have a horizontal scroller.
The scroller area comprise of different colors [which are nothing but UIImageView of colors].
Above the scroller area is a canvas [which is also a UIImageView].
Now, what I want is that when I select any color in the scroller area, it should set on the canvas.
It is pretty much like a color picker but I am already providing solid color options to be selected by the user.
How should I go about it ? Any sample Code to get me started or my mind kicking would be awesome ?
Thanx a ton
To implement it easily.
Use UIView not UIImageView
Set UIView backgroundColor and place it on the scroll
Set UITapGestureRecognizer on views,Refer here
when a purticular view is touched you have its instance of view which
is touched
get the background color of the view and set it in the scroller
Can you take UIButtons instead of UIImageViews inside your scrollView to show colours to be picked.
This will make the implementation easier. User will select any colour button and you will receive a callback in buttons action selector method. Then based on button tag or any property you can set the colour to your canvas.
In current implementation it will be tricky as you need to know the exact content offset where the tap is done to make out which UIImageView was pressed.
coding steps:
In your scroll view add UIButtons instead of UIImageView like:
UIButton* button = [[UIButton alloc] initWithFrame:someRect];
//put some tag to button
button.tag = someInt;
//add selector to each button to get callback
[view addTarget:self action:#selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
In the btnSelected method put this code:
-(IBAction)btnSelected:(id)sender;
{
UIButton* button = (UIButton*) sender;
if (button.tag == someInt) {
//do something like
canvas.backgroundColor = button.backgroundColor;
}
}
Instead of using UIImageViews or UIViews as suggested, I would put custom UIButtons for picking colors.
UIButton * colorButton;
//You will probably do these inside a for loop
colorButton = [UIButton buttonWithType:UIButtonTypeCustom];
[colorButton setFrame:CGRectMake(X, Y, buttonSize, buttonSize)];
[colorButton setTitle:#"" forState:UIControlStateNormal];
[colorButton setBackgroundColor:SOME_COLOR];
//Border visualization would be good if you are putting buttons side-by-side
[colorButton.layer setBorderWidth:1.0];
[colorButton.layer setBorderColor:[[UIColor darkGrayColor] CGColor]];
//You can use this tag to identify different buttons later
[colorButton setTag:INDEX+SOME_OFFSET]; //use loop index with some offset
[colorButton addTarget:self action:#selector(colorButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
-(void) colorButtonPressed:(UIButton *) sender
{
UIColor *selectedColor = sender.backgroundColor; //Or identify with sender.tag
[yourCanvas doSmtOnCanvas:selectedColor];
}
Initialize UIImageView then add gesture recognizer to all imageViews by calling this method.
- (void) setupImageView : (UIImageView *) imageView{
[imageView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(changeUIImageViewBackgroundColor:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:tapGestureRecognizer];
}
Now change color of canvasView within the selector by following way:
- (void) changeUIImageViewBackgroundColor : (UITapGestureRecognizer *) recognizer{
UIImageView *imageView = (UIImageView *)[recognizer view];
[canvasView setBackgroundColor:[imageView backgroundColor]];
}
You can put a tap gesture in your scrollview and whenever a tap is made in the scroll check whether the tap is on the imageview. (You will get whether the tap point in on imageview or not) and according you can change the image.
Second alternative is to take a custom button and handle on its click.
Hope it helps

table view with 3 buttons

I have a code for a table view with a cell that i created. Each cell holds three buttons. Each button should have an image as a background image.
This is my code for the CellForRowArIndex:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CellView" owner:self options:nil];
cell = nibLoadedCell;
}
for (int i = 1; i < 4; i++) {
//----- select the correct button from the cell
button = (UIButton *)[cell viewWithTag:(i)];
//----- cleaning the button, drawing it's corners, borders and giving it an action
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.layer.borderWidth = 2;
button.layer.cornerRadius = 5;
button.clipsToBounds = YES;
//------ Setting up the btnImg in order to place on the buttons (if is for checking i'm not taking pictures out of the array bounds)
UIImage *btnImg = [[UIImage alloc] init];
if (((indexPath.row * 3) + i - 1) < photosArray.count) {
btnImg = [photosArray objectAtIndex:((indexPath.row * 3) + i - 1)];
//NSLog(#"I am at indexpath.row: %d",indexPath.row);
//NSLog(#"I show image number %d in the array",[photosArray indexOfObject:btnImg]);
NSLog(#"Expression is: %d",((indexPath.row * 3) + i - 1));
}
//------ Add btnImg to the button
[button setBackgroundImage:btnImg forState:UIControlStateNormal];
//------ Giving special tags for each button in order to recognize it later
button.tag = imageButtonTag;
imageButtonTag--;
}
return cell;
}
As you can see, the for loop in my code is in charge of taking the images from my "photosArray" and placing them in the correct button in the cell.
The issue:
The first ~23 buttons get the correct images. Afterwards, (requires to scroll down a bit) starting image 24, the images start repeating. Image 24 is equal to image 0. Image 25 equals image 1 and so on…
I checked many things already. Everything looks fine! I even checked the photosArray indexOfObject and the numbers are correct.
You might think the array is made this way. I thought so too, but i assure you it's fine. I deleted my for loop and instead entered the following code:
//----- select the correct button from the cell
button = (UIButton *)[cell viewWithTag:(1)];
//----- cleaning the button, drawing it's corners, borders and giving it an action
[button setBackgroundImage:nil forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.layer.borderWidth = 2;
button.layer.cornerRadius = 5;
button.clipsToBounds = YES;
//------ Setting up the btnImg in order to place on the buttons
UIImage *btnImg = [photosArray objectAtIndex:indexPath.row];
//------ Add btnImg to the button
[button setBackgroundImage:btnImg forState:UIControlStateNormal];
This gives me a tableView with one button in each cell and the buttons get the right images…
One last thing I tried, is this code in my previous code right after creating btnImg:
if ([btnImg isEqual:[photosArray objectAtIndex:0]]) {
NSLog(#"Match found at: %d",[photosArray indexOfObject:btnImg]);
}
But nothing matches (although when i run it i see the images are the same).
This issue is driving me insane for about a week now and i would really appreciate it if someone could help me solve it.
Thanks!
Resetting of view tag is causing it not to be identified. Better way for you to identify each control is through subclassing or some other strategy, especially since you are using tags to get to the control

UIButton tags in iPhone

I have 2 button objects (UIButton *obtn,*ebtn), I have created 12 buttons(6 buttons with 6 tag values for obtn, 6 buttons with 6 tag values for ebtn)... I have a single btnClick: method for all those 12 buttons... when i click on a button of tag value 1... Iam getting the properties of selected button to another button by using sender... I want to get the properties of unselected button (Example obtn object with tag value 3) into another button How can i get it...?
My Direct Question is: How to assign the properties of a button with a tag value to another button when it is in unselected state...
It sounds like you want to be able to get a pointer to a button with a certain tag, and at the moment all you can do is get the button that has sent you the action.
The method you are looking for is viewWithTag:. This will get you a button (or any other view) with the tag you want. You pass this method to the superview of the button. So, in your action method (assuming all buttons are in the same superview):
UIView *superview = [sender superview];
UIButton *otherButton = [superview viewWithTag:3];
You then have sender, which is the button that was tapped, and otherButton, which is the button with tag 3. You can modify each one as you wish.
// Create textfield 5
btn_select_Country = [UIButton buttonWithType:UIButtonTypeCustom];
//btn_select_Client = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn_select_Country.frame = CGRectMake(BTN_X_CORD, 228, BTN_WIDTH, BTN_HEIGHT);
btn_select_Country.tag = 1;
// [btn_select_Client setTitle:#"Button One" forState:UIControlStateNormal];
[btn_select_Country addTarget:self action:#selector(countryListBtnTap:) forControlEvents:UIControlEventTouchUpInside];
btn_Image_Select_Country= [UIImage imageNamed:#"drop-down.png"];
[btn_select_Country setImage:btn_Image_Select_Country forState:UIControlStateNormal];
//btn_select_Client.tag=1205;
[scrollview addSubview:btn_select_Country];
- (IBAction)currencyListBtnTap:(id)sender;
{
button_Number = [sender tag];
[self allTextFeildResignFirstResponder];
}

UIButton created in loop not clickable

Here's my problem:
I add several buttons to a scrollview in a loop:
for (int i = 0; i < [allEventArray count]; i++) {
UIButton *eventButton = [UIButton buttonWithType:UIButtonTypeCustom];
eventButton.tag = i + 10;
UIImage *normalImage = [UIImage imageNamed:#"rosterButton.png"];
normalImage = [normalImage
stretchableImageWithLeftCapWidth:normalImage.size.width/2
topCapHeight:normalImage.size.height/2];
[eventButton setBackgroundImage:normalImage
forState:UIControlStateNormal];
[eventButton addTarget:self action:#selector(gotoThisAirport)
forControlEvents:UIControlEventTouchUpInside];
[drawRosterView addSubview:eventButton];
}
I set the frame size and other stuff later on by opening the tag property.
All the buttons appear as advertised.
The problem is that only the first button in the view responds to clicking. The others just sit there chilling, doing nothing. How can this be? It driving me absolutely crazy!
Please help!
It sounds like your UIButton isn't fully inside your UIView.
Try only adding the UIButton to UIView and set a background color for the UIView to something like [UIColor greenColor]. You should then see if the UIButton is partially outside of the UIView.
Also, if you add an NSLog("click") call in -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; of the UIView, then you can click anywhere on screen and get feedback if the UIView detects the touch or not.

How to remove the button tag duplicates in iPhone?

I have created buttons are programmatically and set the button tags.
View Did Load:
I have created one view and added the buttons are sub view into that view.
-(void) viewDidLoad
{
self.answerSubview = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 320, 230)];
self.answerSubview.backgroundColor = [UIColor clearColor];
[self.view addSubview:answerSubview];
[self buttonCreation];
}
-(void) buttonCreation{
int x =100;
for(i = 0; i < 4; i++) {
UIButton *answerBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[answerBtn setFrame:CGRectMake(40, x, 260, 40)];
answerBtn.tag = ar[i];
[answerBtn setTitle:[answerList objectAtIndex:i] forState:UIControlStateNormal];
[self.answerSubview addSubview:answerBtn];
x = x+50;
}
}
And i want to set the background image for the certain index of the button, when the time is finished,
-(void) timerFinished
{
for (UIView* view in [self.answerSubview subviews]) {
if([view isKindOfClass:[UIButton class]]){
UIButton *btn = (UIButton*)view;
NSLog(#"button value %d", btn.tag);
if(btn.tag == correctIndex)
{
[btn setBackgroundImage:[UIImage imageNamed:#"selected_correct_answer.png"] forState:UIControlStateNormal];
}
}
}
}
But every time,the tag values are added into the stack,I have randomly generated numbers and set it into the button tags, so now it comes like,
REsult:
First time, Second time,
0 0
1 1
3 3
2 2
3
1
2
0
(Actually my expected result is 3 1 2 0).
So how could i remove the previous tag values and get the correct tag values at a time.
Please help me out.
There are two things you can consider doing. Since the only thing that changes on every iteration is the text on the button. You could probably reuse them. Setup once and put them in ivars. On every iteration, change the text and tag of the button.
If that is not the route you're interested in, you can probably send removeFromSuperview message to those four button objects.
However, the first method is a better approach in my opinion because it will do away with continuous creation-destruction cycle.