drag drop issue in buttons - ios

i have created a few butttons via code
for(int i=0;i<2;i++)
{
UIButton *btn_pin = [[UIButton alloc] initWithFrame:CGRectMake(100*i,100*i, 20, 20)];
[btn_pin setBackgroundColor:[self colorFromHexString:#"5DBBAB"]];
[btn_pin setTitle:[NSString stringWithFormat:#"%d",[arr_product count]+1] forState:UIControlStateNormal];
[btn_pin.titleLabel setFont:[UIFont fontWithName:#"ProximaNova-Regular" size:10.0]];
btn_pin.layer.cornerRadius = 10.0;
btn_pin.layer.masksToBounds = YES;
btn_pin.layer.borderColor = [UIColor whiteColor].CGColor;
btn_pin.layer.borderWidth = 1.0;
btn_pin.userInteractionEnabled = TRUE;
btn_pin.titleEdgeInsets = UIEdgeInsetsMake(2, 0, 0, 0);
[btn_pin.layer setBorderColor:[UIColor whiteColor].CGColor];
[btn_pin addTarget:self action:#selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDown];
[btn_pin addTarget:self action:#selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[img_main addSubview:btn_pin];
[tbl_product reloadData];
}
and now i want to move those buttons aroudn via drag , i have created a drag drop already but how do i get the reference of this button .

in your loop:
btn_pin.tag = i;
and in imageMoved :
UIButton *btn_pin = (UIButton *)sender;
int refIndex = btn_pin.tag;

Related

getting element from array one by one and createbutton

I have a UItextFeild as search bar on clicking a tableview with suggestion open.now i want to click on that row and make a button with its label in below view as described in image .how can i acheive this
I want it one by one means when i click on row it create a button on view and UITableView hide then again i start searching and on again clicking on some other row make a button with different value of X.
_selectednames = [NSMutableArray arrayWithCapacity:self.sortedArray.count];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// MGContactItemsModel *model = _arrayForContacts[indexPath.row];
self.searchText.text = [NSString stringWithFormat:#"%#",[_sortedArray objectAtIndex:indexPath.row]];
self.tableView.hidden = YES;
[_selectednames addObject:_searchText.text];
for (int i; i < _selectednames.count ; i++) {
[self makeLabelsAndButtons:i];
}
now in my makeLabelsAndButtons funtion i done something like this
CGFloat xOffset = 10.0f;
int buttonCount = 0;
CGRect buttonFrame = CGRectMake(xOffset, 10.0, 50.0, 15.0);
NSString *nameString = [self.selectednames objectAtIndex:indexPath];
UIButton *_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
//_button.frame = CGRectMake(xOffset, 10.0, 50.0, 15.0);
for(int i=buttonCount; i >0;i++) {
xOffset = xOffset+10;
buttonFrame.origin.x += buttonFrame.size.width+xOffset;
}
_button.frame = buttonFrame;
buttonCount++;
_button.tag = indexPath;
//button.center = CGPointMake(xOffset, 10.0f);
//[button setBackgroundImage:[UIImage imageNamed:picString] forState: UIControlStateNormal];
[_button setTitle:self.selectednames[indexPath] forState:UIControlStateNormal];
_button.backgroundColor = [UIColor lightGrayColor];
_button.titleLabel.font = [UIFont fontWithName:#"Arial" size:8.0f];
_button.layer.cornerRadius = 10.0f;
_button.layer.masksToBounds = YES;
[self.buttonView addSubview:_button];
NSLog(#"name: %#", nameString);
also on clicking button it will removed.any help would be thankful.
It works for me.Please try it.
y=20;
x=20;
for (int i=0; i<[arrAssignUsers count]; i++) {
CGRect screenRect=[[UIScreen mainScreen]bounds];
CGFloat screenWidth=screenRect.size.width;
[arrBtnStatus addObject:[NSNumber numberWithBool:NO]];
NSString *strNames=[arrAssignUsers objectAtIndex:i];
stringsize=[strNames sizeWithAttributes:
#{NSFontAttributeName: [UIFont systemFontOfSize:20.0f]}];
btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
CGFloat m=x+stringsize.width+10;
CGFloat n=screenWidth-20;
if (m<=n) {
btn.frame=CGRectMake(x, y,stringsize.width,stringsize.height);
x=x+stringsize.width +10;
}
else
{
y=y+stringsize.height+10;
x=20;
btn.frame=CGRectMake(x, y,stringsize.width,stringsize.height);
x=x+stringsize.width+10;
}
[btn setTitle:self.arrAssignUsers[i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.tag=i;
[btn addTarget:self
action:#selector(notifyButtonPressedInEditTask:) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor=WHITE_COLOR;
btn.layer.cornerRadius=10;
[btn.layer setMasksToBounds:YES];
btn.layer.borderWidth=2.0f;
btn.layer.borderColor=[UIColor orangeColor].CGColor;
[self checkNotifybtnStatus:btn];
[cell addSubview:btn];
}
}
It may helps to you.Thank you
You can use UICollectionView to create buttons. All you need to do is
1. Create custom cell with button as you want
2. When you tap on table view cell, add item to array and reload UICollectionView
3. Show buttons and it will automatically add scrollview for you.
When you tap on UICollectionView cell, remove object from array and reload UICollectionView again.
This will more easy as compared to adding buttons and setting frames.
And design UICollectionView cell in such way that it will same length as your text, use NSLayoutConstraints for it. It will work fast and smooth.
Code will be minimum and reusable.
Try out this working code...
Take UIScrollView in your Xib for buttons
//Adding menu buttons
btnArray = #[#"SYMPTOMATIC", #"SOCIAL", #"VOCATIONAL", #"PERSONAL"];
[self addButtonsInScrollMenu:btnArray];
/**
* Add Menu Buttons in Menu Scroll View
*
* #param buttonArray Array containing all menu button title
*/
#pragma mark - Add Menu Buttons in Menu Scroll View
-(void) addButtonsInScrollMenu:(NSArray *)buttonArray
{
CGFloat buttonHeight = self.menuScrollView.frame.size.height;
CGFloat cWidth = 0.0f;
for (int i = 0 ; i<buttonArray.count; i++)
{
NSString *tagTitle = [buttonArray objectAtIndex:i];
CGFloat buttonWidth = [self widthForMenuTitle:tagTitle buttonEdgeInsets:kDefaultEdgeInsets];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(cWidth, 0.0f, buttonWidth, buttonHeight);
[button setTitle:tagTitle forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:#"Roboto-Regular" size:12.0f];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.menuScrollView addSubview:button];
UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, button.frame.size.height - kButtonBorderHeight, button.frame.size.width, kButtonBorderHeight)];
bottomView.backgroundColor = [UIColor darkTextColor];
bottomView.tag = 1001;
[button addSubview:bottomView];
if (i == 0)
{
button.selected = YES;
[bottomView setHidden:NO];
}
else
{
[bottomView setHidden:YES];
}
cWidth += buttonWidth;
}
NSLog(#"scroll menu width->%f",cWidth);
self.menuScrollView.contentSize = CGSizeMake(cWidth, self.menuScrollView.frame.size.height);
}

how to create action multiple dynamic buttons in ios?

i am creating multiple buttons dynamically now i want to set action for each buttons.
when i press buttons its show result on next screen in my application .
i am using FMDB library to read data from database.(read database according to id on buttons press). i know how to read database from database . i want only fetch data on buttons press and display that
data on table view .how to create action for each buttons?
this my buttons code :
-(void)DynamicButton:(NSMutableArray*)objectName
{
for(UIView *view in [scrollView subviews])
{
[view removeFromSuperview];
}
int yPossion = 100, xPossion = 44; int temp = 0;
for (int i = 0; i<[objectName count]; i++)
{
SMSCategory *cat = [objectName objectAtIndex:i];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTag:i];
[aButton setTranslatesAutoresizingMaskIntoConstraints:YES];
[aButton setBackgroundColor:[UIColor blackColor]];
[aButton setBackgroundImage:[UIImage imageNamed:#"icon-menu.png"]
forState:UIControlStateNormal];
[aButton setTitle:[NSString stringWithFormat:#"%d",i]
forState:UIControlStateNormal];
[aButton setFrame:CGRectMake(xPossion, yPossion, 70, 60)];
aButton.highlighted=YES;
[scrollView addSubview:aButton];
;
xPossion += aButton.frame.size.width+35;
temp++;
if (temp==3)
{
yPossion = aButton.frame.origin.y+aButton.frame.size.height+20;
temp = 0;
xPossion = 44;
yPossion += aButton.frame.size.width-15;
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width ,yPossion-
50)];
}
UILabel *label = [[UILabel alloc] init];
[label setTranslatesAutoresizingMaskIntoConstraints:YES];
[label setText:cat.Name];
[label setTextColor:[UIColor blackColor]];
label.font = [UIFont systemFontOfSize:12];
[label sizeToFit];
[label setFrame:CGRectMake(4, 44, 70, 60)];
[scrollView addSubview:label];
[aButton addSubview:label];
}
}
UIButton *test = [self buttonWithTitle:#"test" target:self selector:#selector(testDoit) frame:CGRectMake(self.view.center.x-50, 270.0, 100.0, 50.0) : 12];
- (UIButton *)buttonWithTitle:(NSString *)title target:(id)target selector:(SEL)inSelector frame:(CGRect)frame :(int)fontSize{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:frame];
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont boldSystemFontOfSize:fontSize];
button.userInteractionEnabled = YES;
button.titleLabel.textAlignment = UITextAlignmentCenter;
button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
button.titleLabel.shadowOffset = CGSizeMake (1.0, 0.0);
button.titleLabel.textColor = [UIColor blackColor];
button.showsTouchWhenHighlighted = YES;
button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleRightMargin|
UIViewAutoresizingFlexibleTopMargin|
UIViewAutoresizingFlexibleHeight|
UIViewAutoresizingFlexibleBottomMargin;
[button addTarget:self action:inSelector forControlEvents:UIControlEventTouchUpInside];
//[button autorelease];
return button;
}

Delete button on UIWebView

Im getting touch location coordinates from UIWebView and displays the button with coordinates. It's working with coordinates match. So, i need to increase the button. I didn't use Array for button. When i delete particular button from UIWebView it delete all the button. Shall i use NSMutableArray?. Here x & y is the touch point coordinates in float value
if(x && y){
NSLog(#"x is %f",x);
NSLog(#"y is %f",y);
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 addTarget:self
action:#selector(click1:)
forControlEvents:UIControlEventTouchDown];
[button1 setTitle:#"click" forState:UIControlStateNormal];
button1.frame = CGRectMake(x, y, 30.0, 20.0);
// [btnArray addObject:button1];
button1.tag = gTag;
gTag++;
[wbCont.scrollView addSubview:button1];
}
Delete the button:
-(void)recycle:(id)sender{
for(int i=1;i<gTag;i++){
[[wbCont.scrollView viewWithTag:i] removeFromSuperview];
}
-(void)click1:(id)sender{
UIButton *mainBtn = (UIButton *)sender;
int mainTag = mainBtn.tag;
txtview = [[UITextView alloc]initWithFrame:CGRectMake(0,0,320,568)];
txtview.font = [UIFont fontWithName:#"Helvetica" size:12];
txtview.font = [UIFont boldSystemFontOfSize:12];
txtview.backgroundColor = [UIColor whiteColor];
txtview.scrollEnabled = YES;
txtview.pagingEnabled = YES;
txtview.editable = YES;
txtview.tag = mainTag*1000;
for(int i=0; i<[textArray count];i++){
txtview.text=[textArray objectAtIndex:i];
}
[self.view addSubview:txtview];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(done:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Done" forState:UIControlStateNormal];
button.frame = CGRectMake(240.0, 20.0, 60.0, 40.0);
[txtview addSubview:button];
recyclebtn=[UIButton buttonWithType:UIButtonTypeCustom];
recyclebtn.tag = mainBtn.tag;
[recyclebtn addTarget:self action:#selector(recycle:) forControlEvents:UIControlEventTouchDown];
[recyclebtn setImage:[UIImage imageNamed:#"recycle.png"] forState:UIControlStateNormal];
recyclebtn.frame=CGRectMake(0, 10, 30, 30);
[txtview addSubview:recyclebtn];
}
-(void)recycle:(id)sender {
UIButton *btnTemp = (UIButton *)sender;
[[wbCont.scrollView viewWithTag:btnTemp.tag] removeFromSuperview];
int txtTag = btnTemp.tag*1000;
[[self.view viewWithTag: txtTag] removeFromSuperview];
}

Making series of buttons into an array ios

I have about 10-12 buttons that I'm adding to my scrollview. How can I make these into an array of buttons so that I can simplify the code? As of right now my code(only first three buttons are shown) is as follows:
UIButton *redButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
redButton.frame = CGRectMake(0, 0, 50, 30);
redButton.tag = 2;
[redButton setTitle:#"red" forState:UIControlStateNormal];
redButton.backgroundColor = [UIColor redColor];
[redButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[redButton addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.scollView addSubview:redButton];
UIButton *blueButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
blueButton.frame = CGRectMake(70, 0, 50, 30);
blueButton.tag = 3;
[blueButton setTitle:#"blue" forState:UIControlStateNormal];
blueButton.backgroundColor = [UIColor blueColor];
[blueButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[blueButton addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.scollView addSubview:blueButton];
UIButton *greenButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
greenButton.frame = CGRectMake(140, 0, 50, 30);
greenButton.tag = 5 ;
[greenButton setTitle:#"green" forState:UIControlStateNormal];
greenButton.backgroundColor = [UIColor greenColor];
[greenButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[greenButton addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.scollView addSubview:greenButton];
...
Can you see if this is possible
- (void)addButtonsToScrollView
{
NSArray *buttons = #[#{#"Tag":#2,#"Title":#"red",#"Color":[UIColor redColor]},
#{#"Tag":#3,#"Title":#"blue",#"Color":[UIColor blueColor]},
#{#"Tag":#5,#"Title":#"green",#"Color":[UIColor greenColor]}];
CGRect frame = CGRectMake(0.0f, 0.0f, 50.0f, 30.0f);
for (NSDictionary *dict in buttons)
{
UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
button.tag = [dict[#"Tag"] integerValue];
[button setTitle:dict[#"Title"]
forState:UIControlStateNormal];
button.backgroundColor = dict[#"Color"];
[button setTitleColor:[UIColor blackColor]
forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:button];
frame.origin.x+=frame.size.width+20.0f;
}
CGSize contentSize = self.scrollView.frame.size;
contentSize.width = frame.origin.x;
self.scrollView.contentSize = contentSize;
}
You can use:
[NSArray arrayWithObjects:redButton,greenButton,blueButton,nil];
But might be better off using a NSDictionary
[NSDictionary dictionaryWithObjectsAndKeys:
redButton, #"red",
blueButton, #"blue",
greenButton, #"green",
nil];
That way you can use the key to look them up instead of index.
Ok, we have the solution, try this code mate,
-(void) createButtons{
NSDictionary *buttonColors = #{#"Red":[UIColor redColor],#"Green":[UIColor greenColor],#"Black":[UIColor blackColor],#"Yellow":[UIColor yellowColor],#"Blue":[UIColor blueColor]};
int tag = 1;
for(NSString *key in buttonColors.allKeys){
UIColor *color = [buttonColors objectForKey:key];
UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect frame = CGRectMake(((tag -1)*70), 0, 50, 30);
[button setFrame:frame];
button.tag = tag;
button.backgroundColor = color;
[button setTitleColor:color forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:key forState:UIControlStateNormal];
[self.scrollView addSubview:button];
tag++;
}
}

UIScrollView with a large number of UIButtons

What I want is a UIView with lots of UIButtons in it. They get placed and arranged according to data stored in an NSArray. Because there are quite a lot of buttons they don't fit on the screen all at once. The user should be able to zoom out to see all the buttons or to zoom in to see details (the label on them) and to easily select them.
I tried two different approaches:
1) I constructed a UIView subclass, put the buttons in it and an instance of this View inside a UIScrollview.
Effect: I can access all Buttons via their tag and scrolling and zooming works fine. BUT I can't get the buttons to handle any events (press on them)...
2) I wrote a UIViewController with exactly the same functionality and added an instance of it to the UIScrollView.
Effect: I can press the buttons now, but scrolling and zooming have stopped to work.
Here the relevant Code of the View:
- (UIView *)initWithArray:(NSArray *)nArray{
self = [super init];
if (self) {
int count = [nArray count];
for (int i=0; i<count; i++) {
UIButton *button = [[UIButton alloc]
initWithFrame:(__some Code to place the Button__);
button.tag = i+1;
NSString *title = [[NSString alloc] initWithFormat:__code for generating Title__];
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[self addSubview:button];
}
}
return self;
}
And the Code for the matrixController:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *nArray = [[NSMutableArray alloc] __some code___];
int count = [nArray count];
for (int i=0; i<count; i++) {
UIButton *button = [[UIButton alloc]
initWithFrame:CGRectMake(__some Code to place the Button__];
button.tag = i+1;
NSString *title = [[NSString alloc] initWithFormat:__code for generating Title__];
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button];
}
}
And the code for the ScrollViewController:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 970)];
[self.view addSubview:scrollView];
[scrollView setBackgroundColor:[UIColor blackColor]];
//Zooming
[scrollView setMinimumZoomScale:0.25];
[scrollView setMaximumZoomScale:4.0];
[scrollView setDelegate:self];
// constructing the view
[scrollView addSubview:chartView];
[scrollView bringSubviewToFront:chartView];
OR
[scrollView addSubview:[matrixController view]];
How can I get this to work??
I'm able to get a scroll view containing multiple buttons to pan and zoom just fine, with the buttons still handling touch events:
- (void)didTapButton:(UIButton *)button
{
NSLog(#"Button %ld", (long)button.tag);
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3.0f, scrollView.frame.size.height * 3.0f);
scrollView.maximumZoomScale = 3.0f;
UIView *zoomView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, scrollView.contentSize.width, scrollView.contentSize.height)];
zoomView.backgroundColor = [UIColor whiteColor];
for (NSInteger index = 0; index < 100; index++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake((scrollView.frame.size.width / 2.0f) - 50.0f, 10.0f + (50.0f * (CGFloat)index), 100.0f, 30.0f);
button.tag = index;
[button setTitle:[NSString stringWithFormat:#"Button %ld", ((long)index + 1)] forState:UIControlStateNormal];
[button addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
[zoomView addSubview:button];
}
[scrollView addSubview:zoomView];
[self.view addSubview:scrollView];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView.subviews objectAtIndex:0];
}
EDIT: I said not to rely on tag in my comment below, yet I'm doing in here. :) It's merely so I could log the button number, so that part should be ignored.
for (int i=0; i<10; i++)
{
UIButton *scrollingbutton_outlet = [[UIButton alloc] init];
scrollingbutton_outlet = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img = [UIImage imageNamed:#"box_normal.png"];
scrollingbutton_outlet.frame = CGRectMake(0, 100, img.size.width, img.size.height);
[scrollingbutton_outlet setTitle:[NSString stringWithFormat:#"%d",i+1] forState: UIControlStateNormal];
scrollingbutton_outlet.tag=i+1;
[buttonArray addObject:[NSString stringWithFormat:#"%d",i+1]];
buttonArray = [[NSMutableArray alloc] init];
[scrollingbutton_outlet setBackgroundImage:img forState:UIControlStateNormal];
[scrollingbutton_outlet addTarget:self
action:#selector(scrollbuttonpress:)
forControlEvents:UIControlEventTouchUpInside];
scrollingbutton_outlet.frame = CGRectMake(img.size.width*i,0, img.size.width, scrollviewoutlet.frame.size.height);
[scrollviewoutlet addSubview:scrollingbutton_outlet];
width = scrollingbutton_outlet.frame.size.width;
height = scrollingbutton_outlet.frame.size.height;
scrollviewoutlet.contentSize = CGSizeMake(width*i+scrollingbutton_outlet.bounds.size.width+5, height);
}

Resources