簡體   English   中英

使用segue在情節提要中的視圖控制器之間傳遞數據

[英]Passing data between view controllers in storyboards using segue

我是iOS開發的新手,在閱讀了許多有關傳遞變量的教程之后,我仍然需要您的幫助。

我的PP.m文件中的此功能:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {


if ([segue.identifier isEqualToString:@"Btn1Transport"])
{
   [segue.destinationViewController setMyData:(50)];
    NSLog(@"Transporter1");
}

if ([segue.identifier isEqualToString:@"Btn2Transport"])
{
   [segue.destinationViewController setMyData:(100)];
    NSLog(@"Transporter2");
}

}

這是在我的Category.m文件中:

- (void)viewDidLoad{

[super viewDidLoad];

recipeLabel.text = @"Hello"; //for test, is working

}

-(void)setMyData:(int)myData
{

    NSLog(@"Happines %d",myData);
    NSString* result = [NSString stringWithFormat:@"%d", myData];
    recipeLabel.text = result; //not working
}

問題出在行NSLog(@“ Happines%d”,myData); 我的數據可以正常打印,但沒有設置為recipeLabel。 因此,為了測試它是否有效,我制作了recipeLabel.text = @“ Hello”; 標簽也很好。 我究竟做錯了什么? 對於初學者的問題,我們深表歉意。

不,您不能直接從prepareForSegue事件寫入TextLabel,當目標視圖控制器加載時,該值將被覆蓋...

您必須在目標視圖控制器中設置一個變量,然后必須將標簽的值設置為等於目標視圖控制器的viewDidLoad事件中的變量的值,以使其起作用。

//The variable myIntVar is a private variable that should be declared in the header file as private 
- (void)viewDidLoad{

[super viewDidLoad];

recipeLabel.text = myIntVar; //set the value equal to the variable defined in the prepareForSeque
}

-(void)setMyData:(int)myData
{

    NSLog(@"Happines %d",myData);
    NSString* result = [NSString stringWithFormat:@"%d", myData];
    myIntVar = result; 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM