几天前我写了一个使用presentViewController加载视图控制器的方法:
-(void)passaGC:(NSString *)user { UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UIViewController *viewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:@"generaC"]; [self presentViewController:viewController animated:YES completion:nil]; }
但今天我需要将变量用户从此方法传递给加载的viewController.
我发现了堆栈溢出的其他问题,但没有什么与我的请求非常相似
解决方法
将属性添加到目标viewController(在.h中):
@property (strong,nonatomic) NSString *user;
最后你的方法看起来像
-(void)passaGC:(NSString *)user { UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UIViewController *viewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:@"generaC"]; viewController.user = user; [self presentViewController:viewController animated:YES completion:nil]; }