2015-04-01 97 views
0

我使用textinput創建UIAlertView,但我清除了背景並給出了顯示黑色邊框的邊框顏色。UIAlertView UITextField顯示黑色邊框

UIAlertView *alerttorename=[[UIAlertView alloc]initWithTitle:@"Dropbox Export" message:@"Export As:" delegate:projectDetailsReference cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
      alerttorename.alertViewStyle = UIAlertViewStylePlainTextInput; 
      [[alerttorename textFieldAtIndex:0] setText:[appDelegate.projectName stringByDeletingPathExtension]]; 
      UITextField *textField = [alerttorename textFieldAtIndex:0]; 
      [textField setBackgroundColor:[UIColor clearColor]]; 
      textField.borderStyle=UITextBorderStyleRoundedRect ; 
      textField.layer.borderColor=[[UIColor lightGrayColor]CGColor]; 
      textField.layer.borderWidth = 0.1f; 
      textField.layer.cornerRadius = 5; 
      textField.clipsToBounds  = YES; 

      alerttorename.tag=233; 
      [alerttorename show]; 
+0

'UIAlertView中' 被棄用iOS8上,更好的用戶UIAlertController'..請參閱我的回答..! ! – itsji10dra 2015-04-01 06:51:45

回答

0

參考這個例子中,UIAlertController:

UIAlertController *alertController = [UIAlertController 
       alertControllerWithTitle:alertTitle 
           message:alertMessage 
          preferredStyle:UIAlertControllerStyleAlert]; 

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) 
{ 
     textField.placeholder = NSLocalizedString(@"LoginPlaceholder", @"Login"); 
}]; 

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) 
{ 
     textField.placeholder = NSLocalizedString(@"PasswordPlaceholder", @"Password"); 
     textField.secureTextEntry = YES; 
}]; 

文本字段的值可以在確定操作處理程序進行檢索:

UIAlertAction *okAction = [UIAlertAction 
    actionWithTitle:NSLocalizedString(@"OK", @"OK action") 
    style:UIAlertActionStyleDefault 
    handler:^(UIAlertAction *action) 
    { 
    UITextField *login = alertController.textFields.firstObject; 
    UITextField *password = alertController.textFields.lastObject; 
    }]; 

有關UIAlertController更多信息,請參閱本link

+0

儘管您的答案與現代API相當,但有些情況下何時無用。如果您想爲iOS <8.x提供向後兼容性,則不能使用「UIAlertController」。所以在某種程度上,你還沒有回答原來的問題。如果有人問你蘋果,你不要只給他們橙子,並說,這是更好的。 – n00bProgrammer 2015-04-01 07:20:29

0

使用此類別代替UIView可以在iOS > 7.0中顯示alertView。在這裏,您無需擔心UIAlertViewUIAlertViewController

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) 

@interface UIView (AlertView) 
+(void)showSimpleAlertWithTitle:(NSString *)title 
         message:(NSString *)message 
      cancelButtonTitle:(NSString *)cancelButtonTitle; 
@end 

@interface UIView (AlertView) 

+(void)showSimpleAlertWithTitle:(NSString *)title 
         message:(NSString *)message 
      cancelButtonTitle:(NSString *)cancelButtonTitle 
{ 
    if(SYSTEM_VERSION_LESS_THAN(@"8")) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle: title 
                message: message 
                delegate: nil 
              cancelButtonTitle: cancelButtonTitle 
              otherButtonTitles: nil]; 
     [alert show]; 
    } 
    else 
    { 
     // nil titles break alert interface on iOS 8.0, so we'll be using empty strings 
     UIAlertController *alert = [UIAlertController alertControllerWithTitle: title == nil ? @"": title 
                    message: message 
                  preferredStyle: UIAlertControllerStyleAlert]; 

     UIAlertAction *defaultAction = [UIAlertAction actionWithTitle: cancelButtonTitle 
                  style: UIAlertActionStyleDefault 
                  handler: nil]; 

     [alert addAction: defaultAction]; 

     UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController; 
     [rootViewController presentViewController: alert animated: YES completion: nil]; 
    } 
} 

@end 

使用這一類是這樣的:

[UIView showSimpleAlertWithTitle:@"Title here" message:@"Message here" cancelButtonTitle:@"Ok"];