iOS - passing data to pushed viewcontroller

2020-06-17 16:33发布

问题:

When pushing a viewcontroller, how do I pass data long with it as well (such as "asdf":123)? Are you supposed to addObjects to a NSBundle?

回答1:

MyViewController * myVC = [[MyViewController alloc] initWithNibName:nil bundle:nil];
myVC.someProperty = someValue;    // Pass your data here
[self.navigationController pushViewController:myVC animated:YES];

And in your MyViewController class :

.h

@interface MyViewController : UIViewController
@property (nonatomic, strong) NSString * someProperty;
@end

.m

@implementation MyViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        // your data has been set
        // self.someProperty is equal to "some value"
    }


回答2:

I don't believe you can, but of course you can add relevant properties or methods to your ViewController class and use these prior to pushing it.



回答3:

No. You may be equating NSBundle with Android's Bundle, but they are not related and do not perform similar tasks. Clafou is correct to suggest passing data via methods and properties.