我需要将TextField
添加到UIAlertView
。我明白,苹果不鼓励这种方法。那么是否有任何图书馆可以将TextField
添加到UIAlertView
外观相似的框架中?将TextField添加到UIAlertView
回答
尝试这样:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Title"
message:@"\n\n"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Save", nil] autorelease];
CGRect rect = {12, 60, 260, 25};
UITextField *dirField = [[[UITextField alloc] initWithFrame:rect] autorelease];
dirField.backgroundColor = [UIColor whiteColor];
[dirField becomeFirstResponder];
[alert addSubview:dirField];
[alert show];
你可以试试:
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Your title here!" message:@"this gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView addSubview:testTextField];
[myAlertView show];
[myAlertView release];
关注这个link查看详细。
是的,你的代码有效。但是苹果不赞成这种方式,因为它使用私有API。有没有其他办法可以解决这个问题? – shajem 2012-04-01 07:17:28
这里没有使用私有API .. – 2012-04-01 08:14:03
@anonymous OP引用文档中的句子:此类的视图层次结构是私有的,不能修改。 – 2012-04-01 08:53:37
指这个...... http://iosdevelopertips.com/undocumented/alert-with-textfields.html这是私人api,如果您将它用于appstore中的应用程序,它可能会被拒绝,但它对于企业发展很好。
感谢您的回复,但我希望将我的应用发布到appstore:S – shajem 2012-04-01 07:16:28
比我不会建议您应该实施此操作。 – 2012-04-01 07:19:14
我使用的是BlockAlertsAndActionSheets而不是AppleView组件和AlertSys和ActionSheets,因为我更喜欢blocks-approach。源代码中还包含BlockTextPromptAlertView
,这可能是您想要的。您可以替换该控件的图像以获得Apple风格。
Tutorial which gets you started
例子:
- (IBAction)newFolder:(id)sender {
id selfDelegate = self;
UITextField *textField;
BlockTextPromptAlertView *alert = [BlockTextPromptAlertView promptWithTitle :@"New Folder"
message :@"Please enter the name of the new folder!"
textField :&textField];
[alert setCancelButtonWithTitle:@"Cancel" block:nil];
[alert addButtonWithTitle:@"Okay" block:^{
[selfDelegate createFolder:textField.text];
}];
[alert show];
}
- (void)createFolder:(NSString*)folderName {
// do stuff
}
你能告诉我一个使用'BlockTextPromptAlertView'的例子。我的印象是开发人员不允许向UIAlertView添加任何文本字段等。 – shajem 2012-04-01 07:15:49
你的印象是对的。这就是为什么BlockAlertsAndActionSheets完全不使用UIAlertView的**,而是完全独立的实现,没有使用任何私有API。我目前无法访问我的源代码,但可以在几个小时内使用代码示例更新我的答案。 – 2012-04-01 08:05:39
更新了示例用法 – 2012-04-01 08:47:13
不幸的是,这唯一的官方API是iOS 5的最多,这是一个名为alertViewStyle
属性,它可以设置为以下参数:
UIAlertViewStyleDefault
UIAlertViewStyleSecureTextInput
UIAlertViewStylePlainTextInput
UIAlertViewStyleLoginAndPasswordInput
UIAlertViewStylePlainTextInput
是你想要的。
苹果公司强烈建议不要使用上述视图层次结构。
我必须为iOS 4和iOS 5创建此项。所以您知道任何其他我可以使用的图书馆 – shajem 2012-04-01 08:01:51
对于iOS5的:
UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Title" message:@"Please enter someth" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
[av textFieldAtIndex:0].delegate = self;
[av show];
此外,您将需要实现UITextFieldDelegate
,UIAlertViewDelegate
协议。
添加从“Shmidt”,代码捕捉UIAlertView中输入的文本回答粘贴以下(谢谢你“韦恩哈特曼” Getting text from UIAlertView)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
self.userNumber = [alertView textFieldAtIndex:0].text;
if (self.userNumber) {
// user enetered value
NSLog(@"self.userNumber: %@",self.userNumber);
} else {
NSLog(@"null");
}
}
}
由于iOS的8 UIAlertView
已被弃用,取而代之的UIAlertController
,这增加了支持添加使用该方法UITextField
S:
- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;
为一个例子见this answer。
首先将UIAlertViewDelegate添加到ViewController中。.h文件一样
#import <UIKit/UIKit.h>
@interface UIViewController : UITableViewController<UIAlertViewDelegate>
@end
,比添加下面,你要提醒显示代码,
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message"
delegate:self
cancelButtonTitle:@"Done"
otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
,它的委托方法,它返回的UITextField
什么输入-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%@", [alertView textFieldAtIndex:0].text);
}
- 1. 在uialertview中添加textfield的iphone验证
- 2. 将UIActionsheet添加到UIAlertView中
- 3. 将元素添加到UIAlertview
- 4. 将UIWebView添加到UIAlertView iOS
- 5. 将TextField添加到JOptionPane
- 6. 将图像添加到TextField
- 7. 如何将UITableView添加到UIAlertView?
- 8. 将文本框添加到UIAlertView
- 9. 如何将UIScrollView添加到UIAlertView
- 10. 如何将userInfo添加到UIAlertView?
- 11. 如何将按钮添加到textfield iphone?
- 12. 将textField数据添加到列表。 Swing
- 13. 将多列添加到radiobuttonlist textfield
- 14. 如何将textField添加到工具栏
- 15. UIAlertView - 通过代码添加文本框中的textfield值
- 16. 当我在alertview上添加textview时,将TextView添加到UIAlertView
- 17. 不添加TextField到jFrame
- 18. TextField覆盖UIAlertView的按钮
- 19. uialertview textfield隐形ios 10.1.1
- 20. 添加一个UILabel或NSString到UIalertView
- 21. 如何将自定义UIbutton添加到UIAlertView?
- 22. iphone-sdk:将文本框添加到UIAlertview不适用于iOS 4?
- 23. 将图像添加到uialertview和有关私有API
- 24. 如何在Swift中将日期选择器添加到UIAlertController(UIAlertView)?
- 25. 如何将自定义按钮添加到UIAlertView?
- 26. 如何在iOS中将3个UITextFields添加到UIAlertView中?
- 27. 添加多个confirmationalerts(UIAlertView)
- 28. UIAlertView中添加的NSString
- 29. 在UIAlertView中设置TextField和PickerView
- 30. 如何将TextField和CheckBox添加到相同的AlertView中
我认为苹果阻止你的做法。 – shajem 2012-04-01 07:13:45