【问题标题】:how to pass data b/w two view controllers? [duplicate]如何在两个视图控制器之间传递数据? [复制]
【发布时间】:2015-10-07 05:33:27
【问题描述】:

我是ios开发新手。

我有两个视图控制器。

-(IBAction)clickToview:(id)sender
{
    [self performSegueWithIdentifier:@"1" sender:self];   
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    if ([segue.identifier isEqualToString:@"1"]) {
        second *destViewController = segue.destinationViewController;

        destViewController.labA.text=textA.text;  // here i want to set values in labA to labB. in second view .
        destViewController.labB.text=textB.text;

        NSLog(@"labA ");
    }
}

-(void)viewDidLoad
{
    textA.text=@"London ";  // this is my text fields 
    textB.text=@"Dreams ";

    [super viewDidLoad];
}

【问题讨论】:

  • 信不信由你,有几个重复的标题完全相同。我无法想象如果您实际使用过 Google,您至少找不到其中一个或网站搜索。
  • 另外,这与 Xcode 无关——这是一个语言/算法/API 问题,如果你使用 makeemacs 编写你的 iOS 并没有什么不同应用程序。
  • @HotLicks 如果问题是“如何在 Interface Builder 中而不是通过编写代码”IMO,这只会与 Xcode 相关。

标签: ios objective-c


【解决方案1】:

在故事板中,您可以将价值一个视图发送到另一个视图,例如 Bellow 方式。你的 Implement 方法做错了。

在您的第二个视图控制器中,您需要使用property 定义NSString 并合成它。

.h 类

@property (nonatomic, strong) NSString *YourString;

.m 类

@synthesize YourString;

现在你可以像这样使用它:-

-(IBAction)youMethod:(id)sender
{
    [self performSegueWithIdentifier:@"secondview" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
            if ([segue.identifier isEqualToString:@"secondview"]) {
         secondviewcontroller *controller = segue.destinationViewController
                controller.YourString = @"myValue";
             }

}

【讨论】:

  • 感谢兄弟的工作
猜你喜欢
  • 2012-07-27
  • 1970-01-01
  • 2017-02-16
  • 1970-01-01
  • 1970-01-01
  • 2021-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多