2013-02-05 100 views
0

我试图创建一个简单的应用程序,以了解如何在视图之间传递数据。我有两个意见。第一个使用此方法将一些字符串传递到第二个视图:iPhone:在视图之间传递数据

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

+0

什么方法 - (无效)setSecondFavoriteColor:在所述第一视图(的NSString *)色彩控制器看起来像? – jmstone617

+0

你是如何定义'secondColor'属性的。 self.secondColor在视图中的日志会出现,并setSecondFavoriteColor? –

+0

setSecondFavoriteColor方法:(NSString *)secondFavoriteColor只是简单的“self.secondColor = secondFavoriteColor;”。 secondColor属性在头文件中声明为“@property(weak,nonatomic)NSString * secondColor;”。我记录secondColor,并在第一个视图控制器它包含正确的字符串都在viewWillAppear和setSecondFavoriteColor,但在viewWillAppear里面的第二个视图控制器总是等于(空)。 – Soel

回答

0

我想你需要trasform 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]; 
} 
+0

不幸的是,它没有奏效。不管怎么说,还是要谢谢你。 – Soel

+0

嗯..对不起,我不知道是什么问题。 –

+0

你可以测试[segue.destination respondToSelector:@SEL]吗?我认为你应该测试[segue.destination responseToSelector:@selector(setFirstColor :)]等等。 –

0

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

  • 可以使用单
  • 用你的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; 

} 
相关问题