[英]How to change text of a button when another button is pressed in a different view
In my project I am trying to count the number of rows selected and then when I press the button it goes to the previous view and the button text in that view should change to how many rows are selected. 在我的项目中,我试图计算所选的行数,然后当我按下按钮时,它将转到上一个视图,并且该视图中的按钮文本应更改为选择的行数。 I realize I must be making a simple mistake, but can't figure it out.
我意识到我一定在犯一个简单的错误,但无法弄清楚。 I would appreciate some help with this.
我希望对此有所帮助。
Here is the code I am using in the .m file: 这是我在.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];
}
And here is the .h code for CompareViewController: 这是CompareViewController的.h代码:
@interface CompareViewController : UIViewController
@property (assign) NSInteger Cid;
- (IBAction)addRemoveCountries:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *addCountries;
Form you code I see is you are setting the title even before the view is initialised. 我看到的代码形式是,即使在初始化视图之前,也要设置标题。 What you should do is add a property to your
CompareViewController
set it on the action. 您应该做的是向您的
CompareViewController
添加一个属性,并在操作上对其进行设置。 and in viewDidLoad:
method of CompareViewController
set this title to your button. 在
viewDidLoad:
方法CompareViewController
设置这个标题您的按钮。
EDIT: 编辑:
In your CompareViewController.h add a property: 在您的CompareViewController.h中添加一个属性:
@property (nonatomic, strong) NSString *buttonTitleyouNeedToSet;
In doneButton:
add following ling after you initialise compareViewController
在
doneButton:
在初始化compareViewController
之后添加以下compareViewController
compareViewController.buttonTitleyouNeedToSet = [NSString stringWithFormat:@"%ld Countries Selected", countId];
In CompareViewController.m in viewDidLoad:
set the button title: 在
viewDidLoad:
CompareViewController.m中viewDidLoad:
设置按钮标题:
[addCountries setTitle:buttonTitleyouNeedToSet forState:(UIControlState)UIControlStateNormal];
Do like this 像这样
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;
}
}
Hope this will helpful 希望这会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.