Why is UIControlTouchUpInside UNDEFINED? - iphone

I've got a simple question but it has got me confounded. The code below gets an UNDEFINED error for UIControlTouchUpInside. What do I need to include or import? If I comment out the line, it works. So that means forState:UIControlStateNormal is defined. I'm new so hope you can help.
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Accept?" forState:UIControlStateNormal];
[button addTarget:self action:#selector(acceptTapped) forControlEvents:UIControlTouchUpInside];

You don't need to import anything. You need only consult the documentation, where you will find that the symbol you're looking for is UIControlEventTouchUpInside. You could also just type "UIControl" in XCode and hit escape. UIControlEventTouchUpInside will be in the list that pops up.

Related

UIButton Bug? (Xcode Version 4.6 (4H127))

1st: Xcode Version 4.6 (4H127), simulator 6.1
So... i do the following.
My button (inherited from UIButton) loaded from nib. Then i've set image to it for UIControlStateNormal.
I've got something like this:
After that i need to nullify it:
[button setImage:nil forState:UIControlStateNormal];
then i see this:
Check diffs:
So the title layouted right, but the image still there...
Also, if i do following:
[button setImage:[UIImage new] forState:UIControlStateNormal];
I get result like the second one, but without an image (as expected, but not fully, without layouting):
I am really interested in what is happening here?
Its really interesting, mb Apple broke something? Or i've just doing all wrong? But i've done it many times(setting image->nil), all worked Okay.
Thank you for reading :) I would appreciate for any help.
I have wrote a sample code regarding this.
Your both calls worked me fine
[self.myCustomButton setImage:nil forState:UIControlStateNormal];
[self.myCustomButton setImage:[UIImage new] forState:UIControlStateNormal];
I think, you may set the BackGroundImage instead of image.
Sample Methods
-(IBAction)removeImage:(id)sender{
[self.myCustomButton setImage:nil forState:UIControlStateNormal];
//[self.myCustomButton setImage:[UIImage new] forState:UIControlStateNormal];
}
-(IBAction)setImage:(id)sender{
[self.myCustomButton setImage:[UIImage imageNamed:#"Button_Disable.jpg"] forState:UIControlStateNormal];
}
This thing was here:
My subclass of UIButton overrides -layoutSubviews, where setting imageView.hidden using some criteria.
So, if hidden property was YES, when i've calling setImage:forState - this bug happened.
My fix was next:
I just start using alpha property instead of hidden. (I know that is dirty, but this is the fastest way i've found)

How to make a UILabel work as a url that's clickable and has url effect (when getting clicked)?

I know there is three20 solution. But I don't want to use three20 just for that purpose. So programmingly, is there a solution for that?
Yes there is a solution. You can use the regular expression to detect particular kind of strings on touches began of the label and then based on the regular expression which matches the string you can perform the action.
So if you want to do that, you need to subclass the UILabel and overwrite the Touch methods for the label.
Hope this helps you.
There is one of the Label's called OHAttributedLabel which does similar function. And here is a fork, that supports ARC.
You can use a UIButton with the button type as UIButtonTypeCustom, on the button click you can call the url.
UIButton *urlButton = [UIButton buttonWithType:UIButtonTypeCustom];
urlButton.frame = CGRectMake(10, 10, 80, 30);
[urlButton setTitle:#"Click Here" forState:UIControlStateNormal];
[urlButton addTarget:self action:#selector(urlButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:urlButton];

How to determine which event called imageMoved:withEvent:forControlEvents?

I've added some targets to a button. They work fine. But in my imageMoved... method, how do I determine which one of the two events fired it? Here is where I added the targets:
[button addTarget:self action:#selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[button addTarget:self action:#selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragExit];
I saw an example recently where the event is tested in the imageMoved method, something like:
if (event == UIControlEventTouchDragExit )
but that won't compile. I'm not finding any examples online nor any clues in UIControl Class Reference.
What I'm trying to do with this is store the original location of the button/image so that if the user doesn't complete the drag to a target it will snap back to where it came from.
Thanks for any help anyone can offer.
Use two different methods
[button addTarget:self action:#selector(imageMoved:withDragInsideEvent:) forControlEvents:UIControlEventTouchDragInside];
[button addTarget:self action:#selector(imageMoved:withDragExitEvent:) forControlEvents:UIControlEventTouchDragExit];
and reuse common functionality in 3rd method.

How is a UIButton's image set programmatically?

How does one set the image of a UIButton from within the code?
I try the following but it just leaves the button as a white square.
[A1 setImage:[UIImage imageNamed:[NSString stringWithFormat:#"m1.10001.png"]] forState:UIControlStateNormal];
Your image construction seems a bit overly complicated, but it should work. Don't forget to use the correct button type (custom):
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"m1.10001.png"] forState:UIControlStateNormal];
With newer SDKs you can omit the .png. Make sure that the image actually exists (you can store it in a temporary variable and check for nil, for example).
And of course make sure that the button exists (or is bound), i.e. non-nil, when setting the image.

UIImageView as a link to a URL

How to make a UIImageView open a URL in Safari when user clicks it?
I would suggest using a button for this purpose instead. You can create a custom UIButton with whatever image you want. This gives the advantage of providing the built-in target-action mechanism of a button, as well as being able to provided a highlighted image to provide the user feedback. Consider using something like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"regular_image.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"highlighted_image.png"] forState:UIControlStateHighlighted];
[button addTarget:self action:#selector(loadURL) forControlEvents:UIControlEventTouchUpInside];
Note that it will still look like "just an image" to the user.