I know how to set the enable = YES/NO, a button created using a property and xib. However, how do you do the same thing to a programmatically created button from another method in the same class?
For example here is my button in the viewDidLoad:
UIButton *AllList = [UIButton buttonWithType:UIButtonTypeCustom];
AllList.frame = CGRectMake(40, 80, 107.f, 53.5f); //set frame for button
UIImage *buttonImageFull = [UIImage imageNamed:#"allModsBtn.png"];
[AllList setBackgroundImage:buttonImageFull forState:UIControlStateNormal];
[self.view addSubview:AllList];
// add targets and actions
[AllList addTarget:self action:#selector(getButtons:) forControlEvents:UIControlEventTouchUpInside];
AllList.tag = 0;
I would like to set the enable of this button to YES or NO in another method.
#implementation {
UIButton *myButton;
}
- (void)viewDidLoad {
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.tag = 121;
myButton.frame = CGRectMake(40, 80, 107.f, 53.5f); //set frame for button
UIImage *buttonImageFull = [UIImage imageNamed:#"allModsBtn.png"];
[myButton setBackgroundImage:buttonImageFull forState:UIControlStateNormal];
[self.view addSubview:myButton];
// add targets and actions
[myButton addTarget:self action:#selector(getButtons:)
forControlEvents:UIControlEventTouchUpInside];
myButton.tag = 0;
}
- (void)someOtherMethod {
myButton.enabled = YES;
OR
//In this case you dont need to define uibutton to globaly
UIButton *button = (UIButton*)[[self view] viewWithTag:121];
[button setEnabled:YES];
}
You have to make that button an ivar of your view controller.
#implementation {
UIButton *myButton;
}
- (void)viewDidLoad {
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(40, 80, 107.f, 53.5f); //set frame for button
UIImage *buttonImageFull = [UIImage imageNamed:#"allModsBtn.png"];
[myButton setBackgroundImage:buttonImageFull forState:UIControlStateNormal];
[self.view addSubview:myButton];
// add targets and actions
[myButton addTarget:self action:#selector(getButtons:)
forControlEvents:UIControlEventTouchUpInside];
myButton.tag = 0;
}
- (void)someOtherMethod {
myButton.enabled = YES;
}
Something like this:
UIButton *myButton = [self.view viewWithTag:BUTTON_TAG]; // in your case is 0
[myButton setEnabled:YES];
Two ways to do it:
1) you can make it an ivar and then
AllList.enabled = YES;
or
[AllList setEnabled:YES];
2)
set a unique tag to the button
UIButton *AllList = [UIButton buttonWithType:UIButtonTypeCustom];
AllList.frame = CGRectMake(40, 80, 107.f, 53.5f); //set frame for button
AllList.tag = kUNIQUE_TAG;
in the method you want to mess with the button's enabled property
UIButton *theButton = (UIButton *)[self viewWithTag:kUNIQUE_TAG];
[theButton setEnabled:YES];
Related
I am new to iOS development and I have a problem with UIScrollView. First of all, I've created a Scroll View in a storyboard and added
#property (retain, nonatomic) IBOutlet UIScrollView *mainScroll;
in view controller handler
In the viewDidLoad method I have the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.mainScroll.contentSize = CGSizeMake(100.0,100.0); // Have to set a size here related to your content.
// You should set these.
self.mainScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.mainScroll.minimumZoomScale = .5; // You will have to set some values for these
self.mainScroll.maximumZoomScale = 2.0;
self.mainScroll.zoomScale = 1;
UIButton* b = [[UIButton alloc] init];
[b setBounds:CGRectMake(.0f,0.f,100.0,100.0)];
[self.mainScroll addSubview:b];
}
But the button does not appear in the simulator. However if I add a button on storyboard in the IB, the button appears and I can scroll the view.
How can I add the button in code?
Try this code
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.mainScroll addSubview:button];
bounds and frame are not the same thing.
Use this code instead...
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Hello" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 100, 44);
[self.mainScroll addSubview:button];
You need to set scroll view contents size, something like the following code.
[self.mainScroll setContentSize:CGSizeMake(width, height)];
Try this :
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Hello" forState:UIControlStateNormal];
button.frame = CGRectMake(self.mainScroll.frame.origin.x + 25 ,self.mainScroll.frame.origin.y +25, 50, 44);
[self.mainScroll addSubview:button];
Best and easiest way to make a button from code is:
UIButton* yourButton = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
[yourButton setTitle:#"Your Title" forState:UIControlStateNormal];
// if you set the button's image the title won't be visible, because button's title is "under" the image. I recomend you to use this if you have a cut and dried image.
[yourButton setImage:[UIImage imageNamed:#"yourImage.png"] forState:UIControlStateNormal];
// if you set the button's background image, button's title will be visible
[yourButton setBackgroundImage:[UIImage imageNamed:#"yourBackgroundImage.png"] forState:UIControlStateNormal];
// you can add target to the button, for handle the event when you touch it.
[yourButton addTarget:self action:#selector(handleToucUpInside:) forControlEvents:UIControlEventTouchUpInside];
// then add the button
[self.view addSubview:yourButton];
- (void) handleToucUpInside:(id)sender{
// here you can do anything you want, to handle the action
}
When I click inside text box, How can I add plus button to UITextField as shown in the following screen? When this button is clicked it would launch iPhone contact application. I would very much appreciate any code example. Thanks
Following code use for adding button in UITextField.
-(void)ViewDidLoad{
UITextField *txt = [[UITextField alloc]initWithFrame:CGRectMake(140, 120, 160, 40)];
txt.returnKeyType = UIReturnKeyDone;
txt.placeholder = #"Enter or Mobile Number";
[txt setBorderStyle:UITextBorderStyleRoundedRect];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"crossImg.png"] forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0);
[button addTarget:self action:#selector(clearText:) forControlEvents:UIControlEventTouchUpInside];
txt.rightView = button;
txt.rightViewMode = UITextFieldViewModeAlways;
[self.view addSubview:txt];
}
-(IBAction)clearText:(id)sender{
}
You can use the below code for this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == yourFirstTextField)
{
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setImage:[UIImage imageNamed:#"addImage.png"] forState:UIControlStateNormal];
[addButton addTarget:self action:#selector(yourActionHere) forControlEvents:UIControlEventTouchUpInside];
textField.rightViewMode = UITextFieldViewModeWhileEditing;
textField.rightView = addButton;
}
}
You can use the above method if you added the field in IB.
If you are created it through code, you need to add the below code when creating:
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setImage:[UIImage imageNamed:#"addImage.png"] forState:UIControlStateNormal];
[addButton addTarget:self action:#selector(yourActionHere) forControlEvents:UIControlEventTouchUpInside];
textField.rightViewMode = UITextFieldViewModeWhileEditing;
textField.rightView = addButton;
Use following method for add UIButton
[self.txtField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
- (BOOL) textFieldDidChange:(UITextField *)textField
{
UIButton *btnColor = [UIButton buttonWithType:UIButtonTypeCustom];
[btnColor addTarget:self action:#selector(btnColorPressed:) forControlEvents:UIControlEventTouchUpInside];
btnColor.frame = CGRectMake(self.txtTag.bounds.size.width - 50, 5, 25, 25);
[btnColor setBackgroundImage:[UIImage imageNamed:#"PaintPickerButton.png"] forState:UIControlStateNormal];
[self.txtTag addSubview:btnColor];
return YES;
}
Or Write
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
// self.scrollView.frame = CGRectMake(0, 35, self.view.bounds.size.width, self.view.bounds.size.height - 250);
[UIView commitAnimations];
UIButton *btnColor = [UIButton buttonWithType:UIButtonTypeCustom];
[btnColor addTarget:self action:#selector(btnColorPressed:) forControlEvents:UIControlEventTouchUpInside];
btnColor.frame = CGRectMake(150, 5, 25, 25);
[btnColor setBackgroundImage:[UIImage imageNamed:#"PaintPickerButton.png"] forState:UIControlStateNormal];
[self.txtField addSubview:btnColor];
return YES;
}
Use rightView property of UITextField.
In textFieldDidBeginEditing: delegate create button with action you want & set to rightView.
And in textFieldDidEndEditing: set nil to rightView
you can also go for custom view ant then add sub view to it. then you hide it. it will be visible only when the user taps it
self.locationName.clearButtonMode = UITextFieldViewModeWhileEditing;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 30, 30);
[button setImage:[UIImage imageNamed:#"goButtonIcon.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(goButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
self.locationName.rightView = button;
self.locationName.rightViewMode = UITextFieldViewModeUnlessEditing;
and then call this method:
-(IBAction)goButtonClicked:(id)sender{
}
self.locationName -------> UITextField Reference (in ViewDidload you can write this code)
I have a Storyboard View Controller (with my own custom sub class) of which I have a initWithCoder custom method, where I want to popular the view with my own custom objects (I want to maintain the flexibility of doing this programmatically.
Code works fine until [self.view addSubview:button]; is called, then it crashes with: "Could not load NIB in bundle".
All I wanted to do was add a UIButton on my view...
- (id)initWithCoder:(NSCoder*)aDecoder
{
if(self = [super initWithCoder:aDecoder])
{
[self initLoginForm];
}
return self;
}
-(void)initLoginForm
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 170, 100, 30);
[button setTitle:#"Login..." forState:UIControlStateNormal];
[button addTarget:self action:#selector(handleLoginAttempt) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
how about putting the button in the viewDidLoad method, then add it in the initLoginForm.
Something like this:
-(void)viewDidLoad {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 170, 100, 30);
[button setTitle:#"Login..." forState:UIControlStateNormal];
[button addTarget:self action:#selector(handleLoginAttempt) forControlEvents:UIControlEventTouchUpInside];
}
-(void)initLoginForm {
[self.view addSubview:button];
}
or add it in the viewDidLoad then hide it, then in the initLoginForm show it.
I have created buttons dynamically and now I want to set the background for a particular index of the buttons.
Here is my sample snippet:
In the interface file:
UIButton *answerBtn;
#property (nonatomic, retain) UIButton *answerBtn;
for(int i = 0; i < [myArray count]; i++) {
answerBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[answerBtn setFrame:CGRectMake(30, x, 260, 40)];
answerBtn.tag = i;
[answerBtn setTitle:[answerList objectAtIndex:i] forState:UIControlStateNormal];
[self.view addSubview: answerBtn];
}
In my case, I want to set the button background in different methods.
-(void) custom Method
{
if(indexValue == correctIndex) // Values are 2
{
// so I want to set the background image for the second button
[answerBtn setBackgroundImage:[UIImage imageNamed:#"selected_correct_answer.png"] forState:UIControlStateNormal];
}
}
But it doesn't set the corresponding index, so how can I do that?
Try this
for(int i = 0 ; i < [myArray count] ; i++ )
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTag:i];
[btn setFrame:CGRectMake(30, x, 260, 40)];
[btn addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (IBAction) btnClicked::(id)sender
{
UIButton *btnTapped=(UIButton *)sender;
for(UIView *btn in self.view.subviews)
{
if([btn isKindOfClass:[UIButton class]])
{
UIButton *btnComp = (UIButton*)btn;
if(btnComp.tag == btnTapped.tag)
[btnComp setBackgroundImage:[UIImage imageNamed:#"selected_correct_answer.png" forState:UIControlStateNormal];
else
[btnComp setBackgroundImage:[UIImage imageNamed:#"default_image.png" forState:UIControlStateNormal];
}
}
}
Use -(void)custom:(id)sender instead of -(void)custom.
In the sender you can have the index of the button.
UIButton *answerBtn = (UIButton *)[self objectWihTag:1]
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setImage:[UIImage imageNamed:#"restaurant.png"] forState:UIControlStateNormal];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setImage:[UIImage imageNamed:#"restaurant1.png"] forState:UIControlStateNormal];
if([scaleStr isEqualToString:#"0.139738"]){
btn1.frame = CGRectMake(330,145,5,5);
[imageScrollView addSubview:btn1];
} else if ([scaleStr isEqualToString:#"0.209607"]) {
[btn1 removeFromSuperView];
btn2.frame = CGRectMake(495,217.5,10,10);
[imageScrollView addSubview:btn2];
}
i have set the button when doubletap the background image in UIScrollview.If i double tap second time , the first button also show.I need to remove the button which i created first .
Also i remove the btn1 in next condition.
Kindly help me regarding on this.
A little bit of context might help here, but my guess is that you're really just creating a new button each time and not holding a reference to the old one. So when you call removeFromSuperView on the second tap, you're removing a button that hasn't even been added to the view, not the one you added on your last pass. Try making your buttons instance variables instead of local. In extremely loose pseudo-code:
#interface MyClass : UIView {
UIButton *btn1;
UIButton *btn2;
}
#implementation MyClass {
-(MyClass *) init {
self = [super init];
[self setupButtons];
return self;
}
-(void) setupButtons {
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setImage:[UIImage imageNamed:#"restaurant.png"] forState:UIControlStateNormal];
btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setImage:[UIImage imageNamed:#"restaurant1.png"] forState:UIControlStateNormal];
}
-(void) myFunction {
[btn1 removeFromSuperView];
[btn2 removeFromSuperView];
if([scaleStr isEqualToString:#"0.139738"]){
btn1.frame = CGRectMake(330,145,5,5);
[imageScrollView addSubview:btn1];
} else if ([scaleStr isEqualToString:#"0.209607"]) {
btn2.frame = CGRectMake(495,217.5,10,10);
[imageScrollView addSubview:btn2];
}
}
}