当前视图控制器未显示

Present view controller is not showing

当用户点击共享按钮时,我想显示 presentViewController 以将数据共享到 whatsapp 或其他一些应用程序。同时我想在 5 秒内杀死第二个 viewcontrlooer。

如果用户点击分享按钮或没有点击,第 2 个 ViewController 将在 5 秒后被杀死。如果用户点击共享按钮,我想显示 presentViewController,同时我想杀死第二个 ViewController。怎么可能?

我尝试了下面的方法,但它不起作用。

我在第一个 Viewcontroller 中实现了 Share 方法,当用户在第二个 ViewController 中单击共享按钮时,我在第一个 [=31] 中调用了共享方法=] 仍然没有显示 ViewController

下面是第一篇的分享方法Viewcontroller

    -(void)share:(NSString *)code :(NSString *)brandname :(NSString *)clickid :(NSString *)url{

    NSString *textToShare =[NSString stringWithFormat:@"%@%@%@",@"Install and register ",brandname,@" app  here" ];
    NSString *subText = [NSString stringWithFormat:@"%@%@%@%@%@",@"it's awesome, plus ad me will reward you '",code,@"' on ad me to redeem reward \n\n",@"No ad me? Get ad me \n",@"Link: https://e2by6.app.goo.gl/0RKn" ];
    NSString *longd =[NSString stringWithFormat:@"%@%@",url,clickid];

    //NSURL *long_link=[NSURL URLWithString:longd];

    NSArray *objectsToShare = @[textToShare, longd,  subText];

    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];

    NSArray *excludeActivities = @[UIActivityTypeAirDrop,
                                   UIActivityTypePrint,
                                   UIActivityTypeAssignToContact,
                                   UIActivityTypeSaveToCameraRoll,
                                   UIActivityTypeAddToReadingList,
                                   UIActivityTypePostToFlickr,
                                   UIActivityTypePostToVimeo];

    activityVC.excludedActivityTypes = excludeActivities;

    [self presentViewController:activityVC animated:YES completion:nil];
}

他们有什么办法可以完成这项工作吗?

谢谢。

我正在使用此代码,

[activityVC setCompletionWithItemsHandler:^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
        if (completed) {
            //dismiss your viewcontroller here
        }else{
          //dismiss your viewcontroller here
        }
    }];

点击分享按钮时不要调用viewcontroller dismiss 方法,那时候你会得到presentViewController。为 ActivityViewContoller 实现编译块,当您完成将数据共享到 whatsapp 或 gmail 等时,编译块将执行。它 returns 布尔值共享是否成功。在该块中,您应该调用 viewcontroller dismiss 方法。

您的代码变为,

   -(void)share:(NSString *)code :(NSString *)brandname :(NSString *)clickid :(NSString *)url{

    NSString *textToShare =[NSString stringWithFormat:@"%@%@%@",@"Install and register ",brandname,@" app  here" ];
    NSString *subText = [NSString stringWithFormat:@"%@%@%@%@%@",@"it's awesome, plus ad me will reward you '",code,@"' on ad me to redeem reward \n\n",@"No ad me? Get ad me \n",@"Link: https://e2by6.app.goo.gl/0RKn" ];
    NSString *longd =[NSString stringWithFormat:@"%@%@",url,clickid];

    //NSURL *long_link=[NSURL URLWithString:longd];

    NSArray *objectsToShare = @[textToShare, longd,  subText];

    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];

    NSArray *excludeActivities = @[UIActivityTypeAirDrop,
                                   UIActivityTypePrint,
                                   UIActivityTypeAssignToContact,
                                   UIActivityTypeSaveToCameraRoll,
                                   UIActivityTypeAddToReadingList,
                                   UIActivityTypePostToFlickr,
                                   UIActivityTypePostToVimeo];

    activityVC.excludedActivityTypes = excludeActivities;

[activityVC setCompletionWithItemsHandler:^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
        if (completed) {
            //dismiss your viewcontroller here
        }else{
          //dismiss your viewcontroller here
        }
    }];

    [self presentViewController:activityVC animated:YES completion:nil];
}

希望对你有所帮助