2012-07-25 4 views
0

두 개의 뷰 컨트롤러간에 데이터를 전달해야합니다. 내 코드가 있습니다.ViewControllers간에 데이터가 전달되지 않습니다

제 뷰 컨트롤러

내 cartable보기 controller.h

@property(nonatomic,retain)NSString *cnftitle; 

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

난 합성도

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

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

하나 NSlog은 라벨을 다른 프린트 내 텍스트를 인쇄 NULL로 ...

내가 빠진 것이 있습니까?

+0

뷰가로드되고 먼저 스택에 뷰 컨트롤러를 누르면 값이 바로 인쇄됩니다. 그 후에 값을 설정하고 있습니다. 문제는 NSLog가있는 곳입니다. 뷰 컨트롤러를 스택에 푸시하기 전에 문자열 값을 설정하면 올바르게 작동합니다. – Dima

답변

4

보기를로드 한 후에 값을 할당합니다.

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

희망의 pushViewController: 전에 할당을 이동

.

는 코딩 즐길 :)

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) 
관련 문제