【问题标题】:Send string to destinationViewController发送字符串到destinationViewController
【发布时间】:2013-12-11 18:29:56
【问题描述】:

我在所谓的应用程序中有 2 个屏幕。第一个(ViewController.m)获取值并将其发送到 ViewController2.m 中的方法。然后,第二个视图中标签的预加载文本更改为传输的字符串。

案例是:

1) NSLog 打印从第一页的文本字段中获取的正确输出

2) 但是,setText 方法不会改变标签的文本。

3) 奇怪的是,相同的代码可以更改 viewDidLoad 中标签的文本。

- (void) setText:(NSString *)paramText {
    self.myLabel.text = paramText;
    NSLog(@"%@", paramText);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.myLabel.text = @"try";

}

我如何从 ViewController.m 调用 setText:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"ikinciEkran"]) {

        ViewController2 *nextController = segue.destinationViewController;

        [nextController setText:self.myText.text];

    }

}

【问题讨论】:

  • 你是在主线程调用setText方法吗?
  • 看起来你做的一切都是正确的。添加一些NSLogs 以查看您到达prepareForSegue:,并输入if
  • 我做了并且看起来它进入了 if 但我仍然得到标签的相同结果。
  • 我无法在 setText: 方法中将标签文本更改为静态文本。

标签: ios objective-c uilabel


【解决方案1】:

你的二传手,你需要:

_text = paramText;

【讨论】:

    【解决方案2】:

    我不知道我是否理解正确。但是如果您在适合您的视图控制器之间传递字符串。例如,将 VC1 文本字段值转换为字符串

    if([segue.identifier isEqualToString:@"ikinciEkran"]) {
    NSString *textLocalString = [NSString stringWithFormat:@"%@",Viewcontroller1.text];
    }
    ViewController2 *nextController = segue.destinationViewController;
           nextController.introString = textLocalString;
    

    然后,在 Viewcontroller2 中声明 Introstring

    @property(weak,nonatomic) NSString *introString;
    

    在ViewController2.m中,放入NSlog和Check

    NSLOG(@"TransferString:%@",Introstring);
    

    如果您在 vewcontrller 之间传输 nstring 值,这将起作用。

    【讨论】:

    • 它可以很好地传递文本,但是 self.myLabel.text = paramText; 有问题在 setText 方法中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多