iOS - passing data to pushed viewcontroller

2020-06-17 16:29发布

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?

3条回答
闹够了就滚
2楼-- · 2020-06-17 16:43
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"
    }
查看更多
劫难
3楼-- · 2020-06-17 17:00

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.

查看更多
够拽才男人
4楼-- · 2020-06-17 17:06

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.

查看更多
登录 后发表回答