嘿我想这样做这是我的按钮和按钮中有文本字段我想这样做当我按下按钮时,操作表选择器会出现并给出 4 到 5 个字符串列表,无论我选择什么,它都会出现在按钮中的文本字段上. 请帮我
问问题
842 次
1 回答
1
首先为您的按钮添加一个目标。在 Objective-C 中,这将是这样的:
[myButton addTarget:self
action:@selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
然后创建方法buttonPressed
。一个例子是:
- (void)buttonPressed:(id)sender {
if ([sender isEqual:self.myButton]) {
//This is where you can create the UIAlertController
}
}
然后,创建UIAlertController
:
UIAlertController *myAlertController = [UIAlertController alertControllerWithTitle:@"Title"
message:@"Message"
preferredStyle:UIAlertControllerStyleActionSheet];
然后,您为要在操作表上显示的每个按钮创建操作。尽管操作块可以为空,但您需要为按钮设置标题和操作。
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"Action 1"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
//Whatever you want to have happen when the button is pressed
}];
[myAlertController addAction:action1];
//repeat for all subsequent actions...
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
// It's good practice to give the user an option to do nothing, but not necessary
}];
[myAlertController addAction:cancelAction];
最后,您提出UIAlertController
:
[self presentViewController:myAlertController
animated:YES
completion:^{
}];
笔记:
如果您正在为 iPad 构建并为 UIAlertController 使用 Action Sheet 样式,那么您将需要为 UIAlertController 设置一个源来呈现。这可以这样做:
if ([sender isKindOfClass:[UIView class]]) {
if ([myAlertController.popoverPresentationController respondsToSelector:@selector(setSourceView:)]) { // Check for availability of this method
myAlertController.popoverPresentationController.sourceView = self.myButton;
} else {
myAlertController.popoverPresentationController.sourceRect = self.myButton.frame;
}
}
于 2016-02-29T13:04:27.533 回答