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

时间:2015-04-28 05:09:16

标签: iphone ios8 xcode6.3

在我的项目中,我试图计算所选的行数,然后当我按下按钮时,它会转到上一个视图,该视图中的按钮文本应该更改为选择了多少行。我意识到我必须犯一个简单的错误,但无法弄清楚。我将不胜感激。

以下是我在.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;

2 个答案:

答案 0 :(得分:1)

表单代码我看到你是在初始化视图之前设置标题。您应该做的是为CompareViewController添加一个属性,将其设置为操作。并在viewDidLoad: 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];

答案 1 :(得分:0)

这样做

finally block

希望这会有所帮助