iOS - Can not set Button 'Disable', without removing the content of it - iphone

Sorry for what should be a simple solution.
I am trying to 'Disable' a button in code using the following Code:
in the first Class which Called 'CheckBox':
checkBox.h :
#interface CheckBox : UIViewController
{
BOOL checkboxSelected;
UIButton *checkBoxButton;
}
#property (nonatomic, retain) IBOutlet UIButton *checkBoxButton;
- (IBAction)checkBoxButton:(id)sender;
-(void) setCheckBoxSelected:(BOOL)checkingStatus;
-(void) setCheckBoxEnabled:(BOOL)enablingStatus;
in checkBox.m :
- (IBAction)checkBoxButton:(id)sender {
if (checkboxSelected == 0){
[checkBoxButton setSelected:YES];
checkboxSelected = 1;
} else {
[checkBoxButton setSelected:NO];
checkboxSelected = 0;
}
}
-(void) setCheckBoxSelected:(BOOL)checkingStatus {
checkBoxButton.selected = checkingStatus;
}
-(void) setCheckBoxEnabled:(BOOL)enablingStatus {
[checkBoxButton setEnabled:enablingStatus];
}
and in the implementation of another class which called 'MainViewController.m' :
- (void)viewDidLoad{
allTransactionCheckBox = [[CheckBox alloc] init];
[self.viewWithdraw addSubview:withdrawCheckBox.view ];
withdrawCheckBox.labelCheckBox.textColor = [UIColor blackColor];
withdrawCheckBox.labelCheckBox.font = [UIFont systemFontOfSize:14];
withdrawCheckBox.labelCheckBox.text = #"Withdraw";
[withdrawCheckBox setCheckBoxSelected:YES];
}
The above code is 'Disable' the button, but it (Remove/ Hide) the 'check mark Picture' inside the button.Why?
All links ( outlet + Actions ) are connected .
What obvious thing am I missing? Thanks.

What exactly are you trying to do? Maybe setting userInteractionEnabled is what you want?
Cheers

You have defined the button in interface builder. It looks like you havent set the image for all control states - select the button and look at the attributes inspector - there is a drop down for normal, highlighted, etc. is your check mark image defined for all those states?

This looks way too complex for what you want to do. You have a reference to your button -- checkBoxButton. Just use checkBoxButton.enabled = NO;

Related

How to set NSString before calling a method objective c

It might be a simple question yet I could not figure out what I am missing.
In ViewControl.h I declared UIColor
#property (nonatomic, strong) UIColor * myColor;
In ViewControl.m I have a method that do something and return new UIColor
#synthesize myColor = _myColor;
In ViewDidLoad Method
- (void)viewDidLoad
{
myColor = [UIColor RedColor];
}
-(void) ShowColorPopUpView
{
if (!self.wePopoverController)
{
ColorViewController *contentViewController = [[ColorViewController alloc] init];
contentViewController.delegate = self;
self.wePopoverController = [[WEPopoverController alloc] initWithContentViewController:contentViewController];
self.wePopoverController.delegate = self;
self.wePopoverController.passthroughViews = [NSArray arrayWithObject:self.navigationController.navigationBar];
[self.wePopoverController presentPopoverFromRect:self.tvTweetDetails.frame
inView:self.view
permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown)
animated:YES];
} else
{
[self.wePopoverController dismissPopoverAnimated:YES];
self.wePopoverController = nil;
}
}
-(void) colorPopoverControllerDidSelectColor:(NSString *)hexColor
{
_myColor = [GzColors colorFromHex:hexColor];
[self.view setNeedsDisplay];
[self.wePopoverController dismissPopoverAnimated:YES];
self.wePopoverController = nil;
}
- (UIColor *) returnColor
{
return _myColor;
}
My Question starts here: I have two methods to change a textview font and background color
- (IBAction)btnFontColorPopUpMenu:(id)sender
{
[self ShowColorPopUpView];
tvTweetDetails.textColor = [self returnColor];
}
- (IBAction)btnTextViewBackGroundColor:(id)sender
{
[self ShowColorPopUpView];
tvTweetDetails.backgroundColor = [self returnColor];
}
The issue now is when I call the method it return it returns RED and if I call it again it returns the the BlackColor.
How Can I call the method and change the Color to the new one and then return it. I want to get the Black color directly.
I want to execute the method first then return the color but what happens is assign the color before execute the method.
I hope I made it the Question Clear.
Okay, I'll take a whack at this.
I suspect you are doing some kind of presentViewController:... method in your color changer method. That's great, but it has implications. The method you call that in continues to execute during that presentation. That means it may return, etc.
This is where the concept of delegates comes in. You may benefit from restructuring the data flows here a bit.
What I suggest (if I am correct about the presentation of a color picker UI) is that you make the following changes:
Create a #protocol ColorPickerDelegate with one method: -(void) userChoseColor:(UIColor *) color
Add a #property (weak) id<ColorPickerDelegate> delegate to your color picker view controller
make your VC here implement that protocol
Inside the delegate method, set your local property
Implement a custom setter for the local propert, and update the background color whenever the color changes.

How to enable an UIButton after entering a valid value

I have this app and a reset button to reset the values that I entered. I'm trying to get the button to be gray as long as I don't enter a valid value.
When the value is valid, it should enable and become touchable...
How can I do this?
I am using this code but it isn't working...
//Reset values
- (IBAction)resetPressed:(UIButton *)sender {
if (didPan==1) {
resetPressed.enabled = YES;
} else {
resetPressed.enabled = NO;
}
self.prozent=0;
didPan=NO;
//remove drawn intersection line
[intersectionLine removeFromSuperview];
NSLog(#"resetPressed");
}
To enable the button if it matches your criteria, use setEnabled:YES.
E.g.
UIButton *button = (UIButton *)sender;
[button setEnabled:YES];
An even better way to do this would be to use the dot notated version, like such:
button.enabled = YES;
In your condition you are using if (didPan==1) { although it would be giving the required result but it is better to use if(didPan)
secondly resetPresed is action name not the button or sender, so you should use sender instead of resetPressed
and for setting button status use [sender setEnabled:YES];//OR sender.enabled=YES;
so your code would look like this
- (IBAction)resetPressed:(UIButton *)sender {
if (didPan) {
[sender setEnabled:YES]; //OR sender.enabled=YES;
} else {
[sender setEnabled:NO]; // OR sender.enabled=NO;
}
}
If you are entering a value in a UITextField, you could set the delegate for the text field and write code for enabling/disabling inside textFieldDidEndEditing: method of the delegate object.
i.e; theButton.enabled = YES/NO
From your code it looks like you are writing code for disabling the button inside the action of that button itself which won't work if the button is disabled.
***I did solve the problem after long time of searching.
I found out that the ResetButton must have a Property
it looks like this
#property (strong, nonatomic) IBOutlet UIButton *resetButton;
Then I highlighted the button and unchecked enabled from the control(tried it before but it didn't work)
The I used button.enabled=YES and changed the colour using [resetButton setAlpha:1] for normal and [resetButton setAlpha:0.5] for not activated
Thanks guys for your help!*
if you enter the value for example in a UITextField, then use the delegate of the textfield to determine if the value is correct:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([textField.text isValid]) { // check if text in textfield is valid
button.enabled = YES;
} else {
button.enabled = NO;
}
return YES;
}

iPhone - call UISwitch that is generated in a UIView when a button is pressed

To clarify my question, my program has three lightbulb on the screen (Customized UIButton)
when any lightbulb is pressed, I programatically generate a UIView with a switch on it
when I turn on the switch, corresponding lightbulb will light up (change its background image)
However, I have trouble accessing this UISwitch since I can't declare it publicly
My code goes something like this:
#property buttonA;
#synthesize buttonA;//all three buttons have their background image set to 'off.png'
- (IBAction)lightBulbPressed:(UIButton *)sender
{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(1,1, 64, 64)];
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0,0,64,64)];
[mySwitch addTarget:self action:#selector(onOrOff) forControlEvents:UIControlEventValueChanged];
[myView addSubview:mySwitch]
[self.view addSubview:myView];
}
So what troubles me is how to program the selector onOrOff, so that it knows which switch is being touched and change the background image of corresponding button accordingly.
Think about your method:
- (IBAction)lightBulbPressed:(UIButton *)sender {
// your method
}
You already know who called it. This piece of information is stored in sender.
So you can save it and use later in onOrOff
By the way, if you are using UISwitch you have to check
UIControlEventValueChanged
and not UIControlEventTouchUpInside.
EDIT: To pass your sender you can store its value to a NSString *buttonTapped declared in your .h file
- (IBAction)lightBulbPressed:(UIButton *)sender {
if (sender == bttOne) {
buttonTapped = #"ButtonOneTapped";
} else if (sender == bttTwo) {
buttonTapped = #"ButtonTwoTapped";
} else if (sender == bttThree) {
buttonTapped = #"ButtonThreeTapped";
}
// your method
}
- (void)onOrOff {
if ([buttonTapped isEqualToString:#"ButtonOneTapped"]) {
// Button One
} else if ([buttonTapped isEqualToString:#"ButtonTwoTapped"]) {
// Button Two
} else if ([buttonTapped isEqualToString:#"ButtonThreeTapped"]) {
// Button Three
}
}
One way to do so, is taht you give them distinct tag numbers in IB, and in - (IBAction)lightBulbPressed:(UIButton *)sender method, get their tag. e.g. NSInteger pressedButtonTag = [sender tag];, and go from there.
Also, instead of alloc/init myView every time user presses a button, you can add that view in IB, add the switch to it, put in the hierarchy of the owner but not the view, and set an outlet to it in .h. Call it whenever you need it, and again, access the switch by tag e.g. ( UISwitch *mySwitch = (UISwitch *)[myView viewWithTag:kSwitchTag]; ) and do whatever you want to do (on or off), add it to the subview and remove it later. This is more efficient.

changing a button's text from another method when the buttons are in a subclass?

How do you access a button that's in a subview of a view?
I have a viewController 'GamePlay', which has a scrollview in it called 'gameScroll'. Within this scrollview I have about 100 buttons, each with a tag, and I want to be able to change the text of the button from another method.
- (void) viewDidLoad {
//Created buttons in a for-loop, assigning each a tag
//numOfButtons is total number of buttons created
}
I imagine it would be something like this? but I cant seem to find an answer on exactly how i do it when the button is in a subview
- (void) otherMethod {
for (int i=0; i<numOfButtons; i++) {
// tagsForAction = get list of buttons that need to be changed from another array
for (j=0; j<tagsForAction.length; j++) {
intTagForAction = [tagsForAction objectAtIndex:j];
if (i = tagForAction) {
UIButton* button = [Gameplay.gameScroll.view viewWithTag:tagForAction];
button.title = #"A";
}
}
}
}
I know this code isnt totally right. Im just giving you an idea of the process. I can do everything except this part in the if statement:
UIButton* button = [Gameplay.gameScroll.view viewWithTag:tagForAction];
button.title = #"A";
so how do I change the text of these buttons?
NSArray *subViewList = [gameScroll subviews];
for (id button in subViewList)
{
if ([button isKindOfClass:[UIButton class]])
{
[button setTitle:#"OK" forState:UIControlStateNormal];
}
}
Try this code
You don't have to use the for loop like that, you can get an array of subviews from any view by doing something like [someView subviews]. So, inside of your otherMethod function you can do something like:
for (UIView *v in [gameScroll subviews]) {
if (v.tag == <some_int_here>) {
[v setTitle:#"Some other title" forState:UIControlStateNormal];
}
}
I'm not exactly sure what your criteria is for determining which buttons inside the gameScroll view have to get updated, but you can work from here.

UIButton sender title problem

seems like a quite easy problem but I don't get it.
It have two UIButtons, one is titled 'next' the other is titled 'previous'. Both are linked to the same method. All I wanna do is change the variable 'helpStatus'depending on which button is pressed:
if([sender currentTitle] == #"next"){
helpStatus++;
}
if ([sender currentTitle] == #"previous"){
helpStatus--;
}
NSLog(#"%#", [sender currentTitle]);
the logged titels are 'next' and 'previous' just like it should be but it doesn't work and I don't know why.
You need to use isEqualToString otherwise you are just comparing if they are the same object, not if they are equal :)
if([[sender currentTitle] isEqualToString:#"next"]){
helpStatus++;
}
if ([[sender currentTitle] isEqualToString:#"previous"]){
helpStatus--;
}
I would not use the string. What happens if you decide to change the label? Consider using a tag instead.
e.g. b
button.tag = 100;
...
- (void)buttonPressed:(id)sender {
UIButton *button = (UIButton*)sender;
if(button.tag == 100) {
}
}
or in your case (I am kidding, sort of), even:
button1.tag = 1;
button2.tag = -1;
...
- (void)buttonPressed:(id)sender {
UIButton *button = (UIButton*)sender;
helpStatus+= button.tag;