簡體   English   中英

創建運行時已知的類的實例

[英]Create an instance of a class known at runtime

我有此代碼,但它返回“ nil”而不是新類。 在這里它沒有用,但是在我的程序中卻很有意義。

    Class myClass = [SettingsTableViewController class];  

    UIViewController *targetViewController = [[myClass alloc] initWithNibName:nil bundle:nil];
    [[self navigationController] pushViewController:targetViewController animated:YES];

您正在忘記某些東西,或者您可能不知道。 這是正確的代碼。

Class myClass = NSCLassFromString(@"yourClassName");

if(myClass){
   myClass = [[yourclassName alloc] init];
}

暫無
暫無

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

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