在iOS开发中,UIActionSheet是一种常用的弹出式菜单,用于向用户提供一组选项。要在UIActionSheet中传递参数,您可以使用以下方法:
@interface CustomActionSheet : UIActionSheet
@property (nonatomic, strong) NSDictionary *params;
@end
initWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles:
方法,并添加一个新的参数来接收传递的参数。@interface CustomActionSheet : UIActionSheet
@property (nonatomic, strong) NSDictionary *params;
- (instancetype)initWithTitle:(NSString *)title delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles params:(NSDictionary *)params;
@end
clickedButtonAtIndex:
方法,并在该方法中使用传递的参数。@implementation CustomActionSheet
- (void)clickedButtonAtIndex:(NSInteger)buttonIndex {
// 使用传递的参数
NSLog(@"Params: %@", self.params);
}
@end
CustomActionSheet *actionSheet = [[CustomActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Button 1", @"Button 2", nil, params:@{@"key": @"value"}];
[actionSheet showInView:self.view];
这样,您就可以在UIActionSheet的点击事件中使用传递的参数了。
领取专属 10元无门槛券
手把手带您无忧上云