Iphone 如何在不同视图中按下另一个按钮时更改按钮的文本

Iphone 如何在不同视图中按下另一个按钮时更改按钮的文本,iphone,ios8,xcode6.3,Iphone,Ios8,Xcode6.3,在我的项目中,我试图计算选定的行数,然后当我按下按钮时,它会转到上一个视图,该视图中的按钮文本应更改为选定的行数。我意识到我一定是犯了一个简单的错误,但我想不出来。如果能帮上忙,我将不胜感激 以下是我在.m文件中使用的代码: - (IBAction)doneButton:(id)sender { appDelegate = (AppDelegate*) [[UIApplication sharedApplication] delegate]; CountryModel *coun

在我的项目中,我试图计算选定的行数,然后当我按下按钮时,它会转到上一个视图,该视图中的按钮文本应更改为选定的行数。我意识到我一定是犯了一个简单的错误,但我想不出来。如果能帮上忙,我将不胜感激

以下是我在.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
在操作中设置它。在
viewdiload:
methodof
CompareViewController
中,为按钮设置此标题

编辑:

在CompareViewController.h中添加属性:

@property (nonatomic, strong) NSString *buttonTitleyouNeedToSet;
doneButton:
初始化
compareViewController后添加以下命令

compareViewController.buttonTitleyouNeedToSet = [NSString stringWithFormat:@"%ld Countries Selected", countId];
viewDidLoad:
中的CompareViewController.m中设置按钮标题:

[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;
        }
    }

希望这会有所帮助

那么如何将countId的值传递给CompareView控制器?它在didSelectRowAtIndexPath中定义