-2

I have an application in which I'm using a specific design for a reason. I put a text field in an alert view above an otherbutton with a background image. Everything is working fine in ios 6 version.

UIAlertView *av=[[UIAlertView alloc] initWithTitle:@"fdhdj" message:@" hdfjkhfjkhdk" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@" ",@"cancel",nil];
     av.alertViewStyle = UIAlertViewStylePlainTextInput;
 namefield = [[UITextField alloc] initWithFrame:CGRectMake(10.0,43.0, 264.0, 44.0)];
    namefield.borderStyle = UITextBorderStyleNone;
    namefield.background = [UIImage imageNamed:@"text_field_default.png"];
    namefield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    namefield.textAlignment = UITextAlignmentCenter;
    //[namefield setBackgroundColor:[UIColor whiteColor]];
    [av addSubview:namefield];
    [namefield release];
    av.tag=12;
    av.delegate=self;  
    [av show];

    [av release];

But now in ios 7, I heard you can't easily alter the view hierarchy of a UIAlertView. One alternative for this case is to set

alert.alertViewStyle = UIAlertViewStylePlainTextInput

But can we add that text field in wherever we want? As in my case above the first otherbutton.can anybody help me?

Homam
  • 5,018
  • 4
  • 36
  • 39
hacker
  • 8,919
  • 12
  • 62
  • 108
  • 2
    Why not do your own UIView hierarchy that you have complete control over instead of fooling with UIAlertView. [Apple's documentation for UIAlertView states](https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html#//apple_ref/doc/uid/TP40006802-CH3-DontLinkElementID_2): "The view hierarchy for this class is private and must not be modified.", which is what you're doing by adding subviews. – Michael Dautermann Oct 07 '13 at 06:33

4 Answers4

3
 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Enter Student Name" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Save",nil];

 [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
 [alertView show];

i used to do like this ,and its working very fine

kshitij godara
  • 1,523
  • 18
  • 30
2

This is my component to support addSubview with alertView in iOS7.

CXAlertView - Custom alert-view which allow you to add view as main content.

enter image description here

Chris
  • 428
  • 2
  • 10
0

The simple answer to your question is NO, you can't change anything in this testField for the UIAlertViewStylePlainTextInput and you shouldn't.

This is from Apple:

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

And unfortunately what you heard I heard you can't easily alter the view hierarchy of a UIAlertView is wrong, you cannot alter the view hierarchy of a UIAlertView in iOS7 at all.

There are a good alternative on the web, you can check in cocoacontrols.com

Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
0

You can't easily alter the view hierarchy of a UIAlertView in iOS 7. (Nor should you; the documentation specifically tells you not to.) Head over to the developer forums to see a long discussion about it.

One alternative in your case is to set alert.alertViewStyle = UIAlertViewStylePlainTextInput; This will add a text field for you. You can access it in the UIAlertView delegate callback by using UITextField *textField = [alertView textFieldAtIndex:0];.

Ketan Ubhada
  • 275
  • 3
  • 14