How do I access a button in a subview - ios

I want to implement this fade in/out in a horizontal scroll I have, but I can't figure out how to access my button that is in a subview of my UIScrollView. This is what I'm trying to implement Fade in/out stackoverflow.
This is my code...
Game *game = (Game *)[_schedule.games objectAtIndex:i];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 20, 150, 100)];
[button setBackgroundImage:[UIImage imageNamed:game.opponent] forState:UIControlStateNormal];
//[button setTitle:game.opponent forState:UIControlStateNormal];
[_gameScrollList addSubview:button];
How Can I add the observer to my button, Can this be done?
[self.scrollView addObserver:[self.contentViews objectAtIndex:i]
forKeyPath:#"contentOffset"
options:NSKeyValueObservingOptionNew
context:#selector(updateAlpha:)];

To access the reference of this button added as subview, you can:
Use this button as a iVar, saving the reference to use it later;
Similarly, use this button as a property;
Iterate through subviews of _gameScrollList, looking for a subview of UIButton's class (not recommended, will work well only if it has just one UIButton as subview)
Example of how implement the second approach. Just declare a property:
#property(nonatomic, strong) UIButton *button;
and then:
Game *game = (Game *)[_schedule.games objectAtIndex:i];
self.button = [[UIButton alloc] initWithFrame:CGRectMake(x, 20, 150, 100)];
[self.button setBackgroundImage:[UIImage imageNamed:game.opponent] forState:UIControlStateNormal];
//[self.button setTitle:game.opponent forState:UIControlStateNormal];
[_gameScrollList addSubview:self.button];
So, after, when you want to access this button somewhere, just call for the property
self.button.alpha = 0.5; //example of interation with your button
If this is not what you are looking for, you really should edit your question to be more clear.
Hope it helped.

Related

How to dynamically create and delete view when user clicks on button [duplicate]

This question already has answers here:
How do I create a custom view class Programmatically? [closed]
(5 answers)
Closed 6 years ago.
Please go through this scenario.
In a UIViewController there is a UIScrollView and inside it there is UIView and a UIButton. When a user taps on the UIButton, a new UIView should be added with the same x, invialViewWidth + 30, width, height and a UIButton.
If a user clicks the second UIButton, a new 3rd UIView with a third UIButton must be created. Similarly, you have to create a n number of views.
Inside the views there will be another UIButton delete. When it is clicked, the UIView should be removed from the UIScrollView.
For example, if you want to delete the 5th UIView then you should click on the delete UIButton which is in the 5th UIView.
I have gone through lot of questions but I haven't gotten a correct answer.
Create a custom view which will contain two Buttons add and delete. You can do something like this:
-(UIView *)getNewViewWithTag:(NSInteger )tag{
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];//Set appropriate frame
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
addButton.frame = CGRectMake(0,0,50,50); //Set appropriate frame
addButton.tag = tag;
[addButton addTarget:self action:#selector(addNewView:) forControlEvents:UIControlEventTouchUpInside];
[containerView addSubview:addButton];
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
deleteButton.frame = CGRectMake(50,0,50,50); //Set appropriate frame
deleteButton.tag = tag;
[deleteButton addTarget:self action:#selector(deleteView:) forControlEvents:UIControlEventTouchUpInside];
[containerView addSubview:deleteButton];
containerView.tag = tag;
return containerView;
}
-(void)addNewView:(id)button{
NSInteger tag = [button tag];
UIView *view = [self getNewViewWithTag:tag+1];
//set appropriate frame
[scrollView addSubview: view];
}
-(void)deleteView:(id)button{
NSInteger tag = [button tag];
UIView *viewToDelete = [scrollView viewWithTag:tag];
[viewToDelete removeFromSuperview];
}

Moving a UIImageView with a UIButton

I am rather new to Xcode and iOS development but have been enjoying the challenge so far.
I have run into an issue where I am attempting to move a UIImageView I have created programmatically on top of a MapBox mapView.
I would like to move this UIImageView with a UIButton by a measure of pixels, with the UIButton being on top of the mapView.
This is the code I have so far in my ViewController.m:
[self.view addSubview:mapView];
UIImageView* ship;
ship=[[UIImageView alloc] initWithFrame:CGRectMake(150, 200, 40, 40)];
UIImage * image;
image=[UIImage imageNamed:#"spaceShip"];
[ship setImage:image];
ship.alpha = 0.75;
[self.view addSubview:ship];
UIButton *upButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
upButton.frame = CGRectMake(150, 250, 40, 40);
upButton.userInteractionEnabled = YES;
[upButton setTitle:#"UP" forState:UIControlStateNormal];
[upButton addTarget:ship action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:upButton];
}
- (IBAction)moveUp {
ship.center = CGPointMake(ship.center.x, ship.center.y -10);
}
Could anyone show me how to get this upButton to recognize and move my UIImageView?
One problem is that ship is a local variable that you create in the first block of code. When that code block goes out of scope, ship will be nil, so when you try to set it's center in the button method, it won't work. You need to create a property for ship, and use that instead.
Your other problem is that when you add the action to your button, you call it buttonPressed:, but the method you implemented is moveUp. So, if you create a property called ship, then your action method should be,
- (void)buttonPressed:(UIButton *) sender {
self.ship.center = CGPointMake(self.ship.center.x, self.ship.center.y -10);
}

How can I disable the multiple user interactions on a screen at a same point of time

In my application contains no.of UIElements like facebook button, twitter button, logout button, imageview with gestures.
`
facebookBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[facebookBtn setImage:#"facebook.png"];
[facebookBtn addTarget:self action:#selector(switchToFacebookShare) forControlEvents:UIControlEventTouchUpInside];
[facebookBtn setFrame:CGRectMake(0, 50, 24, 24)];
[self.view addsubview:facebookBtn];`
`twitterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[twitterBtn setImage:#"facebook.png"];
[twitterBtn addTarget:self action:#selector(switchToTwitterShare) forControlEvents:UIControlEventTouchUpInside];
[twitterBtn setFrame:CGRectMake(0, 100, 24, 24)];
[self.view addsubview:twitterBtn];`
`imageTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(sectionTapping:)];
[imageTapGesture setNumberOfTapsRequired:1];`
`sectionPortraitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[sectionPortraitImageView setUserInteractionEnabled:YES];
[sectionPortraitImageView addGestureRecognizer:imageTapGesture];
[contentView addSubview:sectionLandscapeImageView];`
like this i have added all the elements
When user click on all buttons at a time all three functionalities are working.
Here only one operation has to be done.
How can I solve this problem?
To Handle multiple touch issue at a time and also set individual property
[sectionLandscapeImageView setExclusiveTouch:YES];
// To handle all subviews multiple touch user interactions
-(void)multipleTouchHandling
{
self.view.multipleTouchEnabled=NO;
self.view.exclusiveTouch=YES;
for(UIView* view1 in self.view.subviews)
{
if([view1 isKindOfClass:[UIButton class]])
{
UIButton* btnVw = (UIButton*)view1;
[btnVw setExclusiveTouch:YES];
}
}
}
I see a few ways out of this situation.
You keep strong links to each button, and make
[someButton setUserInteractionEnabled:NO]
for each element after user click someone. But it takes a lot of time and code
(i think better solution) Or you can just show the UIActivityIndicatorView on top of view to block all user interaction while you processing some data or make some other actions.

Programmatically created UIButton not visible

I have created a UIButton, added it to a view, which has then been added to a UIScrollView. This View also contains a UITextView which displays and is formatted correctly.
My code is below. Hopefully someone can help. The area where the button should be is clickable, and acts correctly, however the button is not visible. I even changed the tintColor to red, but I cannot see it.
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *billBlocker = *UIView initialized*
//The UITextView is a subview of `billBlocker`, as well as the `billBlockButton`
//added the UITextView here
UIButton *billBlockButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 5,292,30 )];
[billBlockButton setTitle:#"BillBlockerFrenzy" forState:UIControlStateNormal];
[billBlockButton setTintColor:[UIColor redColor]];
[billBlockButton addTarget:self action:#selector(segueToRegulationsGame) forControlEvents:UIControlEventTouchUpInside];
[billBlocker addSubview:billBlockButton];
[self.scrollView addSubview:billBlocker];
}
You should use cluster method when crate button:
UIButton *billBlockButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[billBlockButton setFrame:CGRectMake(0, 5,292,30 )];
...
This code should work. Make sure the scrollView frame and content size is set correctly and scrollView is added to self.view.
buttonWithType: is recommended method for creating the button, this is the only way you can specify button type.
When you use alloc init, I believe, you use some standard button type and if iOS change the standard also could change.
Probably the problem is you forgot the set outlet of scroll view i tried and its working like this
#property (strong,nonatomic) IBOutlet UIScrollView *sView; //scrollviews outlet
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//added the UITextView here
UIButton *billBlockButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 5,292,30 )];
[billBlockButton setTitle:#"BillBlockerFrenzy" forState:UIControlStateNormal];
[billBlockButton setBackgroundColor:[UIColor redColor]];
[billBlockButton addTarget:self action:#selector(nothing) forControlEvents:UIControlEventTouchUpInside];
[self.sView addSubview:billBlockButton];
}
Forgot to say you need to add your method instead of nothing method..
Hope it helps.
When we create the UIButton object programmatically, the default title color of UIButton is white.
So, set the title color of UIButton object.
[billBlockButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
iOS 7 or later will have transparent button. Unless you set text, you will not be able to see. Do following:
[[yourButton layer] setBorderColor:[UIColor blackColor].CGColor];
[[yourButton layer] setBorderWidth:2.0];
[yourButton setTitle:#"Hello" forState:UIControlStateNormal];

Add buttons at runtime that can be accessed by any method on iOS

I tried creating UIButtons from an Array of objects. I created these successfully by using
float x=0,y=0;
for( NSMutableDictionary *dict in places)
{
NSLog(#"x: %f",x);
x=x+25;
y=y+25;// Vary these depending on where you want the buttons to be
UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(x,y,25,25)] autorelease];
button.backgroundColor=[UIColor redColor];
[self addSubview:button];
}
but I need some way to make all of these buttons accesible by other methods in my class, particularly a method that is triggered by the CLLocation delegate (or some other ways, even by user interaction) and which animates any of the buttons.
Hope anyone can help me!
btw: the object "places" is an Array of MutableDictionaries.
Thank you!
Create a mutable collection for keeping the references to the buttons. If you need to have them ordered somehow, create NSMutableArray, if not, create NSMutableSet. In the header file declare the ivar like NSMutableSet buttonsSet, then in the class implementation:
float x=0,y=0;
buttonsSet = [[NSMutableSet alloc] init];
for( NSMutableDictionary *dict in places) {
NSLog(#"x: %f",x);
x=x+25;
y=y+25;// Vary these depending on where you want the buttons to be
UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(x,y,25,25)] autorelease];
button.backgroundColor=[UIColor redColor];
[buttonsSet addObject:button];
[self addSubview:button];
}
Note that the buttons are accessible also through self.subviews array.
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(60, 327, 210, 32); // position in the parent view and set the size of the button
[myButton setTitle:#"Madhavan" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(editClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];

Resources