当前视图控制器未显示

时间:2017-10-04 13:10:44

标签: ios objective-c presentviewcontroller

当用户点击分享按钮时,我想将 presentViewController 显示为分享数据到whatsapp或其他一些应用程序。与此同时,我希望在5秒内杀死第二个viewcontrooer。

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

我尝试了以下方式,但它无法正常工作。

我在1st Viewcontroller中实现了 Share 方法,当用户点击第二个ViewController中的分享按钮我在第一个ViewController中调用share方法时仍然没有显示presentViewController

下面的

是1st 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];
}

他们有办法做这个工作吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

我正在使用此代码,

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

单击share按钮时不要调用viewcontroller dismiss方法,那时你将获得presentViewController。实现 ActivityViewContoller 的编译块,当你完成共享数据到whatsapp或gmail等时,将执行编译块。它返回布尔值共享是否成功。在该块中,您应该调用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];
}

我希望它会对你有所帮助。