我想从第一个控制器传递一些变量:
float user_distance;
UIImage *user;
float user_battery;
NSString *user_name;
到第二个控制器。 两者之间的连接由函数:
完成- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
以这种方式:
UserViewController *secondViewController = [[UserViewController alloc] initWithNibName:@"UserViewController" bundle:nil];
[self.navigationController pushViewController:secondViewController animated:YES];
如何从此功能中恢复值?
提前致谢!
答案 0 :(得分:1)
将您需要的属性添加到.h文件中的第二个控制器..
//SecondController.h
@interface SecondController : UIViewController
@property (nonatomic) float *property1;
@property (nonatomic, strong) UIImage *property2;
@property (nonatomic) float *property3;
@property (nonatomic, strong) NSString *property4;
@end
然后在第一个控制器.m文件中导入第二个控制器.h文件。 并在按下第二个控制器之前设置属性。
//FirstController.m
#import "SecondController.h"
...
...
UserViewController *secondViewController = [[UserViewController alloc] initWithNibName:@"UserViewController" bundle:nil];
secondViewController.property1 = ;//your value here
secondViewController.property2 = ;//your value here
secondViewController.property3 = ;//your value here
secondViewController.property4 = ;//your value here
[self.navigationController pushViewController:secondViewController animated:YES];