So basically I am making an API call to retrieve a list of Charities. I then place all this in an array and set UIButtons dynamically.
I then allow the user to select the charity and display a view with that index's data.
My loop is here;
for (int i = 0; i < [self.imageArray count]; i++) {
NSDictionary *listRoles = [self.imageArray objectAtIndex:i];
NSString *charityName = [listRoles objectForKey:#"name"];
NSString *charityDescription = [listRoles objectForKey:#"description"];
NSString *charityImage = [listRoles objectForKey:#"image"];
UIImage *pImage=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:charityImage]]];;
UIButton *button = [[UIButton alloc] initWithFrame:frame];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:pImage forState:UIControlStateNormal];
[button setTag:i];
[self.scrollView addSubview:button];
I then have a clicked method;
- (void)buttonClicked:(UIButton*)button
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
CharityProfileViewController *cpvc = [storyboard instantiateViewControllerWithIdentifier:#"CharityProfile"];
[self presentViewController:cpvc animated:YES completion:nil];
}
How can I retrieve the index, I know that I could set random tags for the UIButton but how would I still know which one?
You can set the tag value of button as
[button setTag:i+1];
then in Button Action
- (void)buttonClicked:(UIButton*)button
{
NSLog(#"Button Tag : %d",button.tag);
NSLog(#"Selected Index Object : %#",[self.imageArray objectAtIndex:button.tag-1]);
}
You can either retrieve the a tag for your button by button.tag or add your buttons to an array and use then [buttonsArray indexOfObject:button] in your buttonClicked: method
Related
In my app I'm picking five images at a time and displaying in a small view and I have button if I click that button the 5 images in that view should go to next View Controller .but when I'm clicking that button only the image in 0th index is passing and remaining images are not passing.Hope someone helps me
And here is my code.This code is for passing from first view to second view and chosenImages is an array where all the picked images are saving
SecondViewController *secview=[[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
if (secview.imgView.tag==0) {
secview.img=[self.chosenImages objectAtIndex:0];
}
else if (secview.imgView1.tag==1)
{
secview.img=[self.chosenImages objectAtIndex:1];
}
else if (secview.imgView1.tag==2)
{
secview.img=[self.chosenImages objectAtIndex:2];
}
else if (secview.imgView1.tag==3)
{
secview.img=[self.chosenImages objectAtIndex:3];
}
else if (secview.imgView1.tag==4)
{
secview.img=[self.chosenImages objectAtIndex:4];
}
NSLog(#"%#",secview.img);
[self presentViewController:secview animated:YES completion:nil];
and in second view my code is:
if (imgView.tag==0)
{
imgView.image=img;
}
else if (imgView1.tag==1)
{
imgView1.image=img;
}
else if (imgView2.tag==2)
{
imgView2.image=img;
}
else if (imgView3.tag==3)
{
imgView3.image=img;
}
else if (imgView4.tag==4)
{
imgView4.image=img;
}
Update:In didFinishPickingMedia I wrote this code
images = [NSMutableArray arrayWithCapacity:[info count]];
for (NSDictionary *dict in info) {
if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypePhoto){
if ([dict objectForKey:UIImagePickerControllerOriginalImage]){
image=[dict objectForKey:UIImagePickerControllerOriginalImage];
[images addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
}
}
else {
NSLog(#"UIImagePickerControllerReferenceURL = %#", dict);
}
}
self.chosenImages = images;
[AllImages setHidden:NO];
previousButtonTag=1000;
scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 4, self.AllImages.frame.size.width, 104)];
int x =0.5;
for (int i=0; i<5; i++) {
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(x, 0, 100, 123);
button.layer.borderWidth=0.5;
button.titleLabel.font=[UIFont boldSystemFontOfSize:12];
button.titleLabel.textAlignment = NSTextAlignmentCenter;
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
// button.layer.borderColor=[[UIColor lightGrayColor]CGColor];
button.tag = i;
[button setBackgroundImage:[self.chosenImages objectAtIndex:i] forState:UIControlStateNormal];
[button addTarget:self action:#selector(OverLayMethod:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[scrollView addSubview:button];
x += button.frame.size.width;
x=x+6.0;
}
scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height);
scrollView.backgroundColor = [UIColor clearColor];
[self.AllImages addSubview:scrollView];
}
I solved my issue by declaring another array in second view.In first view we are saving picked images in one array .so that first view array objects = second view array and I passed that array in second view
my code in second view :
for (int i=0; i<[arr count]; i++) {
self.img=[arr objectAtIndex:i];
if (i==0)
{
imgView.image=img;
}
else if (i==1)
{
imgView1.image=img;
}
else if (i==2)
{
imgView2.image=img;
}
else if (i==3)
{
imgView3.image=img;
}
else if (i==4)
{
imgView4.image=img;
}
}
}
by this i'm getting correct output
I dunno that I got your question but my understanding is that you wanna send 5 images to another view. If so, define a new NSArray property such as 'images' in your SecondViewController and add it to where you go from first view to second
SecondViewController *secondVC = [SecondViewController new];
[secondVC setImages: self.chosenImages];
The problem with your code is that you're using if/else if block statement and if one of the conditions in that if/else if evaluate to true only you get into one of the block. Also, -objectAtIndex: only returns one item.
[self.chosenImages objectAtIndex:0];
Also, I'm sure that secview.img means that you have a UIImage property inside your SecondViewController.
I have a UIViewController with a tableView in it with CustomCells, the CustomCell load a PLAY Button for some of the cells, this button are generated within the CustomCell.m
- (UIButton *)videoButton {
if (!videoButton) {
videoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
videoButton.frame = CGRectMake(120, 319, 50, 30);
[videoButton setTitle:#"Play" forState:UIControlStateNormal];
[videoButton addTarget:self action:#selector(PlayClicked:) forControlEvents:UIControlEventTouchUpInside];
videoButton.backgroundColor= [UIColor clearColor];
[self.contentView addSubview:videoButton];
}
playIMG.hidden = false;
return videoButton;
}
- (IBAction)PlayClicked:(id)sender {
TableView *tvvc = [[TableView alloc] init];
[tvvc PlayBtnClicked:[NSString stringWithFormat:#"%d", [sender tag]]];
}
in my TableView.m
- (IBAction)PlayBtnClicked:(id)sender {
NSString *tag = [NSString stringWithFormat:#"%#", sender]; //OK
int tagNumber = [tag intValue]; //OK
NSString *Media = [arrayID objectAtIndex:tagNumber]; //Not Getting Result !
NSLog(#"%#", arrayID); //Array is empty when logging it from this action
}
arrayID is NSMutableArray
arrayID = [[NSMutableArray alloc] init];
[arrayID addObject#"123"];
[arrayID addObject#"321"];
[arrayID addObject#"231"];
soo i checked the array by adding an action button in my TableView to log the array and its not empty.
how can i fix the empty array which is actually not empty ?
I solved the problem in much easier way than the delegate thing
removed
[videoButton addTarget:self action:#selector(PlayClicked:) forControlEvents:UIControlEventTouchUpInside];
and the action in the custom cell .m file
- (IBAction)PlayClicked:(id)sender {
TableView *tvvc = [[TableView alloc] init];
[tvvc PlayBtnClicked:[NSString stringWithFormat:#"%d", [sender tag]]];
}
in the tableView.m i added edited the action i want to link to this
- (IBAction)PlayBtnClicked:(UIButton*)button {
NSLog(#"Button Clicked Tag: %d", button.tag);
}
and in the cellForRowAtIndexPath method i added the action to the button in the custom cell
if ([MediaType isEqualToString:#"video"]) {
cell.videoButton.hidden = NO;
cell.videoButton.tag = indexPath.row;
[cell.videoButton addTarget:self action:#selector(PlayBtnClicked:) forControlEvents:UIControlEventTouchUpInside];//here linking the action to the button
cell.playIMG.hidden = NO;
}
now everything is working very well :)
The array isn't your problem - you're breaking the model-view-controller nature of the table view controller and its cells. If you want a control in the cell to call a method in the table view controller, make the table view controller the delegate of the cell, and call the delegate method from the cell's button.
If the code you've posted is correct, then you're creating a new table view instance in the PlayClicked method, which is definitely not the way to do it.
As a minor stylistic point, the Cocoa convention of method naming is camelCase, with a lower-case initial letter.
Question: How do I target one of many dynamically created UIButtons so I can change its properties?
Background: I have a storyboard with a UIViewConroller. When this UIVC loads a UIScrollView is added, to which a UIImageView is placed that has an exhibition floor plan. Each exhibitor has an entry in a database that contains its location on the floor plan. When the UIVC is loaded a loop is run for all exhibitors and each one has a UIButton drawn on the UIIV. When a UIB is clicked the button background colour is changed (to confirm which exhibitor has been selected) and a UIAlertView is shown with information about that exhibitor. When the UIAV's 'cancel' (ok) button is pressed the UIAV closes and the background highlight colour that was applied previously should be removed but here is where I am having the problem. I am unable to target the UIButton so I can change its background colour.
What I have tried so far: As each button is created I am giving it a tag and a title and recording both in an array. When the 'cancel' button is pressed on the UIAlertView I have tried checking the tag in the array but I still cannot actually target the UIButton.
I was thinking something like this:
// obviously not correct syntax but the kind of thing I want
[exhibitorBtn(tag) setBackgroundColor:[UIColor greenColor]];
So, say I have 12 UIButtons all called exhibitorBtn but with different titles and tags:
Object ----- Name ---------- Title -------- Tag
UIButton -- exhibitorBtn -- Glaxo ------ 1
UIButton -- exhibitorBtn -- Porsche --- 2
UIButton -- exhibitorBtn -- Rolex ------- 3 < How would I target that button's properties?
Edit - added the code that creates the buttons just to clarify:
for (NSDictionary *dict in exhibitorStandArray) {
NSInteger currentPosition = [exhibitorStandArray indexOfObject:dict];
NSLog(#"Position in array = %li", (long)currentPosition);
if (![dict[#"locCoords"] isEqual: #""]) {
NSInteger buttonTag = [exhibitorStandArray indexOfObject:dict];
NSString *standNo = dict[#"locStandNo"];
NSMutableDictionary *buttonDictionary = [[NSMutableDictionary alloc] init];
[buttonDictionary setObject:[NSNumber numberWithInteger:buttonTag] forKey:#"buttonTag"];
[buttonDictionary setObject:standNo forKey:#"buttonStandNo"];
[_masterButtonList addObject:buttonDictionary];
NSString *locCoords = dict[#"locCoords"];
NSArray *tempArray =[locCoords componentsSeparatedByString:#","];
tlcTop = [[tempArray objectAtIndex:0] integerValue];
tlcLeft = [[tempArray objectAtIndex:1] integerValue];
brcTop = [[tempArray objectAtIndex:2] integerValue];
brcRight = [[tempArray objectAtIndex:3] integerValue];
buttonWidth = brcRight - tlcLeft;
buttonHeight = brcTop - tlcTop;
testBtn = [UIButton buttonWithType:UIButtonTypeCustom];
testBtn.frame = CGRectMake(tlcTop, tlcLeft, buttonHeight, buttonWidth);
testBtn.titleLabel.text = standNo;
testBtn.tag = buttonTag;
NSLog(#"UIButton Title = %#", testBtn.titleLabel.text);
NSLog(#"UIButton Tag = %li", (long)testBtn.tag);
testBtn.titleLabel.hidden = YES;
[testBtn addTarget:self action:#selector(displayInfo:) forControlEvents:UIControlEventTouchUpInside];
[_buttonArray addObject:testBtn];
[_imageView addSubview:testBtn];
}
}
Why can't you just use viewWithTag:
UIButton * button = (UIButton *)[self.scrollView viewWithTag:tag];
Your code probably looks something like this:
for (int i = 0; i < 5; i++)
{
UIButton *exhibitorBtn = [[UIButton alloc] initWithFrame:etc..];
exhibitorButton.tag = i;
[scrollView addSubview:exhibitorBtn];
}
Just change the loop so every button will be added to an array, too. Declare a NSMutableArray as a property: #property (strong, nonatomic) NSMutableArray *buttonsArray;
And #synthesize and initialise it in your init method. buttonsArray = [[NSMutableArray alloc] init]
Then, change the loop like I said above:
for (int i = 0; i < 5; i++)
{
UIButton *exhibitorBtn = [[UIButton alloc] initWithFrame:etc..];
exhibitorButton.tag = i;
[buttonsArray addObject:exhibitorBtn];
[scrollView addSubview:exhibitorBtn];
}
Finally, when you want to access the buttons:
for (int i = 0; i < [buttonsArray count]; i++)
{
UIButton *button = [buttonsArray objectAtIndex:i];
if (button.tag == 3) { // This is the button you wanted to target!
[button setHidden:YES];
}
}
Let's make it clear: you did set target and action of UIButton to controller's IBAction method and get the button as sender argument. Now you show UIAlertView and after it is dismissed, you want to send some message to that button, right?
Another method is to set delegate property for UIAlertView and respond to – alertView:clickedButtonAtIndex: delegate method. Trouble is that sender is lost at that point. You may use objc_setAssociatedObject to associate UIButton with UIAlertView and the retrieve it back when delegate method fires:
#import <objc/runtime.h>
static char myButtonKey = 0; // to use address as a key (value is irrelevant)
- (IBAction)buttonDidClick:(id)button
{
UIAlertView *alertView = <setup alert>;
[alertView setDelegate:self];
objc_setAssociatedObject(alertView, &myButtonKey, button, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
<show alert>;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UIButton *thatButton = objc_getAssociatedObject(alertView, &myButtonKey);
<use thatButton>;
}
Try to create button in this way and add selector to them :
for (int i = 0; i < 5; i++)
{
UIButton *button = [[UIButton alloc] initWithFrame:CGRectZero];
button.tag = i;
[button addTarget:self
action:#selector(buttonPressedMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
// add button to subview here
}
In method, just do whatever, you want to do :
- (void) buttonPressedMethod : (id) sender {
UIButton *selectedButton = (UIButton *)sender;
if (selectedButton.tag == 0) {
}
else if (selectedButton.tag == 1) {
}
else {
}
}
I have created 3 buttons at my top.take the offset of them and giving them same IBAction.
I want to change images at below according to the which button is pressed.
Further i have to pass these images to the another ViewControler, Here
i have got 3 arrays named bundle1 , bundle2 , bundle3 . in each array i have set of 5 images, in my Viewdidload as below,
bundle1 = #[#"1.png",#"2.png", #"3.png", #"4.png" ,#"5.png"];
bundle2 = #[#"6.png", #"7.png", #"8.gif",#"9.jpeg", #"10.jpeg" ];
bundle3 = #[#"11.jpg", #"12.jpg" ,#"13.jpeg", #"14.jpeg", #"15.jpeg"];
now i had take mutabledictionary, which had upper 3 arrays and i had give them a specific key
as below.
mutdict = [[NSMutableDictionary alloc] initWithObjectsAndKeys: bundle1 , #"key1" , bundle2 , #"key2" , bundle3 , #"key3" ,nil];
Now below is the code for IBAction of Upper 3 buttons, (Which is the same for all 3 buttons).
-(IBAction)morebtn:(id)sender
{
for (UIView *subview in [scroll subviews])
{
[subview removeFromSuperview];
}
arraydata = [[NSMutableArray alloc] init];
if ([sender tag] == 1) {
NSLog(#"hey btn1 clicked");
for (UIImage *b in arraydata)
{
[arraydata removeAllObjects];
}
arraydata = [mutdict valueForKey:#"key1"];
}
if ([sender tag] == 2) {
NSLog(#"hey btn2 clicked");
for (UIImage*b in arraydata)
{
[arraydata removeAllObjects];
}
arraydata = [mutdict valueForKey:#"key2"];
}
if ([sender tag] == 3) {
NSLog(#"hey btn3 clicked");
for (UIImage*b in arraydata)
{
[arraydata removeAllObjects];
}
arraydata = [mutdict valueForKey:#"key3"];
}
int x=10,y=300;
for (int k=0; k<arraydata.count; k++) {
NSLog(#"main called");
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:[arraydata objectAtIndex:k]] forState:UIControlStateNormal];
btn.tag=k;
btn.frame=CGRectMake(x, y, 100, 100);
[btn addTarget:self action:#selector(btn4images1:) forControlEvents:UIControlEventTouchUpInside];
x+=100;
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 300, self.view.frame.size.width, 200)];
scroll.contentSize = CGSizeMake(self.view.frame.size.width , 200);
[scroll addSubview:btn];
[self.view addSubview:scroll];
}
}
in upper code i trace which button is pressed and according to it i wil give inages in my NSArray named arraydata.
when i first time click on the all 3 upper buttons they show me image related to their array, but when i second time click on button1 (placed on the top), The images related to button3 are also shown in the Buttonbackground (palced at below part).
so on clicking button1 in below part button background images are overlaped. Seems like my NSMutableDictionary stores the data of last array. don't know what exactly happen.
I'm having issues with my button that is located in side my UITableViewCell. I'm using storyboard and connected my button through IB. Within my cellforRowatIndexPath I added an action to my button:
cell.likeBtnPressed.tag = indexPath.row;
[cell.likeBtnPressed addTarget:self action:#selector(userDidTapOnLikeButton:photo:) forControlEvents:UIControlEventTouchUpInside];
Below you will see what is called when the button is pressed:
-(void)userDidTapOnLikeButton:(UIButton *)button photo:(PFObject *)photo{
//Disable the button so users cannot send duplicat requests
[button setEnabled:NO];
[button setTintColor:[UIColor redColor]];
//Set the new state of the button
BOOL liked = !button.selected;
[button setEnabled:liked];
//Get the current number of likes the post have
NSString *originalButtonTitle = button.titleLabel.text;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
//Update the like count in the ECDCache
NSNumber *likeCount = [numberFormatter numberFromString:button.titleLabel.text];
if (liked) {
likeCount = [NSNumber numberWithInt:[likeCount intValue] + 1];
[[ECDCache sharedCache] incrementLikerCountForPhoto:photo];
}else{
if ([likeCount intValue] > 0) {
likeCount = [NSNumber numberWithInt:[likeCount intValue] - 1];
}
[[ECDCache sharedCache] decrementLikerCountForPhoto:photo];
}
//Add the current user as a liker of the photo in ECDCache
[[ECDCache sharedCache] setPhotoIsLikedByCurrentUser:photo liked:liked];
//Update the button label
[button setTitle:[numberFormatter stringFromNumber:likeCount] forState:UIControlStateNormal];
//Call the appropriate static method to handle creating/deleting the right object
if (liked) {
[ECDUtility likePhotoInBackground:photo block:^(BOOL succeeded, NSError *error) {
[button setEnabled:YES];
[button setTitleEdgeInsets:UIEdgeInsetsMake(-1.0f, 0.0f, 0.0f, 0.0f)];
[[button titleLabel] setShadowOffset:CGSizeMake(0.0f, -1.0f)];
if (!succeeded) {
// Revert the button title (the number) if the call fails
[button setTitle:originalButtonTitle forState:UIControlStateNormal];
}
}];
}
}
When ever I press the button I receive this:
-[UITouchesEvent objectId]: unrecognized selector sent to instance 0x15ee5de0
I'm not sure what I did wrong
The problem is that the second parameter your method is receiving is not a PFObject, but a UIEvent.
There are three types of selectors you can send into addTarget:action:forControlEvents::
#selector(a): This method takes no parameters.
#selector(a:): This method has one parameter which is the UIControl which received the control event.
#selector(a:b:): This method has two parameters, the UIControl which received the control event, and the UIEvent that triggered it.
Since you are only looking to get the button, you should have a signature like this:
-(void)userDidTapOnLikeButton:(UIButton *)button
{
PFObject *photo = [self someLogicToGetThePhotoFromTheButton:button];
...
you can't add a target with more than one parameter in UIButton selector. When the selector is called receive only the sender parameter, in that case, the UIButton object. So I recommend that you use:
[cell.likeBtnPressed addTarget:self action:#selector(userDidTapOnLikeButton:photo:) forControlEvents:UIControlEventTouchUpInside];
-(void)userDidTapOnLikeButton:(id)sender{
//Your code
}
And then retrieve the photo using the tag of the cell for example.
Good luck ;)