selector

Selector in swift3

ぃ、小莉子 提交于 2019-11-26 10:57:17
问题 Why doesn\'t this work in swift 3 ? It crashes at runtime saying: \'-[my_app_name.displayOtherAppsCtrl tap:]: unrecognized selector sent to instance 0x17eceb70\' override func viewDidLoad() { super.viewDidLoad() // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = false // Register cell classes //self.collectionView!.register(ImageCell.self, forCellWithReuseIdentifier: reuseIdentifier) // Do any additional setup after loading

Trying to handle “back” navigation button action in iOS

≯℡__Kan透↙ 提交于 2019-11-26 10:24:54
I need to detect when the user taps the "back" button on the navigation bar, in order to perform some operations when that occurs. I'm trying to set manually an action to such button, this way: [self.navigationItem.backBarButtonItem setAction:@selector(performBackNavigation:)]; - (void)performBackNavigation:(id)sender { // Do operations [self.navigationController popViewControllerAnimated:NO]; } I firstly placed that code in the view controller itself, but I found that self.navigationItem.backBarButtonItem seemed to be nil , so I moved that same code to the parent view controller, which pushes

Passing arguments to selector in Swift

怎甘沉沦 提交于 2019-11-26 09:30:02
问题 I\'m programmatically adding a UITapGestureRecognizer to one of my views: let gesture = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(modelObj:myModelObj))) self.imageView.addGestureRecognizer(gesture) func handleTap(modelObj: Model) { // Doing stuff with model object here } The first problem I encountered was \"Argument of \'#selector\' does not refer to an \'@Objc\' method, property, or initializer. Cool, so I added @objc to the handleTap signature: @objc func

Creating a selector from a method name with parameters

人走茶凉 提交于 2019-11-26 08:46:55
问题 I have a code sample that gets a SEL from the current object, SEL callback = @selector(mymethod:parameter2); And I have a method like -(void)mymethod:(id)v1 parameter2;(NSString*)v2 { } Now I need to move mymethod to another object, say myDelegate . I have tried: SEL callback = @selector(myDelegate, mymethod:parameter2); but it won\'t compile. 回答1: SEL is a type that represents a selector in Objective-C. The @selector() keyword returns a SEL that you describe. It's not a function pointer and

Using -performSelector: vs. just calling the method

别说谁变了你拦得住时间么 提交于 2019-11-26 08:43:48
问题 I\'m still kind of new to Objective-C and I\'m wondering what is the difference between the following two statements? [object performSelector:@selector(doSomething)]; [object doSomething]; 回答1: Basically performSelector allows you to dynamically determine which selector to call a selector on the given object. In other words the selector need not be determined before runtime. Thus even though these are equivalent: [anObject aMethod]; [anObject performSelector:@selector(aMethod)]; The second

iOS - How to implement a performSelector with multiple arguments and with afterDelay?

冷暖自知 提交于 2019-11-26 05:58:08
问题 I am an iOS newbie. I have a selector method as follows - - (void) fooFirstInput:(NSString*) first secondInput:(NSString*) second { } I am trying to implement something like this - [self performSelector:@selector(fooFirstInput:secondInput:) withObject:@\"first\" withObject:@\"second\" afterDelay:15.0]; But that gives me an error saying - Instance method -performSelector:withObject:withObject:afterDelay: not found Any ideas as to what I am missing? 回答1: Personally, I think that a closer

when to use @objc in swift code?

我是研究僧i 提交于 2019-11-26 05:28:47
问题 I am new to swift, and I see some methods like: @objc private func doubleTapGestureRecognized(recognizer: UITapGestureRecognizer) I was wondering, when to use @objc? I read some documents, but they are saying when you want it to be callable in Objective-C, you should add @objc flag However, this is a private function in swift, what does the @obj do? 回答1: private mean it visible only in Swift. so use @objc to visible in Objective-C. If you have a func to selector a private func in swift, it is

SEL performSelector and arguments

流过昼夜 提交于 2019-11-26 05:26:46
问题 It seems like there should be an easy way to call a selector with some arguments when all you have is a SEL object. I can\'t seem to find the correct syntax. -(MyClass*) init: (SEL)sel owner:(NSObject*) parent { int i =10; [parent performSelector:sel:i ]; } 回答1: Take a look at the NSObject documentation. In this case: [parent performSelector:sel withObject:[NSNumber numberWithInt:i]]; (note this method is actually listed in the NSObject protocol documentation). Since -[NSObject

Objective-C: Calling selectors with multiple arguments

烈酒焚心 提交于 2019-11-26 04:34:19
问题 In MyClass.m, I\'ve defined - (void) myTest: (NSString *) withAString{ NSLog(@\"hi, %@\", withAString); } and the appropriate declaration in MyClass.h . Later I want to call [self performSelector:@selector(mytest:withAString:) withObject: mystring]; in MyClass.m but I get an error similar to * Terminating app due to uncaught exception \'NSInvalidArgumentException\', reason: \'* -[MyClass myTest:withAtring:]: unrecognized selector sent to instance 0xe421f0\' I tried a simpler case with a

“unrecognized selector sent to instance” error in Objective-C

[亡魂溺海] 提交于 2019-11-26 03:28:05
问题 I created a button and added an action for it, but as soon as it invoked, I got this error: -[NSCFDictionary numberButtonClick:]: unrecognized selector sent to instance 0x3d03ac0 2010-03-16 22:23:58.811 Money[8056:207] *** Terminating app due to uncaught exception \'NSInvalidArgumentException\', reason:\'*** -[NSCFDictionary numberButtonClick:]: unrecognized selector sent to instance 0x3d03ac0\' This is my code: - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil