Move a programmatically generated button - ios

I have a programmatically created button and I want to move it to a different location when a specific method is called. So far I can see the button I created, and I can move a different button that I drag-dropped into storyboard, but I am not sure how I can refer to the programmatically generated button in my code to change its location.
Code for generating button:
UIButton *generatedButton = [UIButton buttonWithType:UIButtonTypeCustom];
[generatedButton addTarget:self
action:#selector(genButtonTouched:)
forControlEvents:UIControlEventTouchDown];
generatedButton.frame = CGRectMake(84.0, 80.0, 70.0, 40.0);
[self.view addSubview:generatedButton];
Inside another method, I have code for changing a button's location. If this code is in the same method as where I generate the button it works fine, but I need to place this in a different method:
[generatedButton setFrame:CGRectMake(xCoord, yCoord, buttonWidth, buttonHeight)];

In your ButtonViewController.h
#interface ButtonViewController : UIViewController{
}
#property (retain, nonatomic) UIButton *generatedButton;
In your ButtonViewController.m
#implementation ButtonViewController
#synthesize generatedButton;
- (void)viewDidLoad
{
generatedButton = [UIButton buttonWithType:UIButtonTypeCustom];
[generatedButton addTarget:self
action:#selector(genButtonTouched:)
forControlEvents:UIControlEventTouchDown];
generatedButton.frame = CGRectMake(84.0, 80.0, 70.0, 40.0);
[self.view addSubview:generatedButton];
}
// to call the button anywhere within the class, do like this :-
-(void)methodToMoveButton{
[generatedButton setFrame:CGRectMake(xCoord, yCoord, buttonWidth, buttonHeight)];
}
If you want something else, please let me know.

It's no different than moving the button added in the storyboard. Create an instance variable to hold a reference to your generated button. Then you can access the button in the other method using that instance variable.

Related

Custom UIButton No Longer Works After Changing UIView to UIViewController

This code worked in a UIView container, but after changing that container to a UIViewController, the code no longer works:
-(void)viewDidLoad
{
[super viewDidLoad];
[self.view setUserInteractionEnabled:YES];
// set button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(60, 30, 220, 220);
[button setImage:self.obj.img forState:UIControlStateNormal];
[button addTarget:self action:#selector(selected:) forControlEvents:UIControlEventTouchUpInside];
[button setUserInteractionEnabled:YES];
[self.view addSubview:button];
}
-(void) selected:(UIButton *) button // also tried (id) sender
{
NSLog(#“Button Selected“);
}
These containers are created programmatically (dynamically), from web services, so I cannot use Storyboard. The reason for changing to a UIViewController is that I need a segue to another screen. Also, these containers are placed in a UIScrollView.
Everything else is working properly, except the selector.
Setting the userInteraction wasn't necessary earlier, but I've gone ahead and tried that in different configurations. I've added this to the scrollview, the viewController, and the button, just for good measure.
I even tried some online tutorials for programmatically creating UIButtons in UIViewControllers and was able to do that just fine. Not sure what the issue is here. Thanks.

Displaying UIButton programmatically

I'm trying to display a button to the view programmatically with this code:
- (void)viewDidLoad {
_detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
_detailButton.frame = CGRectMake(23, 294, 72, 37);
[_detailButton setTitle:#"UIButton!" forState:UIControlStateNormal];
[_detailButton addTarget:self
action:#selector(showPop:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_detailButton];
}
I have the button declared in the .h file like this:
#property (strong, nonatomic) UIButton *detailButton;
The code does not throw any errors or warnings, but when I run there is no button displayed.
I check your code in my project its working fine i think in your case there are some other view that cover this button so check that your code has no problem at all.
Your button probably covered by another view. Just make sure its on top of other views.
In viewWillAppear add the following line of code:
[self.view bringSubviewToFront:self.detailButton];
If your button's type is of UIButtonTypeCustom, the button's default title color is white, so you can't see it if your view's background color is white.
[_detailButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

Objective c, changing NSString property using buttons tap

I have a view: "myView" , 3 buttons in this view with title:"myButton1","myButton2" and 3... and a property in myView, NSString: "myString". How can i do this: When I press this button I want to change myString in buttons title property("button1..2,3").
very simple
-(void)viewDidLoad{
[super viewDidLoad];
UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];
[btn1 addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
[btn2 addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIButton *btn3=[UIButton buttonWithType:UIButtonTypeCustom];
[btn3 addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)buttonTapped:(UIButton*)sender{
[sender setTitle:myString forState:UIControlStateNormal];
}
I assume that myView is either the view of a UIViewController (i.e. accessible through myViewController.view) or it is one of its subviews / subsubview etc.
If you are using Interface Builder to create your view controller (e.g. in a storyboard) you can open the assistant editor
(click the icon in the upper right corner)
and drag a line from each button to your viewController.h file (hold CTRL and drag to the source code in the lower window).
Select 'Action' as connection type and give each method a unique name like buttoniTapped. Open the viewController.c file in the editor. There you should find the newly created action methods. Now make those methods change your string:
- (IBAction)button1Tapped:(id)sender {
self.myView.myString = #"Button 1 was tapped.";
}
- (IBAction)button2Tapped:(id)sender {
self.myView.myString = #"Button 2 was tapped.";
}
- (IBAction)button3Tapped:(id)sender {
self.myView.myString = #"Button 3 was tapped.";
}
(This will only work if you create a property myView in myViewController for the view that contains the three buttons.)
Generally I think it is a better idea to make myString a property of the view controller rather than the view because it makes things easier and separates your data from your view.

UIView addSubview works once but not twice

I would like to add a UIButton in a UIView from my xib file.
In my xib, I declare a UIView outlet named "content" with interface builder: here is the generated code :
#property (weak, nonatomic) IBOutlet UIView *content;
I would like to add a button, here is the code for this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 8, 141.0, 56.0);
[button setBackgroundImage:[UIImage imageNamed:#"TAB_NORMAL"]
forState:UIControlStateNormal];
button.frame = CGRectMake(0, 8, 56.0, 141.0);
[self.content addSubview:button];
If I put this code in -viewDidLoad method, it works, but if I put it in a function called later, the button is never displayed.
What could be the reason ?
Thanks for your help.
If your UIView content is a top level item, that is, not contained in your main view - then it has to be strong. Only items that are not top-level can use weak, since the containing view would keep a strong reference to them. I also assume that content was immediately added to the primary view, which kept a strong reference to it (it in viewDidLoad happened before ARC nil'ed out content.
If for some reason changing the property to strong does not fix the problem, then for sure add a NSLog (or assert) to insure that content is non-nil.

How to perform performSegueWithIdentfier from within UIView

Ok so I have a UIButton embedded into a UIImageView which forms part of a UIView.
The button's selector calls a method which should run:
[self performSegueWithIdentifier:#"SegueToMoreInfo"];
However I get the error:
no visible at interface for 'myclassname' declares the selector 'performSegueWithIdentifier'.
Basically I really need to perform a segue from within the UIView. If not possible how would I call from the UIViewController instead?
EDIT: MORE INFO:
//adding button
UIButton *attractionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[attractionButton addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
[attractionButton setTitle:#"Attraction Info" forState:UIControlStateNormal];
attractionButton.frame = CGRectMake(80.0, 80, 160.0, 40.0);
[_image1 addSubview:attractionButton];
My method :
(IBAction)buttonClicked:(id)sender
{
[self performSegueWithIdentifier:#"SegueToMoreInfo" sender:sender];
}
[self performSegueWithIdentifier:#"aseguename" sender:self];
is a UIViewController method. That is why you are getting this error. Also views should not talk to ViewControllers except through protocols.
Having said that, you are going about this the wrong way. IBActions for buttons and other UIView objects should be in the ViewController. So I would move the IBAction to your View Controller and hook it to the button from there. Then insert your code in that IBAction.
Update after the code posted:
All of the code posted should be in your View Controller. I would just change:
[_image1 addSubview:attractionButton];
to
[self.view addSubview:attractionButton];
or if you really want that button to be a subview to your image, then you can leave your code, just make sure to create an IBOutlet property for that image, from your image in interface builder to your View Controller and call it _image1.
Hope this helps

Resources