我有一个uiButton,我想显示一个源自它的行动表。 这是关闭按钮的代码,
+ (void)addLeftButton:(UINavigationItem *)navItem withTarget:(id) navTarget
{
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[fixedSpace setWidth:-5];
UIImage *buttonImage = [UIImage imageNamed:@"close-button.png"];
UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[closeButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
closeButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:closeButton];
[closeButton addTarget:navTarget action:@selector(didSelectCloseButton:) forControlEvents:UIControlEventTouchUpInside];
[navItem setLeftBarButtonItems:[NSArray arrayWithObjects:fixedSpace,aBarButtonItem,nil]];
}
此IBAction为关闭按钮,
(IBAction为)didSelectCloseButton:(ID)发送方 {
if([self->_customer getCheckInDraft])
{
[self showActionSheet:sender];
}
在showActionSheet中我有这段代码,
(void) showActionSheet:(id)sender
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
[self->_actionSheet showFromBarButtonItem:sender animated:YES];
}
//这里showFromBarButtonItem将发件人作为uiButton获取,但它崩溃了。单击“关闭”按钮,应用程序崩溃。
答案 0 :(得分:0)
你可以这样做:
UIAlertAction *action1;
UIAlertAction *actionCancel;
action1 = [UIAlertAction actionWithTitle:@"action1" style:UIAlertActionStyleDefault handler:nil];
actionCancel = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDestructive handler:nil];
UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"alert" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
alertView.modalPresentationStyle = UIModalPresentationPopover; // for iPad
[alertView addAction:action1];
[alertView addAction:actionCancel];
if (viewSourse) { // Here your button
alertView.popoverPresentationController.sourceView = viewSourse;
alertView.popoverPresentationController.sourceRect = CGRectMake(viewSourse.frame.size.width/2, viewSourse.frame.size.height/2, 0, 0);
}
[self presentViewController:alertView animated:YES completion:nil];
答案 1 :(得分:0)
这是行动表代码。
- (IBAction)showActionSheet:(id)sender {
/* Dismiss keyboard*/
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"What ever you want to ask?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete it" otherButtonTitles:@"Copy", @"Duplicate", nil]; //Change the options that you want to display as per your requirement.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// In this case the device is an iPad.
[actionSheet showFromRect:[(UIButton *)sender frame] inView:self.view animated:YES];
}
else{
// In this case the device is an iPhone/iPod Touch.
[actionSheet showInView:self.view];
}
}
#pragma mark - UIActionSheet method implementation
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"Index = %d - Title = %@", buttonIndex, [actionSheet buttonTitleAtIndex:buttonIndex]);
}
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(@"Action sheet did dismiss");
}
-(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(@"Action sheet will dismiss");
}
/ * UPDATE * /
以上在iOS 8.3及更高版本中已弃用,因此您可以在UIAlertController下使用。
此外,如果您的键盘在用户点击按钮时打开,则键盘会自动关闭并让UIAlertController显示选项。
UIAlertController *alertController;
UIAlertAction *destroyAction;
UIAlertAction *otherAction;
alertController = [UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
destroyAction = [UIAlertAction actionWithTitle:@"Remove All Data"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
// do destructive stuff here
}];
otherAction = [UIAlertAction actionWithTitle:@"Blah"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
// do something here
}];
// note: you can control the order buttons are shown, unlike UIActionSheet
[alertController addAction:destroyAction];
[alertController addAction:otherAction];
[alertController setModalPresentationStyle:UIModalPresentationPopover];
UIPopoverPresentationController *popPresenter = [alertController
popoverPresentationController];
UIButton *btn = (UIButton *)sender;
popPresenter.sourceView = btn;
popPresenter.sourceRect = [btn bounds];
[self presentViewController:alertController animated:YES completion:nil];