我知道有类似的线程,我在网上搜索了很多,我尝试了很多东西,但我仍然无法使它工作。当用户点击UITableView的附件按钮时,我试图将字符串的值从一个视图控制器传递到另一个视图控制器。转换正常,但在目标视图控制器中,字符串打印为null。我知道这是一个常见的程序..但似乎没有任何作用。我尝试过可变字符串,复制,保留,强大,为目标和源控制器创建实例。如果有人能为我提供解决方案,我真的很感激。
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];
答案 0 :(得分:1)
您正在将字符串设置为viewcontroller并显示另一个。
在你的故事板中你是否将viewcontroller的类设置为“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];
}
我没有更改所有代码,所以如果你粘贴它应该可以工作,但我建议你把你的属性恢复到强和NSString。