ViewControllers之间不传递数据

时间:2012-07-25 19:20:54

标签: iphone objective-c ios uiviewcontroller

我需要在两个视图控制器之间传递数据。这是我的代码。

用于第一个视图控制器

-(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;
}

在我的购物车视图controller.h中

@property(nonatomic,retain)NSString *cnftitle;

我合成了

- (void)viewDidLoad
{
    [super viewDidLoad];

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

一个NSlog在标签中打印我的文本,其中另一个打印为NULL ....

我错过了什么吗?

3 个答案:

答案 0 :(得分:4)

您在视图加载后分配值。

pushViewController:之前移动您的作业(因为这是加载视图的位置)

答案 1 :(得分:1)

检查此代码,

-(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];
}

希望这对你有用。

享受编码:)

答案 2 :(得分:1)

    ****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)