2017-07-22 42 views
1

我已經得到了從試圖找到一個答案,這也太沮喪,所以我會爲一個問題...從UIAlertView中文本框中輸入提取的Objective-C

如何從一個UIAlertView中獲得的原始文本Objective-C中的文本框?

這裏是我的代碼:

UIButton *AppCrashButton = [UIButton buttonWithType: UIButtonTypeCustom]; 
AppCrashButton.frame = CGRectMake(0, (self.view.frame.size.height - 44)/6 * 1, self.view.frame.size.width, (self.view.frame.size.height - 44)/6); 

[AppCrashButton setTitle: @"Ping" forState: UIControlStateNormal]; 
[AppCrashButton addTarget: self action: @selector(Click) forControlEvents: UIControlEventTouchUpInside]; 

AppCrashButton.backgroundColor = [UIColor colorWithRed:0.19 green:0.19 blue:0.19 alpha:1.0]; 
AppCrashButton.layer.borderColor = [UIColor colorWithRed:0.96 green:0.26 blue:0.21 alpha:1.0].CGColor; 
AppCrashButton.layer.borderWidth = 0.5f; 

[self.view addSubview: AppCrashButton]; 
-(void) Click { 
    UIAlertView *AppCrashAlert = [[UIAlertView alloc] initWithTitle: @"IP Ping" message: @"Please enter the IP address" delegate: self cancelButtonTitle: @"Cancel" otherButtonTitles: @"OK", nil]; 
    AppCrashAlert.alertViewStyle = UIAlertViewStylePlainTextInput; 

    [AppCrashAlert show]; 
    [AppCrashAlert release]; 
} 

它顯示了一個自定義按鈕,一旦點擊,執行「點擊」的方法,其中執行ping主機。

我想知道如何從文本輸入中獲取文本。

感謝

+1

僅供參考 - 'UIAlertView'被棄用早在iOS版8.您應該使用'UIAlertController'。 – rmaddy

回答

1
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    NSLog(@"Entered: %@",[[alertView textFieldAtIndex:0] text]); 
} 

要在創建得到中輸入文字UIAlertView中文本字段

1

設置UIAlertViewDelegate委託給你的控制器

@interface YourViewController()<UIAlertViewDelegate> 

後您UIAlertView設置它的委託Self在這裏你的情況

UIAlertView *AppCrashAlert = [[UIAlertView alloc] initWithTitle: @"IP Ping" message: @"Please enter the IP address" delegate: self cancelButtonTitle: @"Cancel" otherButtonTitles: @"OK", nil]; 
AppCrashAlert.alertViewStyle = UIAlertViewStylePlainTextInput; 
// Set delegate 
AppCrashAlert.delegate = Self; 
[AppCrashAlert show]; 
[AppCrashAlert release]; 

然後,

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex(NSInteger)buttonIndex{ 
    NSLog(@"Text : %@",[[alertView textFieldAtIndex:0] text]); 
} 
+0

我會在哪裏放置這個? 另外,是否可以提醒輸入的文本,而不是'NSLog'呢? –

+0

您想在按下ok後顯示警報嗎? – vp2698