UIButton addTarget:action:forControlEvents: les résultats dans [NSObject doesNotRecognizeSelector:]
J'ai essayé beaucoup de choses, toujours pas de résultat.
Donc, j'ai le bouton suivant, créé par programmation dans une sous-classe de UIViewController:
rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0.0, 0.0, 110.0, 40.0);
rightButton.titleLabel.font = [UIFont fontWithName:GAME_FONT_NAME_STRING size:20.0];
[rightButton setTitle:@"MyTitle" forState:UIControlStateNormal];
rightButton.backgroundColor = [UIColor clearColor];
[rightButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[rightButton setBackgroundImage:normalImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
[rightButton addTarget:self action:@selector(myButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:rightButton];
lorsque le sélecteur est:
- (void)myButton;
J'ai tout essayé:
- (void)myButton;
- (void)myButton:(id)sender;
- (void)myButton:(id)sender forEvent:(UIEvent *)event;
- (IBAction)myButton;
- (IBAction)myButton:(id)sender;
- (IBAction)myButton:(id)sender forEvent:(UIEvent *)event;
et les sélecteurs, bien sûr:
[rightButton addTarget:self action:@selector(myButton) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton:) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton:forEvent:) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton:) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton:forEvent:) forControlEvents:UIControlEventTouchUpInside];
Le résultat est toujours une exception non interceptée - [NSObject doesNotRecognizeSelector:]. Cependant, l'habitude de trace du programme est:
#0 0x92a6bedb in objc_msgSend ()
#1 0x03b0a430 in ?? ()
#2 0x00306b4e in -[UIControl sendAction:to:forEvent:] ()
#3 0x00308d6f in -[UIControl(Internal) _sendActionsForEvents:withEvent:] ()
#4 0x00307abb in -[UIControl touchesEnded:withEvent:] ()
#5 0x002bcddf in -[UIWindow _sendTouchesForEvent:] ()
#6 0x002a67c8 in -[UIApplication sendEvent:] ()
#7 0x002ad061 in _UIApplicationHandleEvent ()
#8 0x02498d59 in PurpleEventCallback ()
#9 0x01cabb80 in CFRunLoopRunSpecific ()
#10 0x01caac48 in CFRunLoopRunInMode ()
#11 0x02497615 in GSEventRunModal ()
#12 0x024976da in GSEventRun ()
#13 0x002adfaf in UIApplicationMain ()
Alors quel est le problème avec ce bouton?
PS: je suis en utilisant le SDK de l'iPhone 3.1.3
Mise à jour!
Le code suivant dans l'AppDelegate (pas de déclarations dans l'interface):
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIButton *test = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 100.0)];
[test setTitle:@"Title" forState:UIControlStateNormal];
UIImage *bg = [UIImage imageNamed:...];
[test setBackgroundImage:bg forState:UIControlStateNormal];
[test addTarget:self action:@selector(testAction) forControlEvents:UIControlEventTouchUpInside];
[window addSubview:test];
[test release];
[window makeKeyAndVisible];
}
- (void)testAction {
NSLog(@"Write something...");
}
fonctionne parfaitement!
Mais si je crée un vide UIViewController avec le même code:
- (void)viewDidLoad {
UIButton *test = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 100.0)];
[test setTitle:@"Title" forState:UIControlStateNormal];
UIImage *bg = [UIImage imageNamed:...];
[test setBackgroundImage:bg forState:UIControlStateNormal];
[test addTarget:self action:@selector(testAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:test];
[test release];
[window makeKeyAndVisible];
}
- (void)testAction {
NSLog(@"Write something...");
}
- Je obtenir cette mystérieuse erreur. 🙁 Help!
Vous devez vous connecter pour publier un commentaire.
Un vieux post et probablement le tout trié par maintenant, mais il y a un autre problème qui va provoquer un plantage:
est incorrect.
action:@selector(testAction)
doit avoir un côlon n'y a donc:sinon vous allez obtenir une méconnu sélecteur de crash - qui est ce que vous obtenez..
LA SOLUTION!
Mon problème est que j'ai utilisé le code suivant dans l'AppDelegate:
De sorte que le point de vue de la propriété de la vue-contrôleur a été retenu et tout le reste a été purgé. La vue contrôleur n'a pas toutes les fonctionnalités (pas de tactile, pas de méthodes d'instance). C'est pourquoi le addTarget:action:forEvents: n'a pas de travail.
Donc, faire smart - libération de votre point de vue contrôleurs dans le dealloc de l'AppDelegate. 😉
où est myButton déclaré?
si elle est synthétisée, alors au lieu de
utilisation
qui devrait fonctionner. Si non, veuillez expliquer comment vous devez déclarer la propriété.