Pass a NSDictionary as parameter to UITapGestureRecognizer
Is there a reason you can't store the information in the owning view controller? Is it for abstraction?
You can always extend UITapGestureRecognizer to carry more data:
@interface UserDataTapGestureRecognizer : UITapGestureRecognizer
@property (nonatomic, strong) id userData;
@end
@implementation UserDataTapGestureRecognizer
@end
...
UserDataTapGestureRecognizer *downloadOptionPressed =
[[UserDataTapGestureRecognizer alloc] initWithTarget:self
action:@selector(timeFrameLabelTapped:)];
downloadOptionPressed.userData = parameters;
...
- (void)downloadOptionPressed:(UserDataTapGestureRecognizer *)recognizer {
NSArray *parameters = recognizer.userData;
}
pass parameter to UITapGestureRecognizer
You can extend a UITapGestureRecognizer for holding more data:
// MYTapGestureRecognizer.h
@interface MYTapGestureRecognizer : UITapGestureRecognizer
@property (nonatomic, strong) NSString *data;
@end
// MYTapGestureRecognizer.m
@implementation MYTapGestureRecognizer
@end
// =====================
....
MYTapGestureRecognizer *singleTap = [[MYTapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
singleTap.data = @"Hello";
.....
// ====================
-(void)tapDetected:(UITapGestureRecognizer *)tapRecognizer {
MYTapGestureRecognizer *tap = (MYTapGestureRecognizer *)tapRecognizer;
NSLog(@"data : %@", tap.data);
}
How to add a parameter to UITapGestureRecognizer so that the action function can access that parameter
Make your custom View and store the parameter that you want to pass through the Gesture Recognizer inside the view.
class GestureView: UIView{
var myViewValue: String? // Or whichever type of value you want to store and send via the gesture
}
When you initiate your view, add the value as per your requirement:
let panReceptor = GestureView()
panReceptor.myViewValue = "Hello World"
Add a simple TapGesture on this custom view and you may pass the value as below:
let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(viewTapped(sender:)))
panReceptor.addGestureRecognizer(tapGesture)
@objc func viewTapped(sender: UITapGestureRecognizer){
guard let unwrappedView = sender.view as? GestureView else { return }
print("Gesture View value : \(unwrappedView.myViewValue)")
}
In the above example I have in effect passed a String parameter through the sender.view.
You may pass any type in this manner and use the value as per your requirement in the selector method.
Write a method with argument in ios
- (void)setPhoneNumber:(NSString *)phoneNumber; // set a phoneNumber property
- (void)prepareToBeTapped
{
UITapGestureRecognizer *tapFirstGuy = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(makeCallToThisPerson:)];
[self addGestureRecognizer:tapFirstGuy];
}
- (void)makeCallToThisPerson:(id)sender
{
NSString *phoneURL = [NSString stringWithFormat:@"tel:%@", phoneNumber];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneURL]];
}
Pass dictionary of variable bindings to selector
You don't need a dictionary as an array will do:
NSArray *textFields = @[
self.verificationCodeField,
self.verificationCodeField1,
self.verificationCodeField2,
self.verificationCodeField3,
self.verificationCodeField4
];
and then your code will work:
for (UITextField *textField in textFields) {
[textField addTarget:self
action:@selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
}
How do I get the values as an NSDictionary in Swift 3?
You need to convert your string to JSON and then get value out. Here is an example to get user level information. Don't forget to handle unwrapping :)
let str = "{\"error\":false,\"user\":{\"id\":3,\"username\":\"anto\",\"email\":\"a@a\",\"nome\":\"Anto\",\"cognome\":\"xxx\",\"telefono\":\"1234567892\"},\"prodotti\":[{\"username\":\"anto\",\"id_prodotto\":57,\"nome_prodotto\":\"milk\",\"categoria\":\"milk\",\"quantita\":\"2\"},{\"username\":\"anto\",\"id_prodotto\":58,\"nome_prodotto\":\"farina\",\"categoria\":\"alimento\",\"quantita\":\"2\"}]}"
let data = str.data(using: .utf8)
do{
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: Any]
let userDic = json?["user"] as? [String: Any]
dump(userDic?["id"])
dump(userDic?["username"])
}catch let error{
}
Passing data from one view controller to another
Make string variable in your AppDelegate Like
var Model_id = String()
and where you are getting value of username and userid sends its value to AppDelegate. make AppDelgate object like below and same in destinationViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
to pass value to app delegate variable
self.appDelegate. Model_id = "your Value"
and get your values wherever you want to fetch like below
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let string = appDelegate. Model_id as! String
Cannot initialize a parameter of type 'id _NonNull' with an lvalue of type double
The contents of NSArray
(and NSDictionary
) in Objective-C must be objects. All scalar types int
double
etc. are not objects.
There is an easy solution:
Wrap all scalar types in the shortcut NSNumber
initializer @()
double var1 = 12.0;
NSString *var2 = @"Foo";
NSArray *valArray = [NSArray arrayWithObjects: @(var1), var2, nil];
or still shorter
NSArray *valArray = @[@(var1), var2];
To get the double
type back from the array you have to write
double var3 = valArray[0].doubleValue;
Side note: Variable names are supposed to start with a lowercase letter.
Related Topics
CSS Flexible Box Layout on Ipad
How to Know Which Line of Code Has Caused My iOS App to Crash in Xcode 9
Go Back to View Controller from Skscene
Passing Values from One View Controller to Another in Swift
iOS Swift Streaming App Does Not Play Music in Background Mode
My App Is Getting Crashed on UIdocumentpickerviewcontroller
Customize Google Maps Blue Dot for Current Location
Googlemap API Gives Wrong Coordinates for Direction Between Two Points
Pass Extra Argument for UItapgesturerecognizer with Selector
Notification in Swift Every Day at a Set Time
Terminating App Due to Uncaught Exception 'Nsinvalidargumentexception' - iOS Google Sign In
How to Get a Low Res Image, or Thumbnail from the Alassetrepresentation in Swift
Swift - Add Same Navigation Bar Items to Every Page
Google Signin: Unable to Disconnect iOS App
Default Uifont Size and Weight But Also Support Preferredfontfortextstyle
Uitextview - Can't Set Underline or Strikethrough Attributes on Text