如何通过segue传递标签?

时间:2014-01-05 00:29:05

标签: ios iphone objective-c uilabel segue

我正在构建一个iOS实用程序。这是一个anagram游戏,跟踪用户猜测,猜测正确和错误猜测的次数。我会跟踪猜测(正确)的次数,并希望在侧面视图控制器上显示数据。但是,使用以下代码时,segue发生时不会发生任何变化。我做错了什么?

这是我的AnagramsMainViewController.m

#import "AnagramsMainViewController.h"


@interface AnagramsMainViewController ()

@end

@implementation AnagramsMainViewController

@synthesize jumbledWord, jumbledWordLabel, unjumbledWord, unjumbledWordLabel, textField, statusLabel, timesGuessed, timesGuessedCorrect, timesGuessedIncorrect;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Flipside View

- (void)flipsideViewControllerDidFinish:(AnagramsFlipsideViewController *)controller
{
    [self dismissViewControllerAnimated:YES completion:nil];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showAlternate"]) {
         AnagramsFlipsideViewController *controller = [segue destinationViewController];
        controller.timesGuessedCorrectLabel.text = [NSString stringWithFormat:@"Times guessed correct: %d", timesGuessedCorrect];
        controller.timesGuessedIncorrectLabel.text = [NSString stringWithFormat:@"Times guessed incorrect: %d", timesGuessedIncorrect];
        controller.totalTimesGuessedLabel.text = [NSString stringWithFormat:@"Total times guessed: %d", timesGuessed];
        [[segue destinationViewController] setDelegate:self];
    }
}

这是我的AnagramsFlipsideViewController.h

#import <UIKit/UIKit.h>

@class AnagramsFlipsideViewController;

@protocol AnagramsFlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(AnagramsFlipsideViewController *)controller;
@end

@interface AnagramsFlipsideViewController : UIViewController

@property (weak, nonatomic) id <AnagramsFlipsideViewControllerDelegate> delegate;
@property (strong, nonatomic) IBOutlet UILabel *totalTimesGuessedLabel;
@property (strong, nonatomic) IBOutlet UILabel *timesGuessedCorrectLabel;
@property (strong, nonatomic) IBOutlet UILabel *timesGuessedIncorrectLabel;


- (IBAction)done:(id)sender;

@end

(我在AnagramsFlipsideViewController.m

中合成了这些标签

1 个答案:

答案 0 :(得分:1)

你做错了是试图在另一个视图控制器中设置视图的内容。不要那样做。

它违反了另一个视图控制器的封装,所以从面向对象的设计角度来看这是一个坏主意,它有时也不起作用(就像在这种情况下一样。)

相反,为您需要传递的值创建NSString或NSInteger属性(例如,在prepareForSegue方法中为totalTimesGuessed,timesGuessedCorrect,timesGuessedInCorrect,并设置 THOSE

然后在AnagramsFlipsideViewController的viewWillAppear方法中,获取这些字符串值并将其安装在标签中。