iPhone:在视图之间传递数据

时间:2013-02-05 15:32:27

标签: objective-c ios4

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

- (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以相同的方式声明和合成。能帮我找到错误吗?

2 个答案:

答案 0 :(得分: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];
}

OR

- (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];
}

答案 1 :(得分:0)

如果您想在多个视图之间传递数据,那么您可以选择这些选项。

  • 您可以使用单身人士
  • 使用您的AppDelegate。

这是一个关于单身人士的好教程:

  

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

如果你想使用AppDelegate这样做:

AppDelegate.h

@property (nonatomic, retain) NSString *something;

AppDelegate.m

@synthesize something;

FirstViewController.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;

}