简体   繁体   English

数据未在ViewController之间传递

[英]Data are not passed between ViewControllers

I need to pass a data between two view controllers.here is my code. 我需要在两个视图控制器之间传递数据。这是我的代码。

for first view controller 对于第一视图控制器

-(void)editBotton {

    Carttable *second=[[Carttable alloc]init];

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:second];
    nav.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;

    [self.navigationController pushViewController:second animated:YES];

    NSString *temp=titlela.text;//titlela is UILabel

    NSLog(@"%@",temp);
    self.cart=second;
    cart.cnftitle=temp;
}

in my cartable view controller.h 在我的可查看视图controller.h中

@property(nonatomic,retain)NSString *cnftitle;

and i synthesized too 我也合成了

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"%@",cnftitle);
}

one NSlog prints my text in label where another prints as NULL.... 一个NSlog在标签中打印我的文本,其中另一个以NULL打印。

am i missing anything? 我想念什么吗?

You're assigning the value after the view has loaded. 视图加载后,您将分配值。

Move your assignment before the pushViewController: (as that's the point where the view is loaded) 将您的分配移动到pushViewController:之前pushViewController:因为这是加载视图的点)

Check this code, 检查此代码,

-(void)editBotton {

    Carttable *second=[[Carttable alloc]init];
    [second.view setAlpha:1.0f];

    second.cnftitle = titlea.text;

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:second];
    nav.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;

    [self.navigationController pushViewController:second animated:YES];
}

Hope this will work for you. 希望这对您有用。

Enjoy Coding :) 享受编码:)

    ****in xode 4.5.1**  storyboard**
      we assume wee want to passed data between  
    viewcontroller1 to viewcontroller2
    in the viewcontroller1 in .h class we import viewcontroller2.h
    then we declare variable in viewcontroller2 ex: Nsstring *data;
    then we can passed data like this..
    in the viewcontroller1 we can code like this(in button event)

        ViewController3 *vc3=[self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];

       vc3.data=textfield1.text;
        vc3.image=imageview.image;
        [self presentViewController:vc3 animated:YES completion:nil];

note=set the storyboard id of viewcontroller2 as ViewController2
        it is on the right hand side utility menu 3rd tab
(select viewcontroller2 and set storyboard id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM