嘿我想这样做这个我的按钮和按钮有文本字段我想要这样做当我按下按钮时动作选择器出现并给出4到5个字符串列表无论我选择它将在文本字段上apear在按钮中。请帮帮我
答案 0 :(得分: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;
}
}