Menu

Update to the UIActionSheet component

Graham
2015-01-21
2015-01-23
  • Graham

    Graham - 2015-01-21

    Hi guys,

    I've added a method to the UIAlert view so that it can use it's "Popover" feature to be displayed as a popup for a button for example (i.e. with an arrow pointing to the "popup" component)

    (only available on iPads)

    Simply call the overloaded "Execute" method with a "popoverControl" parameter to use the new feature.

    This shouldn't break any existing code but who knows :-)

    Here are the changes...

      TDPFUIActionSheet = class( TComponent )
      private
        ..
        FPopoverControl: TControl;
      public
        ..
        function Execute: TModalResult; overload;
        function Execute(APopoverControl: TControl): TModalResult; overload;
    

    Then I've made the following changes to the implementation...

    function TDPFUIActionSheet.Execute: TModalResult;
    begin
      Result := Execute(nil);
    end;
    
    function TDPFUIActionSheet.Execute(APopoverControl: TControl): TModalResult;
    {$IFDEF IOS}
    var
      AlertView: UIAlertView;
      RunLoop: NSRunLoop;
      DistantFuture: NSDate;
      DefaultRunLoopMode: NSString;
    {$ENDIF}
    begin
      FPopoverControl := APopoverControl;
      {$IFDEF IOS}
      FDPFActionSheetDelegate.FModal := True;
      ShowMessage;
    
      RunLoop := TNSRunLoop.Wrap(TNSRunLoop.OCClass.currentRunLoop);
      DistantFuture := TNSDate.Wrap(TNSDate.OCClass.distantFuture);
      DefaultRunLoopMode := NSDefaultRunLoopMode;
      while FDPFActionSheetDelegate.Modal and RunLoop.runMode(DefaultRunLoopMode, DistantFuture) do;
    
      Result := FDPFActionSheetDelegate.ModalResult;
      {$ENDIF}
    end;
    
    procedure TDPFUIActionSheet.ShowMessage;
    {$IFDEF IOS}
    var
      I, idx: Integer;
      p: Pointer;
      LView: UIView;
      ARect: NSRect;
    {$ENDIF}
    begin
    {$IFDEF IOS}
      // this function is not re-entry safe - have to kill the previous message
      if FMessageView <> nil then
      begin
        FMessageView.dismissWithClickedButtonIndex(FMessageView.cancelButtonIndex, False);
        FMessageView := nil;
      end;
    
      FMessageView := TUIActionSheet.Wrap( TUIActionSheet.Alloc.initWithTitle( NSStr( '' ), nil, nil, nil, nil ) );
      FMessageView.setDelegate( FDPFActionSheetDelegate.GetObjectID );
      FMessageView.setActionSheetStyle( UIActionSheetStyleDefault );
    
      for I := 0 to Buttons.Count - 1 do
      begin
        idx := FMessageView.addButtonWithTitle( NSStr( Buttons[I].Title ) );
        if Buttons[I].ButtonKind = bkCancel then
          FMessageView.setCancelButtonIndex( idx )
        else if Buttons[I].ButtonKind = bkDestructive then
          FMessageView.setDestructiveButtonIndex( idx );
    
      end;
    
      if Buttons.Count = 0 then
      begin
        FUITapGestureRecognizer.setNumberOfTapsRequired( 1 );
        FUITapGestureRecognizer.setNumberOfTouchesRequired( 1 );
        FUITapGestureRecognizer.setDelaysTouchesBegan( true );
        FUITapGestureRecognizer.setDelaysTouchesEnded( false );
        FMessageView.addGestureRecognizer( FUITapGestureRecognizer );
      end;
    
      FMessageView.setTitle( NSSTR( Title ) );
    
      LView := nil;
    
      if Assigned(GetSharedApplication.keyWindow.rootViewController) then
        LView := GetSharedApplication.keyWindow.rootViewController.view;
    
      if LView = nil then
      begin
        p := GetSharedApplication.keyWindow.subviews.objectAtIndex(0);
        if Assigned(p) then
          LView := TUIView.Wrap(p);
      end;
      if (FPopoverControl <> nil) and (IsIPad) then
      begin
    
        ARect.origin.x := FPopoverControl.AbsoluteRect.Left;
        ARect.origin.y := (FPopoverControl.AbsoluteRect.Top) + GetSharedApplication.statusBarFrame.size.height;
        ARect.size.width := FPopoverControl.Width;
        ARect.size.height := FPopoverControl.Height;
        FMessageView.showFromRect(ARect, LView, True);
      end
      else
      FMessageView.showInView(LView);
    {$ENDIF}
    end;
    
     

    Last edit: Graham 2015-01-21
  • Graham

    Graham - 2015-01-21

    Just edited the above due to a compile error. Fixed now :-)

     
  • Babak Yaghoobi

    Babak Yaghoobi - 2015-01-21

    Hi Graham,

    Thank you for good work,
    Please attach DPF.iOS.UIActionSheet.pas full source code file,

    Regards.

     
  • Graham

    Graham - 2015-01-21

    Sure, here you are.

    Graham

     
  • Babak Yaghoobi

    Babak Yaghoobi - 2015-01-21

    Hi Graham

    Thank you again,
    I will put it in SVN directory after it's working ! :)

    Regards

     
  • Graham

    Graham - 2015-01-23

    Hi Babak,

    Please could you integrate the above change into the SVN so I don't overwrite my changes here.

    Thanks,
    Graham

     
  • Babak Yaghoobi

    Babak Yaghoobi - 2015-01-23

    Hi Graham,

    SVN, Updated.

    Thank you for this good job,

    Regards

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.