簡體   English   中英

從Objective-C中的類訪問情節提要

[英]Accessing Storyboard from Class in Objective-C

我在另一個文件(而不是VC.h / .m)中有ViewController類的對象。 VC.h連接到Main.storyboard。 我不確定如何從另一個文件中創建的ViewController對象更改情節提要標簽的label.text。

我被告知它與Main.storyboard的nib文件或以下代碼行有關: [UIStoryboard storyboardWithName:(NSString *)Main bundle:(NSBundle *)nil]; 我的故事板文件只是默認的Main.storyboard

我已經像這樣初始化了VC對象的標簽: self.myVCObject.label1 = [UILabel new]並更改了值: self.myVCObject.label1.text = @"StackOverflow" ; 但在運行該應用程序時,IOS Simulator中沒有任何顯示。 問題是由於VC對象未正確鏈接到情節提要嗎? 我該如何解決該問題?

您需要將IBOutlet從情節提要標簽鏈接到View Controller類。 然后,您可以控制標簽。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MyViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"myViewControllerWithLabel"];

vc.label1.text = @"Hello world";

在此處輸入圖片說明

暫無
暫無

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

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