English 中文(简体)
NSSelectorFromString
原标题:NSSelectorFromString

Is there something wrong with this code? I m trying to set up some buttons by defining at run time the label, the callback selector, and, later, a pointer to the UIButton itself. But with this code, I get EXC_BAD_ACCESS. It gos away if I delete the line with NSSelectorFromString. But since this is just an object being added to the dictionary, I don t understand shy it fails.

NSMutableDictionary *attachButtonDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                        @"Attach To Job",@"keyForLabel",
                                        NSSelectorFromString(@"attachToJob"), @"keyForSelector",
                                        nil];
最佳回答

a 选择人不是排他性物体;选择人代表方法名称不透明。

the program will crash when adding it to the dictionary because it cannot be messaged. for example, it cannot be retained when added.

问题回答

页: 1 页: 1

仅储存插座和在你建立 but子电话NSSelectorFromString(时。

Best, Christian

Edit:

NSMutableDictionary *attachButtonDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                @"Attach To Job",@"keyForLabel",
                                                @"attachToJob", @"keyForSelector",
                                                nil];

UIButton *fancyButton = [[UIButton alloc] init];
[fancyButton addTarget:self action:NSSelectorFromString([attachButtonDictionary objectForKey:@"keyForSelector"]) forControlEvents:UIControlEventTouchUpInside];

虽然你可以把一名选择人储存在字典上,但你可以把他装在安全局。

SEL fooSel = @selector(foo);
NSValue *selWrapper = [NSValue valueWithPointer:fooSel];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:selWrapper 
                                                               forKey:@"foo"];
SEL myFooSel = [[dict objectForKey:@"foo"] pointerValue];
[obj performSelector:myFooSel];




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签