0

我有一个带有 watchOS 扩展的 iOS 应用程序,它们都是用 Objective-C 构建的。viewDidLoad应用程序在加载 ( ) 或出现 ( )时需要发送一个数组viewDidAppear:(BOOL)animated。当应用程序加载时,什么也没有发生,但如果我以某种方式将文件添加到应用程序(通过内置加法器或从我的桌面或 AirDrop 导入文件),手表应用程序会更新。按刷新按钮不会使应用程序工作。但是,一旦我添加了一个文件,它就会很好地更新并开始完美更新。我不确定为什么会发生这种情况,但这是我使用的一些代码。我删掉了一些不太有用的代码。

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)sendStuffToWatch

-(void)sendStuffToWatch
{
if(self.watchSession)
{
    NSError *error;
    NSDictionary *applicationDict = @{@"array":recipes};
    [self.watchSession updateApplicationContext:applicationDict error:&error];
    NSLog(@"sent info to watch");
}
}

-(void)viewDidAppear:(BOOL)animated,只有这个

-(void)viewDidAppear:(BOOL)animated
{
[self sendStuffToWatch];
}

刷新按钮的代码是

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

1 回答 1

0

你的方法都乱套了。你想先调用 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到自己的方法中,然后调用该方法。

于 2016-03-28T04:51:58.650 回答