gpt4 book ai didi

ios - 从 AppDelegate 调用的 UIViewController 中的方法不使用现有对象

转载 作者:行者123 更新时间:2023-11-29 03:59:13 24 4
gpt4 key购买 nike

我一直在努力解决这个问题,并且已经尝试了据我所知的所有方法。我希望有人能解决这个问题,让我解释一下我当前的设置。

我正在我的 iOS 应用程序中使用 Bump-api。我有configureBump method在应用程序首次启动时调用的 AppDelegate 中。因此应用程序从一开始就连接到 Bump 服务器。

接下来,我有一个 NavigationController,其中按以下顺序堆叠了以下 UIViewController:

ViewControllerA -> ViewControllerB -> ViewControllerC

VCA (ViewControllerA) 是启动应用程序时的第一个 View 。在 ViewWillLoad 中,我有以下代码:

myAppDelegate = [[UIApplication sharedApplication] delegate];
[myAppDelegate vibrateBump:NO];

vibrateBump 是我的 AppDelegate 中的一种方法,我用它来关闭和打开碰撞。在这种情况下,我希望在 VCA 上将其关闭,因为我不想传输任何内容。

但是,在 VCB 中,我有与 vibrateBump:YES 相同的代码,这就是我希望 Bump 工作的地方。这一切都很好,不用担心我会到达那里。

现在,在 AppDelegate 内的 configureBump 方法中,我将其设置为在碰撞时从 VCB 调用方法。这就是事情变得奇怪的时候。下面是在 VCB 中调用该方法的示例:

@implementation VCB {
FMDatabase * db;
}

- (void)viewDidLoad{
//db is set up here...
}

-(void) otherMethodWithinVC{
//Some action here...
}

-(void) myMethod:(NSArray*)myArray{
[db open] //This does now work

Do some stuff here.... //All this works

[db close] //This does now work
[self otherMethodWithinVC] //This does now work
}

为什么[db open]不起作用?就好像有另一个 myMethod 实例被调用。当我在测试时对其进行更改并决定在 myMethod 中创建 db 时,db 开始工作。

以下是我如何在 configureBump 方法中进行设置:

- (void) configureBump {
VCB * vcb = [[VCB alloc] init];

[[BumpClient sharedClient] setChannelConfirmedBlock:^(BumpChannelID channel) {
//Some action here...
}];

[[BumpClient sharedClient] setDataReceivedBlock:^(BumpChannelID channel, NSData *data) {
dataArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];

if([dataArray count]>0){
if([vcb myMethod:dataArray];
}
}];

//Some action here...

}

有什么想法吗?

最佳答案

当您从 appdelegate 调用 mymethod 时,您正在创建 VCB Controller 的不同实例,但在

myAppDelegate = [[UIApplication sharedApplication] delegate]; 

您正在像单例一样创建 appdelegate 的单个实例

关于ios - 从 AppDelegate 调用的 UIViewController 中的方法不使用现有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16096826/

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