簡體   English   中英

數據未在ViewController之間傳遞

[英]Data are not passed between ViewControllers

我需要在兩個視圖控制器之間傳遞數據。這是我的代碼。

對於第一視圖控制器

-(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打印。

我想念什么嗎?

視圖加載后,您將分配值。

將您的分配移動到pushViewController:之前pushViewController:因為這是加載視圖的點)

檢查此代碼,

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

希望這對您有用。

享受編碼:)

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