gpt4 book ai didi

ios - 通过表格附件按钮将字符串值传递给不同的 View Controller

转载 作者:行者123 更新时间:2023-11-29 12:39:59 26 4
gpt4 key购买 nike

我知道有类似的线程,我在网上搜索了很多,也尝试了很多东西,但我仍然无法让它工作。当用户单击 UITableView 的附件按钮时,我试图将字符串的值从一个 View Controller 传递到另一个 View Controller 。转换工作正常,但在目标 View Controller 中,字符串打印为空。我知道这是一个常见的程序..但似乎没有任何效果。我尝试了可变字符串、复制、保留、强壮、为目标和源 Controller 创建实例。如果有人能为我提供解决方案,我将不胜感激。

SourceViewController.h

@property (nonatomic,strong) NSString*reference; // I have tried retain, copy, nsmutablestring also

SourceViewController.m

#import "DestinationViewController.h"


- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {

self.reference = [[NSString alloc] initWithString:[allreferences objectAtIndex:[indexPath row] - 1]];

NSLog(@"%@", self.reference); // it prints the value correctly

DestinationViewController * destinationViewController = [[DestinationViewController alloc] init];

destinationViewController.reference = [NSMutableString stringWithString:self.reference];


NSString * storyboardName = @"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"destination"];
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:vc animated:YES completion:nil];


}

DestinationViewController.h

@property (nonatomic, retain) NSMutableString * reference; // i have tried strong, copy, nsstring also

DestinationViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

NSLog(@"%@", self.reference); // it prints null

self.sourcevalue.text = self.reference; // assigning the value to a label


}

回答:

终于解决了。问题是我的过渡。

我改变了:

DestinationViewController * destinationViewController = [[DestinationViewController alloc] init];

到:

DestinationViewController * destinationViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"destination"];

还将过渡更改为:

 [self presentViewController:destinationViewController animated:YES completion:nil];

最佳答案

您正在将字符串设置到一个 View Controller 中并呈现另一个 View Controller 。

在您的 Storyboard中,您是否将 View Controller 的类设置为“DestinationViewController”?

尝试这样的事情:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {

self.reference = [[NSString alloc] initWithString:[allreferences objectAtIndex:[indexPath row] - 1]];

NSLog(@"%@", self.reference); // it prints the value correctly

/* This is wrong!
DestinationViewController * destinationViewController = [[DestinationViewController alloc] init];

destinationViewController.reference = [NSMutableString stringWithString:self.reference];
*/
NSString * storyboardName = @"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"destination"];
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
// Cast vc to the real class
DestinationViewController * destinationViewController = (DestinationViewController*)vc;
destinationViewController.reference = [NSMutableString stringWithString:self.reference];

[self presentViewController:vc animated:YES completion:nil];
}

我没有更改所有代码,所以如果你粘贴它应该可以工作,但我建议你将你的属性放回 strong 和 NSString。

关于ios - 通过表格附件按钮将字符串值传递给不同的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25233073/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com