iPhone Disabling UIActionSheet buttons

2020-02-06 01:53发布

I want to disable buttons in the UIAction sheet and enable them after a certain condition is true. How do I achieve this? Any ideas?

7条回答
SAY GOODBYE
2楼-- · 2020-02-06 02:00

This is probably a bit late. But, the way I figured out is create UIButton(s) and add them to UIActionSheet subview. Make certain these buttons are placed on top and completely cover the default UIActionSheet button(s) to replace. When the UIButton is placed over the default UIActionSheet button, its UIResponder takes precedence over UIActionSheet button UIResponder. So, doing so you can disable and enable those buttons however you'd like anywhere in you UIViewController logic. This may be an alternative to accessing private methods to the SDK (like above - UIThreePartButton) and apple might reject your app. I believe this follows the Apple guidelines.

i.e. Setup UIButtons in order to enable / disable after certain conditions occur.

// Instantiate once within a method
if (self.actionSheet==nil)  {
    UIActionSheet *as = [[UIActionSheet alloc] 
                         initWithTitle:@""
                         delegate:self 
                         cancelButtonTitle:@"Cancel"
                         destructiveButtonTitle:@"Load Data"
                         otherButtonTitles:@"Update Data",nil];

    //[actionSheet showInView:self.view];
    self.loadUIButton.frame = CGRectMake(24.0f,25.0f,275.f,46.0f);
    [as addSubview: self.loadUIButton];
    self.updateUIButton.frame = CGRectMake(24.0f,78.0f,275.f,46.0f);
    [as addSubview: self.updateUIButton];
    //[actionSheet addSubview: self.cancelUIButton];
    //[as showFromToolbar: self.navigationController.toolbar];
    self.actionSheet = as;
    [as release];
}
[self.actionSheet showFromToolbar: self.navigationController.toolbar];

NOTE: This worked perfectly in a previous app I built.

查看更多
来,给爷笑一个
3楼-- · 2020-02-06 02:01

You could try [actionSheet valueForKey:@"_buttons"] in your UIActionSheetDelegate like so:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    for (UIButton *btn in [actionSheet valueForKey:@"_buttons"])
    {
        [btn setBackgroundImage:[UIImage imageNamed:@"btnActionSheetUp.png"]     forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"btnActionSheetDown.png"] forState:UIControlStateHighlighted];
    }
}

Hopefully this wont change in future releases of the sdk.

查看更多
做个烂人
4楼-- · 2020-02-06 02:03

If you add the view to the action sheet, the view wont receive any event. You need to the view to the superview of the action sheet.

See below how i've added an button with touch up event:

- (void)didPresentActionSheet:(UIActionSheet *)actionSheet{
CGRect rect = CGRectMake(0,0, 320, 480);
UIButton* anImage = [[UIButton alloc] init];
[anImage setTitle:@"GHello worl" forState:UIControlStateNormal];
[anImage setTitle:@"GHello worl dfasd" forState:UIControlStateHighlighted];
[anImage setTitle:@"GHello worl selected" forState:UIControlStateSelected];
//[anImage setBackgroundImage:[UIImage imageNamed:@"photo.png"] forState:UIControlStateNormal];
[anImage addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
rect = CGRectMake(0,0, 320, 220);
anImage.frame =rect;


[actionSheet.superview addSubview:anImage];



[anImage release];

}

查看更多
我命由我不由天
5楼-- · 2020-02-06 02:06

I faced a similar problem when I had an action sheet that changes based on some conditions. I omitted the choices that are not applicable from the action sheet altogether but in some cases I end up with an action sheet with just one choice. This seems a bit silly, since I could activate that choice immediate without displaying the sheet at all.

However, I also want users to learn that clicking a certain button on screen gives them a list of choices they can choose from. That's why I want to display the action sheet also in cases when it has just one choice. I thought about disabling the non-applicable choices instead of omitting them from the sheet but that didn't feel right, either.

My case is related to having a user-icon in a custom bottom bar and pressing it presents the following choices:

  • if logged in
    • My Profile
    • Log out
  • if not logged in
    • Log in
查看更多
贼婆χ
6楼-- · 2020-02-06 02:08

UIActionSheet is not intended for customizing. It should display actual set of available options. It should not change button's availability while on top. Just remove unused buttons, or use custom view instead

查看更多
家丑人穷心不美
7楼-- · 2020-02-06 02:10

Is there a circumstance that can change, while the action sheet is open, that could cause the button to become enabled? If not, I think the better approach is to alter the buttons that the sheet displays based on your condition.

Otherwise, the only way of handling this is to iterate through the sheet's subviews, like Craig said, and look for the UIButton objects. I'd be careful about using the button's title, though, because the title could (and should!) be localized for different languages. So comparisons against the title aren't all that reliable. Since you didn't create the button, you don't really know what the tag or action of each button would be, either, so that's a bit difficult, too.

Presumably, the buttons will appear in the subviews array in the order you specified them to the UIActionSheet, but since this isn't documented, there's no guarantee that they will appear in that order, or that they will continue to appear in that order in future releases of the Cocoa Touch SDK. Because of that, I'd worry mainly about being rejected from the App Store for using undocumented functionality.

查看更多
登录 后发表回答