0

我正在尝试创建一个简单的应用程序来了解如何在视图之间传递数据。我有两种看法。第一个使用此方法将一些字符串传递给第二个视图:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"colorSegue"]) {

    [segue.destinationViewController setFirstColor:self.favoriteColorTextField.text];
    [segue.destinationViewController setSecondColor:self.secondColor];
    [segue.destinationViewController setDelegate:self];
}

第二个视图使用此方法接收数据。

- (void)viewWillAppear:(BOOL)animated
{
 [super viewWillAppear:animated];
 if (self.firstColor != nil && ![self.firstColor isEqualToString:@""]) {
    self.favoriteColorLabel.text = [[NSString alloc] initWithFormat:@"Favorite color: %@", self.firstColor];
 }
 if (self.secondColor != nil && ![self.secondColor isEqualToString:@""]){
    self.secondFavoriteColorLabel.text = [[NSString alloc] initWithFormat:@"Second favorite color: %@", self.secondColor];
 }
}

到目前为止一切正常,第二个视图可视化了第一个字符串(但不是第二个,因为它仍然是空的)。然后第二个视图通过委托将一个字符串传回给第一个:

- (void)viewWillDisappear:(BOOL)animated
{
[self.delegate setSecondFavoriteColor:self.secondFavoriteColorTextField.text];
}

第一个视图将字符串可视化,如下所示:

- (void)viewWillAppear:(BOOL)animated
{
 [super viewWillAppear:animated];
 if (self.secondColor != nil && ![self.secondColor isEqualToString:@""]) {
    self.secondFavoriteColorLabel.text = [[NSString alloc] initWithFormat:@"Second favorite color: %@", self.secondColor];
 }
}

在这里一切正常,第一个视图可视化了两个字符串。但现在我遇到了问题。如果我尝试再次将字符串传递给第二个视图,则只有第一个被正确传递。第二个视图总是接收带有 nil 值的 secondColor,即使它的值已经通过委托设置。变量 firstColor 和 secondColor 的声明和合成方式完全相同。你能帮我找出错误吗?

4

2 回答 2

0

我认为您需要将 segue.destinationViewController 转换为 SecondViewController 类

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"colorSegue"]) {
        SecondViewController *svc = [segue destinationViewController];
        [svc setFirstColor:self.favoriteColorTextField.text];
        [svc setSecondColor:self.secondColor];
        [svc setDelegate:self];
}

或者

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"colorSegue"]) {

        [(SecondViewController *)segue.destinationViewController setFirstColor:self.favoriteColorTextField.text];
        [(SecondViewController *)segue.destinationViewController setSecondColor:self.secondColor];
        [(SecondViewController *)segue.destinationViewController setDelegate:self];
}
于 2013-02-05T15:38:24.190 回答
0

如果你想在超过 1 个视图之间传递数据,那么你有这些选项。

  • 您可以使用单例
  • 使用您的 AppDelegate。

这是一个关于单例的好教程:

http://www.galloway.me.uk/tutorials/singleton-classes/

如果您想使用 AppDelegate 这样做:

AppDelegate.h

@property (nonatomic, retain) NSString *something;

AppDelegate.m

@synthesize something;

第一视图控制器.m

#import "AppDelegate.h";

(void) setSomething {
       AppDelegate *AppDelegate = [[UIApplication sharedApplication] delegate];
       AppDelegate.something = @"something";

}

SecondviewController.m

#import "AppDelegate.h";

(void) getSomething {
       AppDelegate *AppDelegate = [[UIApplication sharedApplication] delegate];
       label.text = AppDelegate.something;

}
于 2013-02-05T16:06:11.683 回答