Can uilabel executes action - ios

In Instagram in the header view we could see there is the profile image and the username right , so I am thinking how is this possible because the username is a UILabel how when it's clicked on it sent you the user homepage . Any idea ?

You can add a UITapGestureRecognizer to the label to make it clickable.
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tappedOnLabel:)];
[label setUserInteractionEnabled:YES];
[label addGestureRecognizer:gesture];
-(void)tappedOnLabel:(UIGestureRecognizer*)gestureRecognizer
{
// Perform your action
}

Yes it can, check "User Interaction Enabled" in the Attribute Inspector, and add an Action method with it. Make sure you connect the action method to the label
EDIT-
I am showing this in code as i cant post screenshots.
Create a Label #property(nonatomic, weak) IBOutlet UILabel *myLabel;
Connect the Label in Storyboard.
Create a method, that you want to do when user Taps the label.
-(void)showHello{
NSLog(#"Hello World");
}
Now declare a
UITapGestureRecognizer *tap;
I used tapGesture as I want the action to run when I TAP the label.Declare this Gesture as an Instance Variable
in viewDidLoad alloc and init the gesture and add it to the Label
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myLabel.userInteractionEnabled = YES;
tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(showHello)];
[self.myLabel addGestureRecognizer:tap];
}
Yup that should do it

Related

How to select UIImage in storyboard from View Controller

I am currently trying to select a UIImageView that I have created in the storyboard in my view controller.
Is there a method equivalent to android's findViewbyId in objective c?
Why not just make an IBOutlet for the imageview?
Then you can just access it by something like
self.imageView
Where imageView is what you named your outlet.
Place a UIButton over your UIImageView.
After creating an IBOutlet as Samantha suggested you will need to add a gesture recogniser to your UIImageView.
I would suggest adding the gesture recognizer in viewDidLoad:
-(void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *singlePress = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageViewPressed)];
[singleTap setNumberOfTapsRequired:1];
[_imageView addGestureRecognizer:singleTap];
}
- (void)imageViewPressed {
//Perform imageView action here
}
Use the viewWithTag
UIImageView *yourImage = (UIImageView*)[self.view viewWithTag:20];

iOS tap recognizer to catch all taps

I want a pretty simple thing - in my top controller (which is navigation controller) to set up a tap gesture recognizer which will catch all the taps everywhere on the view. Currently when I tap on a button the system is not even thinking to bother my recognizer (except the gestureRecognizer:shouldReceiveTouch: delegate method, where I return YES). Instead, it just executes a button click. So I want to install "the strongest" recognizer on a view hierarchy no matter what.
You might try putting an empty UIView on top of all other views and add the UITapGestureRecognizer to it. We do something similar with help overlays. The biggest issue is figuring out how and when to ignore the touches so the underlying buttons get them when needed.
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *b = [UIButton buttonWithType:UIButtonTypeInfoDark];
b.frame = CGRectMake(50,50, b.bounds.size.width, b.bounds.size.height );
[self.view addSubview:b];
UIView *invisibleView = [[UIView alloc] initWithFrame:self.view.bounds];
invisibleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[invisibleView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapHit:)]];
[self.view addSubview:invisibleView];
}
-(void)tapHit:(UITapGestureRecognizer*)tap {
NSLog( #"tapHit" );
}
#end

Hide an imageview by clicking it?

When tapping a specific button in my app I want an image to show, I did this using an UIImageView. Then I want to hide that image by tapping it, but I don't understand how to do this?
I tried the following code, but it doesn't work.
#implementation ViewController
-(IBAction)pic {
UIImage *img = [UIImage imageNamed:#"test.png"];
[ImageView setImage:img];
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapRecognize = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleTap:)];
tapRecognizer.delegate = self;
[imageView addGestureRecognizer:tapRecognizer];
}
- (void)handleTap:(UITapGestureRecognizer *)tapGestureRecognizer {
//handle tap
}
Its pretty simple.
Use an UIImageView instead and check that userInteractionEnabled is YES on the UIImageView. Then you can then add a gesture recognizer.
Your .h file should have atleast something like below:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UIGestureRecognizerDelegate>
#property (weak, nonatomic) IBOutlet UIImageView *touchImageView;
#end
Dont forget to connect UIImageView from your storyboard to property declared above.
in your .m file:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.touchImageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleTap:)];
tapRecognizer.delegate = self;
[self.touchImageView addGestureRecognizer:tapRecognizer];
}
- (void)handleTap:(UITapGestureRecognizer *)tapGestureRecognizer {
//handle tap
self.touchImageView.alpha = 0.0f;
}
You could put an image on a button instead. I think using a UIImageView is the right decision though. You need to hook up a gesture to it programmatically. You can do this using something similar to below:
let singleFingerTap = UITapGestureRecognizer(target: self, action: "viewTapped:")
imageView.addGestureRecognizer(singleFingerTap)
You can add a tap gesture recognizer to the UIImageView that contains the image.
var tapGesture = UITapGestureRecognizer(target: <#AnyObject#>, action: <#Selector#>)
In the method you assign as the action, just set myImageView.alpha = 0. This should essentially "hide" your image view. You could also set the height of the image view to 0 if you wanted to hide it in that sense.
An alternative could be to import an open-sourced project, such as the AKImageViewer, to get posts to appear full screen (giving the user a better full view) and allowing them to swipe or cancel to get away from the image (similar to viewing images in the Twitter app.

Optimize touchesBegan UITextField

Next, I learned that to close the virtual keyboard when clicked anywhere on the screen, we must implement the touchesBegan method together with the name of the variables, which bind with the existing view UITextFields.
If we have about 10 text fields, I learned that I have to repeat this command 10 times:
[MyFristTextField resignFristResponder];
[MySecondTextField resignFristResponder];
...
[MyTenTextField resignFristResponder];
Is not there any easier way to do this, for example, call UITextFields all at once?
Keep a reference to the current UITextField in a property. Then send resignFirstResponder to that UITextField when you want the keyboard to dismiss.
You do not need to implement 10 times.
What you can do is just use:
[self.view endEditing:YES];
At the method that you are calling from the tap Gesturrecgnizer.
For example:
self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(closeAllKeyBourds)];
[self.view addGestureRecognizer:self.tap];
Your code should look something like:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(closeAllKeyBourds)];
[self.view addGestureRecognizer:self.tap];
}
{
[self.view endEditing:YES];
[self.view removeGestureRecognizer:self.tap];
self.tap = nil;
}
Tip:
If you are using 10 TextField, you better place then on a scroll view, so you can lift the scroll view up when using a lower text Field.
Hope this helps!
Edit
You have to keep an instance variable, or a property of the TapGestureRecognizer, so you can refer to it when the tap occurred.
So you keep a property at your #inerface, it should look like so:
//At your interface:
#interface LogInpageViewController : UIViewController
//Keep a property
#property (nonatomic,strong) UITapGestureRecognizer *tap;

Handle tap gesture with an argument iphone / ipad

My problem is similar to this one with the only exception - my ImageView appears at the same place inside the window with different content in it. Content has unique identifier which I want to use to call content-specific actions.
To quickly recap, the guy is looking for a way to pass a parameter into the selector section of the initWithTarget method.
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:itemSKU:)];
How can I pass an attribute to the handleTapGesture method or how do I read the unique value otherwise?
Any thoughts appreciated.
EDIT: The content is being pulled from a database and is different every time. The unique identifier is pretty much like an SSN - doesn't repeat.
You could set the UIImageView tag property with your content identifier, and then read that information form the selector.
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
[imageView addGestureRecognizer:tapGesture];
imageView.tag = 0;
And then:
- (void)handleTapGesture:(UITapGestureRecognizer *)sender
{
if( ((UIImageView *) sender.view).tag == 0 ) // Check the identifier
{
// Your code here
}
}
Try extending the UIImageView and add whatever values (as properties) and methods you will need.
#interface UIImageViewWithId: UIImageView
#property int imageId;
#end
Then, if you want to be even MORE awesome you may want to encapsulate your behavior in this "widget"'s implementation. This will keep your ViewController nice and clean, and allow you to use this widget across multiple controllers.
#implementation UIImageViewWithId
#synthesize imageId;
- (void)handleTapGesture:(UIGestureRecognizer *)gesture {
NSLog("Hey look! It's Id #%d", imageId);
}
#end
Then just delegate the tap to the individual UIImageViewWithIds
UIImageViewWithId *imageView = [[UIImageViewWithId ... ]]
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget: imageView action:#selector(handleTapGesture:)];

Resources