iphone xcode4 uibutton settext

iphone - UIButton setTitle forState no funciona



xcode4 settext (6)

Estoy intentando configurar el texto de un UIButton en una nueva vista cuando se presiona el botón "Siguiente" desde una vista anterior. He configurado el IBOutlet correctamente y he buscado por todas partes las respuestas a esto, pero simplemente no funciona en absoluto.

Aquí hay una muestra de lo que estoy tratando de hacer:

- (IBAction)computeQuiz:(id)sender { [fiveFootUniversalResults setTitle:@"Test" forState:UIControlStateNormal]; } - (IBAction)jumpT10ResultsView:(id)sender { NSString *nibFileToLoad = @"JumpT10Results"; UIDevice *device = [UIDevice currentDevice]; if([device userInterfaceIdiom] == UIUserInterfaceIdiomPad) { nibFileToLoad = [nibFileToLoad stringByAppendingString:@"-iPad"]; } JumpmasterPathfinderViewController *nextView = [[JumpmasterPathfinderViewController alloc] initWithNibName:nibFileToLoad bundle:nil]; // Modal view controller nextView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentModalViewController:nextView animated:YES]; [nextView release]; [scrollView setContentSize:CGSizeMake(imageViewWidth, imageViewHeight + 44.0)]; }

Estas acciones están conectadas a un botón en una vista anterior. La vista se carga bien y todo, el único problema es que el texto NO cambiará en el botón. Estoy 100% seguro de que tengo los IBOutlets configurados correctamente, simplemente no sé qué estoy haciendo mal. ¿Algunas ideas?


Esto también puede suceder si olvida usar @synthesize en el botón o las variables relacionadas con él.


Intenta llamar

- (IBAction)computeQuiz:(id)sender

método en el

- (IBAction)jumpT10ResultsView:(id)sender

El método, en lugar de llamar simultáneamente al hacer clic en el botón, aunque sea tan simultáneo como en el siguiente código, la llamada al método me da como resultado la no asignación del objeto del botón fiveFootUniversalResults.

- (IBAction)computeQuiz:(id)sender { [fiveFootUniversalResults setTitle:@"Test" forState:UIControlStateNormal]; } - (IBAction)jumpT10ResultsView:(id)sender { NSString *nibFileToLoad = @"JumpT10Results"; UIDevice *device = [UIDevice currentDevice]; if([device userInterfaceIdiom] == UIUserInterfaceIdiomPad) { nibFileToLoad = [nibFileToLoad stringByAppendingString:@"-iPad"]; } JumpmasterPathfinderViewController *nextView = [[JumpmasterPathfinderViewController alloc] initWithNibName:nibFileToLoad bundle:nil]; // Modal view controller nextView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentModalViewController:nextView animated:YES]; [nextView release]; [scrollView setContentSize:CGSizeMake(imageViewWidth, imageViewHeight + 44.0)]; [self computeQuiz:nil]; }


La respuesta en el siguiente enlace me ayudó cuando tuve el mismo problema. Me permitió guardar todos los atributos en un Diccionario. Creé una nueva cadena atribuida con texto diferente pero con los mismos atributos, que luego agregué al botón que quería cambiar. Espero que esto ayude.

¿Cambiar el texto de una UILabel atribuida sin perder el formato?


No está funcionando porque el IB establece el title attributedTitle lugar del title .

Intenta esto en su lugar:

NSAttributedString *attributedTitle = [self.myButton attributedTitleForState:UIControlStateNormal]; NSMutableAttributedString *mas = [[NSMutableAttributedString alloc] initWithAttributedString:attributedTitle]; [mas.mutableString setString:@"New Text"]; [self.myButton setAttributedTitle:mas forState:UIControlStateNormal];

O alternativamente:

[self.myButton setAttributedTitle:nil forState:UIControlStateNormal]; [self.myButton setTitle:@"New Text" forState:UIControlStateNormal];

(La segunda opción no conservará su formato.)


prueba este código ....

UIButton *backbtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 68, 38)]; [backbtn setTitle:@"Your string" forState:UIControlStateNormal]; //[backbtn setImage:[UIImage imageNamed:@"BackBtn.png"] forState:UIControlStateNormal]; [backbtn addTarget:self action:@selector(backBtnClicked) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:backbtn];


tratar:

[fiveFootUniversalResults setBackgroundColor:[UIColor redColor]]; [fiveFootUniversalResults setTitle:@"Test" forState:UIControlStateNormal];

Tal vez el color de fondo sea el mismo que el color del título. No verás nada cuando escribas en papel negro con un bolígrafo negro. Intente establecer el color de fondo o el color del título antes de llamar a setTitle.