[英]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.