gpt4 book ai didi

iphone - 在另一个 ViewController 中识别一个 ViewController 的实例

转载 作者:行者123 更新时间:2023-11-29 11:15:14 25 4
gpt4 key购买 nike

在我的应用程序中,我有两个 View Controller - 一个 UIViewController 和一个 UITableViewController。现在,在我的 TableViewController 实现中,我需要从 UIViewController(一个属性 - 数组)中检索一些数据。它是合成的数组,所以我实际做的是在 UITableViewController 的实现中,我正在创建我的 UIViewController 的一个实例,一个数组,并将该数组设置为 [myUIViewController getTheArray]。我知道它不会这样工作,它只是检索 nil,但在我的 VC 中它实际上充满了数据。我被困在这一点上,我本可以尝试使用 performSegueWithIdentifier 方法,但这些 View 并未通过 segue 直接相互连接。它们嵌入在选项卡栏 View 中,而我的 ViewController 与我的 TableViewController 完全无关。关于如何使其正常工作的任何想法?谢谢。

@interface classA : UIViewController

@property (strong, nonatomic) Playlist* playlist;

-(Playlist*) getTheCurrentPlaylist;
@end

@implementation classA

@synthesize playlist = _playlist;
// add data to _playlist
-(Playlist*) getTheCurrentPlaylist{
return _playlist;
}
@end

现在,去B类

@interface classB : UITableViewController
@property (strong ,nonatomic) Playlist* playlistTab;
@end

@implementation classB
@synthesize playlistTab = _playlistTab;

-(void) viewDidLoad{
[super viewDidLoad];
classA *cla = [[classA alloc] init];
// what has to be done here?
_playlistTab = [cla getTheCurrentPlaylist];
}

我希望这能说明问题。

最佳答案

我不是 100% 确定我理解您的意思,所以如果我误解了什么,请发表评论。另外,我自己对 iOS 开发还很陌生。

好的,现在您有了一个带有 UIViewController 和 UITableViewController 的标签栏 Controller 。您的 UIViewController 实现有一些数据,您的 UITableViewController 需要访问该数据。您不能只创建一个新的 classA。您需要引用您创建并放入标签栏的classA 的实例。

你可以做的一件事是让 AppDelegate 知道 UIViewController(即 AppDelegate 有一个引用 UIViewController 的属性。然后在你的 UITableViewController 中,你可以做这样的事情来访问 UIViewController:

MyAppDelegate *appDel = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.myUIViewController.playlist;

听起来像是一个真正简单的应用程序,所以使用像 Core Data 这样的东西可能会过于复杂。您可以在上面执行的另一种方法是让您的 ClassA 实例设置一些值 singleton然后让 ClassB 从中读取。或者只是简单地创建一个 AppDelegate 的属性,该属性由 UIViewController 写入并从您的 TableView 中读取。

关于iphone - 在另一个 ViewController 中识别一个 ViewController 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9745052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com