如何在不同视图中按下另一个按钮时更改按钮的文本
How to change text of a button when another button is pressed in a different view
在我的项目中,我试图计算选定的行数,然后当我按下按钮时,它会转到上一个视图,并且该视图中的按钮文本应更改为选定的行数。我意识到我一定犯了一个简单的错误,但无法弄清楚。我将不胜感激。
这是我在 .m 文件中使用的代码:
- (IBAction)doneButton:(id)sender
{
appDelegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
CountryModel *countryModel = [[CountryModel alloc]init];
CompareViewController *compareViewController = [[CompareViewController alloc]init];
[compareViewController. addCountries setTitle:[NSString stringWithFormat:@"%ld Countries Selected", countId] forState:(UIControlState)UIControlStateNormal];
NSLog(@"%ld",countId);
// compareViewController.addCountries.titleLabel.text = [NSString stringWithFormat:@"%ld Countries Selected", countId];
compareViewController.Cid = countryModel.Id;
[self.navigationController pushViewController:compareViewController animated:YES];
}
这里是 CompareViewController 的 .h 代码:
@interface CompareViewController : UIViewController
@property (assign) NSInteger Cid;
- (IBAction)addRemoveCountries:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *addCountries;
从你的代码中我看到你甚至在初始化视图之前就设置了标题。您应该做的是将 属性 添加到您的 CompareViewController
设置中。并在 CompareViewController
的 viewDidLoad:
方法中将此标题设置为您的按钮。
编辑:
在你的 CompareViewController.h 添加一个 属性:
@property (nonatomic, strong) NSString *buttonTitleyouNeedToSet;
在doneButton:
中初始化后添加以下ling compareViewController
compareViewController.buttonTitleyouNeedToSet = [NSString stringWithFormat:@"%ld Countries Selected", countId];
In CompareViewController.m in viewDidLoad:
设置按钮标题:
[addCountries setTitle:buttonTitleyouNeedToSet forState:(UIControlState)UIControlStateNormal];
像这样
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSArray *arr = appDelegate.navController.viewControllers;
for (UIViewController *vc in arr) {
if (vc isKindOfClass:[CompareViewController class]) {
CompareViewController *cvc = (CompareViewController *)vc;
[cvc.btn setTitle:@"Hello" forState:UIControlStateNormal];
break;
}
}
希望这对您有所帮助
在我的项目中,我试图计算选定的行数,然后当我按下按钮时,它会转到上一个视图,并且该视图中的按钮文本应更改为选定的行数。我意识到我一定犯了一个简单的错误,但无法弄清楚。我将不胜感激。
这是我在 .m 文件中使用的代码:
- (IBAction)doneButton:(id)sender
{
appDelegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
CountryModel *countryModel = [[CountryModel alloc]init];
CompareViewController *compareViewController = [[CompareViewController alloc]init];
[compareViewController. addCountries setTitle:[NSString stringWithFormat:@"%ld Countries Selected", countId] forState:(UIControlState)UIControlStateNormal];
NSLog(@"%ld",countId);
// compareViewController.addCountries.titleLabel.text = [NSString stringWithFormat:@"%ld Countries Selected", countId];
compareViewController.Cid = countryModel.Id;
[self.navigationController pushViewController:compareViewController animated:YES];
}
这里是 CompareViewController 的 .h 代码:
@interface CompareViewController : UIViewController
@property (assign) NSInteger Cid;
- (IBAction)addRemoveCountries:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *addCountries;
从你的代码中我看到你甚至在初始化视图之前就设置了标题。您应该做的是将 属性 添加到您的 CompareViewController
设置中。并在 CompareViewController
的 viewDidLoad:
方法中将此标题设置为您的按钮。
编辑:
在你的 CompareViewController.h 添加一个 属性:
@property (nonatomic, strong) NSString *buttonTitleyouNeedToSet;
在doneButton:
中初始化后添加以下ling compareViewController
compareViewController.buttonTitleyouNeedToSet = [NSString stringWithFormat:@"%ld Countries Selected", countId];
In CompareViewController.m in viewDidLoad:
设置按钮标题:
[addCountries setTitle:buttonTitleyouNeedToSet forState:(UIControlState)UIControlStateNormal];
像这样
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSArray *arr = appDelegate.navController.viewControllers;
for (UIViewController *vc in arr) {
if (vc isKindOfClass:[CompareViewController class]) {
CompareViewController *cvc = (CompareViewController *)vc;
[cvc.btn setTitle:@"Hello" forState:UIControlStateNormal];
break;
}
}
希望这对您有所帮助