我实现了UINavigationController
,根视图控制器隐藏或显示内容,具体取决于用户在导航堆栈中做出的选择。所有隐藏/显示选项都在根视图控制器文件中的viewWillAppear
函数中实现。现在,当根视图控制器首次出现时,没有问题。但是,当我更深入,更改一些内容然后按几次后退按钮并进入我的根视图控制器时,ViewWillAppear函数不会立即调用,但是会在延迟之后调用。
结果如何?我之前留下的视图首先被加载,然后逐渐一些按钮开始消失。但是,我希望这些按钮在视图显示之前消失。为此,我需要在视图实际显示之前执行viewWillAppear
,就像应用首次出现一样。
我意识到有些开发人员遇到viewWillAppear
根本没有被调用的问题;但是,在这种情况下,这不是问题:)
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc]
initWithRegionType:AWSRegionUSEast1
identityPoolId:@"###-####-###"];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;
AWSDynamoDBObjectMapper *dynamoDBObjectMapper = [AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper];
AWSDynamoDBQueryExpression *queryExpression = [AWSDynamoDBQueryExpression new];
if ([FBSDKAccessToken currentAccessToken]) {
queryExpression.indexName = @"FBID-index";
queryExpression.hashKeyAttribute = @"FBID";
queryExpression.hashKeyValues = [FBSDKAccessToken currentAccessToken].userID;
}
else{
queryExpression.hashKeyAttribute = @"ID";
queryExpression.hashKeyValues = credentialsProvider.identityId;
}
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
[[dynamoDBObjectMapper query:[User class]
expression:queryExpression]
continueWithBlock:^id(AWSTask *task) {
if (task.error) {
NSLog(@"The request failed. Error: [%@]", task.error);
}
if (task.exception) {
NSLog(@"The request failed. Exception: [%@]", task.exception);
}
if (task.result) {
AWSDynamoDBPaginatedOutput *paginatedOutput = task.result;
//there should really only be one entry with the specified CognitoID
User *user = [paginatedOutput.items objectAtIndex:0];
if(user.Login==true||user.Login==1){
if ([FBSDKAccessToken currentAccessToken]) {
login.hidden = YES;
}
else{
FBlogin.hidden = YES;
[login setTitle: @"Log Out" forState: UIControlStateNormal];
}
}
else{
FBlogin.hidden = YES;
}
dispatch_semaphore_signal(sema);
}
return nil;
}];
while (dispatch_semaphore_wait(sema, DISPATCH_TIME_NOW)) { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:1]]; }
}