Ios 通用UIPopoverPresentationController代码

Ios 通用UIPopoverPresentationController代码,ios,objective-c,uipopovercontroller,Ios,Objective C,Uipopovercontroller,我已经创建了一个通用的UIPopoverPresentationController。我将其放入我的主视图控制器类中,该类具有UIPopoverPresentationControllerDelegate协议。以下是如何从主视图控制器调用它的示例: [self presentPopoverOnSide:UIPopoverArrowDirectionDown target:viewArrowShouldPointAt storyboardID:@"Popover" popoverSize:C

我已经创建了一个通用的UIPopoverPresentationController。我将其放入我的主视图控制器类中,该类具有UIPopoverPresentationControllerDelegate协议。以下是如何从主视图控制器调用它的示例:

   [self presentPopoverOnSide:UIPopoverArrowDirectionDown target:viewArrowShouldPointAt storyboardID:@"Popover" popoverSize:CGSizeMake(200, 200)];
这是代码。这一个例行程序有所有你需要提出一个爆米花

- (void)presentPopoverOnSide:(UIPopoverArrowDirection)side target:(UIView *)target storyboardID:(NSString *)storyboardID popoverSize:(CGSize)popoverSize {
   // get the popover view controller
   UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
   UIViewController * controller = [storyboard instantiateViewControllerWithIdentifier:storyboardID];

   // set the popover view controller parameters
   controller.modalPresentationStyle = UIModalPresentationPopover;
   controller.preferredContentSize = popoverSize;
   [self presentViewController:controller animated:YES completion:nil];

   // set the parameters from the popover itself
   UIPopoverPresentationController * popover = [controller popoverPresentationController];
   popover.delegate = self;   // my view controller is the <UIPopoverPresentationControllerDelegate>

   // define where the popover arrow should point
   popover.permittedArrowDirections = side;
   popover.sourceView = target;
   popover.sourceRect = CGRectMake(0, 0, target.frame.size.width, target.frame.size.height);
}

#pragma Mark ----------------- <UIPopoverPresentationControllerDelegate> methods -------------------

- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popoverPresentationController {
   NSLog(@"prepareForPopoverPresentation");
}

- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
   // only called when touchup outside the popover
   NSLog(@"popoverPresentationControllerShouldDismissPopover");
   return YES;
}

- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
   // only called when touchup outside the popover
   NSLog(@"popoverPresentationControllerDidDismissPopover");
}

- (void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing  _Nonnull *)view {
   NSLog(@"willRepositionPopoverToRect: %@", NSStringFromCGRect(*rect));
}
-(void)presentPopoverOnSide:(UIPopoverArrowDirection)侧目标:(UIView*)目标故事板ID:(NSString*)故事板ID popoverSize:(CGSize)popoverSize{
//获取popover视图控制器
UIStoryboard*情节提要=[UIStoryboard情节提要,名称:@“Main”bundle:nil];
UIViewController*控制器=[storyboard InstanceEviewController标识符:storyboard ID];
//设置popover视图控制器参数
controller.modalPresentationStyle=UIModalPresentationPopover;
controller.preferredContentSize=popoverSize;
[自我呈现视图控制器:控制器已设置动画:是完成:无];
//从popover本身设置参数
UIPopoverPresentationController*popover=[控制器popoverPresentationController];
popover.delegate=self;//我的视图控制器是
//定义弹出箭头应指向的位置
popover.permittedArrowDirections=侧面;
popover.sourceView=target;
popover.sourceRect=CGRectMake(0,0,target.frame.size.width,target.frame.size.height);
}
#pragma标记------------方法-------------------
-(void)prepareforpoverpresentation:(UIPopoverPresentationController*)popoverPresentationController{
NSLog(“PrepareforpoverPresentation”);
}
-(BOOL)popoverPresentationController应添加SmissPopOver:(UIPopoverPresentationController*)popoverPresentationController{
//只有在popover外进行润色时才呼叫
NSLog(@“PopOverpresentationControllersouldDismissPopOver”);
返回YES;
}
-(void)PopOverpresentationControllerIDDismissPopover:(UIPopoverPresentationController*)popoverPresentationController{
//只有在popover外进行润色时才呼叫
NSLog(@“PopOverpresentationControllerdDismissPopOver”);
}
-(void)popoverPresentationController:(UIPopoverPresentationController*)popoverPresentationController将重新定位PopOverpresentRect:(inout cRect*)rect inView:(inout UIView*u自动删除_Nonnull*)视图{
NSLog(@“WillPopOverorect:%@”,NSStringFromCGRect(*rect));
}

Swift版本更具体:

func showPathPopover(button: UIButton) {

  let popController = self.storyboard?.instantiateViewController(withIdentifier: "PathPopover") as! PathPopover

  popController.modalPresentationStyle = UIModalPresentationStyle.popover
  popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.any
  popController.popoverPresentationController?.delegate = self
  popController.popoverPresentationController?.sourceView = button
  popController.popoverPresentationController?.sourceRect = button.bounds

  // present the popover
  self.present(popController, animated: true, completion: nil)

}

你的问题是什么?