0

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

对于第一个视图控制器

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

在我的 cartable 视图 controller.h

@property(nonatomic,retain)NSString *cnftitle;

我也合成了

- (void)viewDidLoad
{
    [super viewDidLoad];

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

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

我错过了什么吗?

4

3 回答 3

4

您在视图加载后分配值。

将您的作业移到之前pushViewController:(因为那是加载视图的点)

于 2012-07-25T19:23:36.360 回答
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];
}

希望这对你有用。

享受编码:)

于 2012-07-25T19:43:06.967 回答
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)
于 2012-12-12T07:04:52.507 回答