UIBUBUTTON ADDTARGET: Ação: Forcontrolevents: Resultados em [NSObject DoNotRecognizeSelector:
-
27-09-2019 - |
Pergunta
Eu tentei muitas coisas, ainda sem resultado.
Então, eu tenho o seguinte botão criado programaticamente em uma subclasse do 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];
onde está o seletor:
- (void)myButton;
Eu tentei de tudo:
- (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;
e os seletores correspondentes, é claro:
[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];
O resultado é sempre uma exceção não capturada - [o NSObject não éCognizaLeCector:]. No entanto, o backtrace usual do programa é:
#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 ()
Então, qual é o problema com esse botão?
PS: Estou usando o iPhone SDK 3.1.3
Atualizar!
O código a seguir no AppDelegate (sem declarações na 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...");
}
funciona perfeitamente!
Mas se eu criar um UIViewController vazio com o mesmo código:
- (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...");
}
Eu recebo esse erro misterioso. :-( Ajuda!
Solução 2
A SOLUÇÃO!
Meu problema era que usei o seguinte código no AppDelegate:
UIViewController *controller = [[MyCustomController alloc] init];
[window addSubview:controller.view];
[controller release];
Portanto, apenas a propriedade View do controlador de exibição foi mantida e tudo o mais foi expurgado. O controlador de exibição não possuía nenhuma funcionalidade (sem eventos de toque, nenhum método de instância). É por isso que o AddTarget: Ação: PORRESENTES: não funcionou.
Então, faça -o inteligente - libere seus controladores de exibição no deatório do AppDelegate. ;)
Outras dicas
Uma postagem antiga e provavelmente tudo classificado até agora, mas há outro problema que causará um acidente:
[test addTarget:self action:@selector(testAction) forControlEvents:UIControlEventTouchUpInside]
está incorreto. action:@selector(testAction)
Deve ter um cólon lá assim:
action:@selector(testAction:)
Ou então você receberá um acidente seletor não reconhecido - que é o que você está recebendo ..
Onde está sendo declarado MyButton? Se for sintetizado, então em vez de
RightButton = [UIBUTTON BOTTLOWITHTYPE: UIBUTTONTYPECUSTOM];
usar
self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
Isso deve funcionar. Caso contrário, poste como você está declarando a propriedade.