我需要將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
- 6. 將TextField添加到JOptionPane
- 7. 如何將UITableView添加到UIAlertView?
- 8. 如何將UIScrollView添加到UIAlertView
- 9. 將活動指示器添加到UIAlertView
- 10. 如何將userInfo添加到UIAlertView?
- 11. 如何將圖像添加到UIAlertView?
- 12. 將文本框添加到UIAlertView
- 13. 如何將`accessibilityLabel`添加到`UIAlertView`按鈕?
- 14. Vaadin:將佔位符添加到TextField
- 15. 如何將按鈕添加到textfield iphone?
- 16. 將textField數據添加到列表。 Swing
- 17. 如何將textField添加到工具欄
- 18. 將多列添加到radiobuttonlist textfield
- 19. 添加到的UITextField UIAlertView中
- 20. UIAlertView - 通過代碼添加文本框中的textfield值
- 21. 當我在alertview上添加textview時,將TextView添加到UIAlertView
- 22. 不添加TextField到jFrame
- 23. TextField覆蓋UIAlertView的按鈕
- 24. uialertview中的textfield驗證
- 25. uialertview textfield隱形ios 10.1.1
- 26. Wicket TextField - 添加Javascript
- 27. 添加一個UILabel或NSString到UIalertView
- 28. 的TextField不被添加到滾動型
- 29. 如何在iOS7中將UIDatePicker添加到UIALertView中
- 30. iphone-sdk:將文本框添加到UIAlertview不適用於iOS 4?
我認爲蘋果阻止你的做法。 – shajem 2012-04-01 07:13:45