Trying to toggle a view on/off using tags - iphone

I am trying to toggle a view on and off which works, but what i need to know is if I am abusing tags with this method. If so is there a better way?
- (IBAction) myButton:(UIButton*)sender {
if ([myLabelText.text isEqualToString:#""])
{
// do nothing
} else {
if ( sender.tag )
{
sender.tag = 0;
[[self firstView] setHidden:YES];
} else {
sender.tag = 1;
firstView.alpha = 100;
[[self firstView] setHidden:NO];
}
}
}

You can use the tag property for anything you like, but in this case you can toggle the visibility without using it:
[self firstView].hidden = ![self firstView].hidden;
Also, the valid range for UIView's alpha property is from 0.0 to 1.0. I'm not sure what you're trying to do there.

Related

How to handle events of multiple segmented control in a single view

I have 2 segmented controls in my viewcontroller view. How can I handle the tap events of both of the segmented controllers?
There are two ways to do so.
Add different actions for every segment control
Add same actions for every segment control & check which control is tapped using its tag.
[yourSegmentedControl addTarget:self action:#selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
- (IBAction)segmentSwitch:(id)sender
{
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
if(segmentedControl.tag == someTag)
{
if(segmentedControl.selectedSegmentIndex == 1)
{
// your code
}
else if(segmentedControl.selectedSegmentIndex == 2)
{
// your code
}
}
else if(segmentedControl.tag == someTag)
{
if(segmentedControl.selectedSegmentIndex == 1)
{
// your code
}
else if(segmentedControl.selectedSegmentIndex == 2)
{
// your code
}
}
}
Apple docs says:
http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UISegmentedControl_Class/Reference/UISegmentedControl.html
You register the target-action methods for a segmented control using the UIControlEventValueChanged constant as shown below.
[segmentedControl addTarget:self
action:#selector(action:)
forControlEvents:UIControlEventValueChanged];
So, you just have to register action for every segmented control.
Set the tag property on each segmented control to a different integer. Then in your method you set as the action for when the value changes, check which integer the tag property is set to using [sender tag].
You can use selected mode of segment:
UISegmentedControl *tempSegment = sender;
if ([tempSegment selectedSegmentIndex] == 0){
//first Action
}
else if ([tempSegment selectedSegmentIndex] == 1){
//second Action
}
Assign two different actions to these segmented controls:
[segmentedControl addTarget:self
action:#selector(action:)
forControlEvents:UIControlEventValueChanged];
Swift version:
#IBAction func yourFunctionName(sender: UISegmentedControl) {
if (sender.selectedSegmentIndex == 0){//choice 1
}else{//choice 2
}
}

i have a problem in UIImageView's animating

-(IBAction) change {
self.imageView.animationImages = myImages;
self.imageView.animationDuration = 2;
if(self.imageView.isAnimating == NO){
[self.imageView startAnimating];
NSLog(#"if bool = %d", self.imageView.isAnimating);
}
else {
self.imageView stopAnimating];
NSLog(#"else bool = %d", self.imageView.isAnimating);
}
}
hello, i'm studying iOS programming.
but i have a question.
i have a button and when i click the button, then this method will be called.
first i click the button, then this code will start the if statement. that's what i want.
i click the button again, i think that will execute the else statement.
but it always execute the if statement only.
why is that?
i really don't know why is that. please help me
I think setting the properties like animationImages or animationDuration will stop the animation, so that by clicking, you every time stop and then just after (re)start it in the if part. Try setting these two properties outside the action method you wrote, and just let the if/else sequence.
-(IBAction) change {
// set these two anywhere else
//self.imageView.animationImages = myImages;
//self.imageView.animationDuration = 2;
if(self.imageView.isAnimating == NO){
[self.imageView startAnimating];
NSLog(#"if bool = %d", self.imageView.isAnimating);
}
else {
self.imageView stopAnimating];
NSLog(#"else bool = %d", self.imageView.isAnimating);
}
}

hiding and displaying uitoolbar on same button click

I am trying to hide and display a UIView on BarButtonItem click. Priviously i also posted the question regarding same but didnt find any suitable answer. I created UIView manually in IB and just placed it in view so it must be shown as soon as view is loaded but i made it hidden in viewDidLoad method by writing
myvew.hidden = YES;
secondly, when i click BarButtonItem then i set
-(IBAction)mymethod
{
myview.hidden = NO;
}
so its diplaying view but when i again click on it it must hide.. how do i do it?
Put the following statement in your button action
myview.hidden = !myview.hidden ;
So your code must be like below.
-(IBAction)mymethod
{
myview.hidden = !myview.hidden ;
}
if (myview.hidden == YES)
{
myview.hidden = NO;
}
else
{
myview.hidden = YES;
}
Check if the view is already hidden and then show, and if not hidden then hide it.
You should do in this way
-(IBAction)mymethod
{
if( myview.hidden == NO ) myview.hidden = YES;
else myview.hidden = NO;
}

Having more than one if statements in one IBAction in the .m file isn't working

I am making an iPhone app that will generate something based on quiz answers. I want the button for the answer to change the question text and sometimes the button label text.
Background Info:
'a' is a button
'questionNumber' is the question number (duh) and it is why the button will change the texts differently each time it is pressed.
Here is the code I am using:
-(IBAction) a
{
questionNumber == 0;
if(questionNumber == 0) {
question.text = #"How Much Do You Use Suppressed Weapons?";
}
questionNumber == 1;
if(questionNumber == 1) {
question.text = #"Do You Like Sleight of Hand?";
answerA.text = #"Yes";
answerB.text = #"No";
[answerC setHidden:YES];
[answerD setHidden:YES];
[answerButton3 setHidden:YES];
[answerButton4 setHidden:YES];
}
}
and this repeats for the other buttons (b,c, and d). I thought it should work, but it doesn't do the "do you like sleight of hand" question. It just stays at the how much do you use suppressed weapons question. Please Help! I really want to get into xcoding for the iphone.
BTW, I am not sure that the tags for this question are correct.
Related Question: How can I have an IBAction that has more than two 'if' statements?
You're not assigning to questionNumber. You're comparing.
questionNumber == 1;
That's a noop. It tests if questionNumber is 1, and then throws away the result. You want
questionNumber = 1;
Try using a switch statement with breaks.
switch (questionNumber)
{
case 0:
{
question.text = #"How Much Do You Use Suppressed Weapons?";
}
break;
case 1:
{
question.text = #"Do You Like Sleight of Hand?";
answerA.text = #"Yes";
answerB.text = #"No";
[answerC setHidden:YES];
[answerD setHidden:YES];
[answerButton3 setHidden:YES];
[answerButton4 setHidden:YES];
}
break;
}

Is there a cost associated with over using hidden on a CALayer?

Was wondering if someone might know the answer to this question - is there a cost associated with over hiding a layer.
For example, if a layer is already hidden, does calling layer.hidden = YES over and over have any more overhead than simply stepping through the call stack? Is Cocoa smart enough to know that the layer is already hidden?
- (void) drawRect:(CGRect)rect
{
if ( characterLeftFlag ) {
characterLeftView.layer.hidden = NO;
characterRightView.layer.hidden = YES;
}
else {
characterLeftView.layer.hidden = YES;
characterRightView.layer.hidden = NO;
}
}
Or do I need to always check to see if the layers visibility has changed and then call hidden?
- (void) drawRect:(CGRect)rect
{
if ( characterLeftFlag && characterLeftView.layer.hidden ) {
characterLeftView.layer.hidden = NO;
characterRightView.layer.hidden = YES;
}
else if ( characterRightFlag && characterRightView.layer.hidden ) {
characterLeftView.layer.hidden = YES;
characterRightView.layer.hidden = NO;
}
}
The first pattern allows closure on the conditional statement which results in better coding practice but does result in extra stack calls, while the second pattern is more explicit but results in a conditional statement that falls through.
Thanks,
Bryan
Why not do this:
- (void)setCharacterLeftFlag:flag {
if ( flag ) {
characterLeftView.layer.hidden = NO;
characterRightView.layer.hidden = YES;
}
else {
characterLeftView.layer.hidden = YES;
characterRightView.layer.hidden = NO;
}
}
this is your setter. Now, the OS may not even call drawRect, if it sees that the layer is hidden, and doesn't need to be redrawn.
Quartz will probably optimize setting hidden = YES on a hidden layer, but if you really want to find out you should benchmark.