[英]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
添加一个属性,并在操作上对其进行设置。 在viewDidLoad:
方法CompareViewController
设置这个标题您的按钮。
编辑:
在您的CompareViewController.h中添加一个属性:
@property (nonatomic, strong) NSString *buttonTitleyouNeedToSet;
在doneButton:
在初始化compareViewController
之后添加以下compareViewController
compareViewController.buttonTitleyouNeedToSet = [NSString stringWithFormat:@"%ld Countries Selected", countId];
在viewDidLoad:
CompareViewController.m中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;
}
}
希望这会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.