Ios 信息未在viewDidLoad上发送到Apple Watch

Ios 信息未在viewDidLoad上发送到Apple Watch,ios,objective-c,watchos,Ios,Objective C,Watchos,我有一个iOS应用程序,它有一个watchOS扩展,这两个都是在Objective-C中构建的。该应用程序需要在加载(viewDidLoad)或出现(ViewDidDisplay:(BOOL)动画时通过阵列发送。当应用程序加载时,不会发生任何事情,但如果我以某种方式向应用程序添加文件(通过内置加法器或从桌面或AirDrop导入文件),watch应用程序将更新。按下刷新按钮不会使应用程序工作。然而,一旦我添加了一个文件,它就会很好地更新,并开始完美地更新。我不知道为什么会发生这种情况,但下面是我使

我有一个iOS应用程序,它有一个watchOS扩展,这两个都是在Objective-C中构建的。该应用程序需要在加载(
viewDidLoad
)或出现(
ViewDidDisplay:(BOOL)动画时通过阵列发送。当应用程序加载时,不会发生任何事情,但如果我以某种方式向应用程序添加文件(通过内置加法器或从桌面或AirDrop导入文件),watch应用程序将更新。按下刷新按钮不会使应用程序工作。然而,一旦我添加了一个文件,它就会很好地更新,并开始完美地更新。我不知道为什么会发生这种情况,但下面是我使用的一些代码。我删掉了一些不太有用的代码

viewDidLoad
中:

- (void)viewDidLoad
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appFolderPath = [path objectAtIndex:0];
NSString *inboxAppFolderPath = [appFolderPath stringByAppendingString:@"/Inbox"]; //changes the directory address to make it for the inbox

//move all files from inbox to documents root
//get to inbox directory
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSArray *inboxContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSString stringWithFormat:inboxAppFolderPath,documentsDirectory] error:nil];

//move all the files over
for (int i = 0; i != [inboxContents count]; i++)
{
    NSString *oldPath = [NSString stringWithFormat:@"%@/%@", inboxAppFolderPath, [inboxContents objectAtIndex:i]];
    //NSLog(oldPath);
    NSString *newPath = [NSString stringWithFormat:@"%@/%@", appFolderPath, [inboxContents objectAtIndex:i]];
    //NSLog(newPath);
    NSError* error;
    [[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:newPath error:&error];
    NSLog(@"error: %@", error.localizedDescription);
}
//end of moving all files



//Turn every file inside the directory into an array
//add directory in case it doesn't already exist
if (![[NSFileManager defaultManager] fileExistsAtPath: inboxAppFolderPath])
{
    [[NSFileManager defaultManager] createDirectoryAtPath: inboxAppFolderPath withIntermediateDirectories:NO attributes:nil error:nil];
    NSLog(@"directory created");
}



//a bunch of NSPredicates go here to filter out the array


[self sendStuffToWatch];

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[self connectToWatch];
}
-(void)connectToWatch
中,有

-(void)connectToWatch
{
//set up WatchConnectivity session
if([WCSession isSupported])
{
    self.watchSession = [WCSession defaultSession];
    self.watchSession.delegate = self;
    [self.watchSession activateSession];


    if([WCSession defaultSession].paired){
        NSLog(@"Watch session active");
    }
    else
    {
        NSLog(@"WATCH NOT CONNECTED");
    }
}
}
-(void)sendsuffttowatch中

-(void)sendStuffToWatch
{
if(self.watchSession)
{
    NSError *error;
    NSDictionary *applicationDict = @{@"array":recipes};
    [self.watchSession updateApplicationContext:applicationDict error:&error];
    NSLog(@"sent info to watch");
}
}
-(void)viewdide出现:(BOOL)animated
中,只有以下内容

-(void)viewDidAppear:(BOOL)animated
{
[self sendStuffToWatch];
}
刷新按钮的代码为

-(IBAction)Refresh:(id)sender
{
[recipeTable reloadData];
[self sendStuffToWatch];
[self viewDidLoad];
}

你的方法都有问题。首先要调用super,然后设置连接,过滤阵列,最后将数据发送到手表

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[self connectToWatch];

// rest of code here to create recipe array

[self sendStuffToWatch];
另外,您不应该在自己的代码中调用
viewDidLoad
。当视图控制器首次加载到内存中时,应该只为您调用一次,而不是由您调用


如果您有要重新运行的代码,则将其从
viewDidLoad
移到自己的方法中,然后只调用该方法。

此外,如果您要强制加载
视图并调用
viewDidLoad
,则可以在需要时调用
loadViewIfNeeded
。不幸的是,它不起作用,但是对于在刷新按钮中调用
viewDidLoad
,我需要重新加载所有内容,我是否应该从
viewDidLoad
获取/everything/from并将其全部粘贴到他们自己的方法中?是(除了调用super之外的所有内容)。至于它不工作,您应该能够使用调试器,逐步检查代码,并确保没有错误发生。由于它最初不起作用,但在添加文件时起作用,因此在第一次运行时,有些东西没有设置或准备好。你只需要找出是哪一部分导致了问题。