Action not happening when tapping UIButton - ios

I have a UIButton placed on a UIImageView and UILabel.
When i press down the button i want an image to show in the UIImageView and the text color of the UILabel to be changed, and when i pull up my finger i want the image to disappear and the text color of the UILabel changed back to black.
- (void)highlightActivity:(id)sender {
activityImage.image = [UIImage imageNamed:[[NSMutableString alloc] initWithString:activityName]];
activityLabel.textColor = activityColor;
}
- (void)deHighlightActivity:(id)sender {
activityLabel.textColor = [UIColor blackColor];
activityImage.image = nil;
}
It works fine when i press the button and then release, but i have a problem when tapping the button, the image isn't showing at all and the text color isn't changing.
Any ideas how to make it happen when tapping too?
Thanks,
Naor.

I think the problem is you attach both
Touch Up inside and outside on deHighlightActivity.
Because, when you pressed the button, you will release (touch up) the button eventually, if you put both touch up inside and outside on deHighlightActivity, that action will be called every time. So that your tapping action calls deHighlightActivity as well.
I don't know what exactly you are trying to do here, but looks like you have some conflicts on your design.

Related

iOS change custom bar button text color when the button becomes available

So I have a registration page (and multiple other places through the registration process where this will be implemented) that has a Next button, which is set up as a custom UIButton dragged onto the nav bar. As users enter in their registration information, I want to change the color of the text from grey when it is unavailable, to a custom green color. I've read some other threads, such as this one , but they haven't been any help. I think I'm doing what they told me to correctly, but I'm getting different results than I want. Right now my code looks like this:
-(void)checkCompletion
{
if( emailEntered && passwordEntered && [_password.text length] >= 5)
{
[_nextBarButtonItem setEnabled:YES];
[_nextButton setEnabled:YES];
[_nextButton.titleLabel setTextColor:[UIColor customGreen]];
[_nextButton setTintColor:[UIColor customGreen]];
[_nextBarButtonItem setTintColor:[UIColor customGreen]];
nextAvailable = YES;
}
else
{
nextAvailable = NO;
}
}
_nextBarButtonItem is the barButtonItem, and _nextButton is the UIButton I dragged onto the navigation bar, which is underneath _nextBarBUttonItem in the hierarchy. customGreen is a category with its header file #include'd into my prefix.pch, as I use the color throughout the app. This function gets called when textfields return and when the password is edited, so I can make the button available before the return key is pressed if users don't want to dismiss the keyboard first.
I have tried several methods, such as making the button my customGreen on the storyboard and disabling both the barButtonItem and the button itself underneath the barButtonItem in the hierarchy, hoping it would grey it out, but the button is still green, you just can't press it. I made the button grey on the storyboard, then call this function, but the text ends up changing to white instead of the green color. I tried explicitly defining the color as I set it, but I get the same result. I do not want my back button to turn this green color, only the next button when it becomes available to press.
Any ideas what I'm doing wrong?

Fastest way to sequential change of UIButton hidden or hide

I want to have a function that by pressing the 1st button, then the 2nd button will be appear. When the 2nd button appear, after pressing it, then the 3rd button will appear and so on until the 10th button.
Making IBAction for each button can do the job. But it will be very time consuming. And I have multiple of these sequence needed to be done.
Is there any other way is faster and simpler to get this job done?
Thanks
For this purpose, you have to use the tag property of UIButton. while creating the UIButton set the tag value. For example for button1 it should be 1, for button2 it should be 2, and so on...
After that set the same IBAction for all UIButtons. And inside that IBAction get the button based on tag value and unhide next UIButton.
-(IBAction)buttonClicked:(id)sender
{
UIButton *button = (UIButton *)[self.view viewWithTag:(sender.tag + 1)];
button.hidden = NO;
}
You should be use loop for these process . . .
(if second button not appear only on first button click)

putting button in navigationItem.titleView programmatically

I wanted to put a timer in the title label of a tableViewController. To my surprise, storyboard let me drop a button the title and I got everything working, however, I need to button to differentiate between single and double taps. Single tap starts and stops the timer, and double tap would reset it back to zero. In order to make a button recognize both type of taps, I had to create a subclass of UIButton, which means that I am now adding the button programmatically rather than through storyboard. In viewDidLoad, I tried to add the button to the titleView of the navigationItem but nothing's showing when I run it in the simulator. I set the x,y coordinates like this
self.navigationItem.titleView.bounds.size.width/2
thinking it would place the button in the middle.
I'm not sure if I've messed up the coordinates or done something else wrong, but nothing's showing. Can you suggest how it might be done?
MMTimerButton *button = [MMTimerButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Button" forState:UIControlStateNormal];
button.frame = CGRectMake(self.navigationItem.titleView.bounds.size.width/2, self.navigationItem.titleView.bounds.size.height/2, 160.00, 40.0);
[self.navigationItem.titleView addSubview:button];

scrollView with UIImage respond touch

I'm using a simple code to add some images to my UIScrollView. Also I've implemented another code to detect touches on each image.
Here is the code:
(void)handleSingleTap:(UIGestureRecognizer *)sender
{
int senderTagIs;
senderTagIs = sender.view.tag;
if (sender.view.layer.borderColor != [UIColor cyanColor].CGColor) {
sender.view.layer.borderColor = [UIColor cyanColor].CGColor;
UIImageView *showFullImage = (UIImageView *)[self.view viewWithTag:sender.view.tag+100];
[showFullImage setTag:sender.view.tag+200];
[self.view addSubview:showFullImage];
showFullImage.hidden = NO;
NSLog(#"Show tag is: %i", sender.view.tag);
}
else
{
sender.view.layer.borderColor = [UIColor whiteColor].CGColor;
UIImageView *hideFullImage = (UIImageView *)[self.view viewWithTag:sender.view.tag+200];
[hideFullImage setTag:sender.view.tag+100];
hideFullImage.hidden = YES;
NSLog(#"Hide tag is: %i", sender.view.tag);
}
}
The above code, sets the border color to cyan and show my small images from UIScrollView, in another UIImageView.
But my problem is, that I can't set the option to hide all images and set border color white for all images when one image is touched.
Ex: If I touch the first image, then the code will work, my big UIImageView will show touched image and touched image from UIScrollView will get the cyan color for border, so far so good.
Now, If I touch third image, my first image is shown, the color border is cyan, and so... I have to touch first image again to disable, but this is not what I want.
So, we've got a few things going on here. First, I'm assuming you're trying to show a collection of images in a scroll view with some custom padding to indicate selection around them. This sounds tailor make for using UICollectionView with a custom cell.
Absent further information, you're not resetting the old color. Either keep a reference to a selected image as a class variable or, assuming your image views are in a collection object like an NSArray, begin your method by iterating through the objects and resetting their view to an unselected state.
If you just need to hack together a solution, the second option should work. I really recommend using UICollectionView. It's a bit more work in the beginning, especially if you're not experienced with it, but it's well worth learning. Here's a good tutorial on UICollectionView.

Change Background Image of an iOS app with a button click

Title pretty much says it all, I am new to Xcode and am trying to make a button that changed the view's background image. I will have three button and when you click one they will change the background that is associated with the button. How would I accomplish this? I have looked around but have not found anything relevant or anything that worked..
Thanks in advance!
EDIT::
As for what I have, I have a project set up with a window and one view with a background image called "default#2x-568h#2x.png" I would like to be able to press a button and have it switch to "second.png". I have a button and I control dragged into my .h which made this:
#interface TeslameterViewController : UIViewController <CLLocationManagerDelegate> {
IBOutlet UIButton *button1;
}
- (IBAction)button1:(id)sender;
#end
thats where I am lost.
EDIT AGAIN:
Found the fix via this video:
http://www.youtube.com/watch?feature=player_embedded&v=iKuGiJMgMiQ
On the button Action event add this line.
(void)Action:(UIButton *)sender
{
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:#"bg.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
}
try this:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"imageName.png"]];
and also see UIView Image Background for example project with complete code...
You need to be more specific on what you need.
Within your xib file, create a UIImageView and stretch its dimensions to fit the size of the screen. Also create a the necessary buttons. Create properties in your view controller to have a reference to the UIImageView and the buttons. Give the buttons an action that changes the picture that the UIImageView loads.
You have to create three buttons in nib file and add outlet to your view controller.
To create outlet you have right click on button then you will get list of event, then control drag to your view controller header file, then one small window will pop up, write method name and click enter. Now u will see method in your controller.write code to change background of button or your view in that method.
for more details:http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphone101/Articles/05_ConfiguringView.html

Resources